72 lines
1.9 KiB
C++
72 lines
1.9 KiB
C++
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <iostream>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
// ----- FILE IO -----
|
|
#define STDIN(filename) freopen(filename, "r", stdin)
|
|
#define STDOUT(filename) freopen(filename, "w", stdout)
|
|
#define NL '\n'
|
|
|
|
// ----- FOR LOOPS -----
|
|
#define F(i,L,R) for (int i = L; i < R; i++)
|
|
#define FE(i,L,R) for (int i = L; i <= R; i++)
|
|
#define FD(i,L,R) for (int i = L; i > R; i--)
|
|
#define FDE(i,L,R) for (int i = L; i >= R; i--)
|
|
|
|
// ----- WEZ -----
|
|
#define takeI(n) int (n); cin >> n
|
|
#define takeII(n,m) int (n),(m); cin >> n >> m
|
|
#define takeIII(n,m,k) int (n),(m),(k); cin >>(n) >> (m) >> (k)
|
|
#define whileZ takeI(T); while(T--)
|
|
//
|
|
// ----- MAX MIN -----
|
|
#define REMAX(a,b) (a)=max((a),(b))
|
|
#define REMIN(a,b) (a)=min((a),(b))
|
|
|
|
// ----- DEBUG -----
|
|
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
|
|
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
|
|
#define printV(a) printA(a,0,a.size()-1)
|
|
|
|
// ----- STL DS
|
|
#define ALL(c) (c).begin(),(c).end()
|
|
|
|
int main() {
|
|
takeI(n);
|
|
|
|
vector<int> weights(2*n);
|
|
F(i,0,2*n) cin >> weights[i];
|
|
sort(ALL(weights));
|
|
|
|
vector<int> distances(2*n-1);
|
|
F(i,0,2*n-1) distances[i] = abs(weights[i] - weights[i+1]);
|
|
|
|
int res = 0;
|
|
|
|
F(i,0,n-1) {
|
|
auto it = min_element(ALL(distances));
|
|
size_t idx = distance(distances.begin(), it);
|
|
|
|
res += *it;
|
|
|
|
if (it == distances.begin()) {
|
|
weights.erase(weights.begin() + idx, weights.begin() + idx + 2);
|
|
distances.erase(it, it+2);
|
|
} else if (it == distances.end() - 1) {
|
|
weights.erase(weights.begin() + idx, weights.begin() + idx + 2);
|
|
distances.erase(it-1, it+1);
|
|
} else {
|
|
weights.erase(weights.begin() + idx, weights.begin() + idx + 2);
|
|
distances.erase(it, it+2);
|
|
distances[idx-1] = abs(weights[idx-1] - weights[idx]);
|
|
}
|
|
// find i st distances[i] is minimal
|
|
// remove distances[i], weights[i]
|
|
// recompute distances[i-1], distances[i]
|
|
}
|
|
|
|
cout << res;
|
|
}
|