#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <math.h>
#include <vector>
#include <set>
#include <map>
using namespace std;
long long p[500005], c[500005], f[500005] = { 0 };
int main() {
int n, x, s1, s2;
long long ans = 0, mix1, mix2;
cin >> n;
vector<pair<long long, long long>> v;
for (int i = 0; i < n; i++) {
cin >> p[i] >> c[i];;
v.push_back(make_pair(abs(p[i] - c[i]), i));
}
sort(v.begin(), v.end());
bool flag = 0;
x = s1 = s2 = mix1 = mix2 = 0;
for (int i = 0; i < n; i++) {
if (max(p[i], c[i]) == p[i]) {
if (s1 == n / 2) {
mix1 = 0;
for (int j = 0; j < i; j++) {
if (f[v[j].second] == 1) {
if (c[v[j].second] > mix1) {
x = v[j].second;
f[v[j].second] = 2;
mix1 = c[v[j].second];
}
}
}
ans -= p[x];
ans += c[x];
s2++;
ans += p[i];
f[i] = 1;
}
else {
s1++;
ans += p[i];
f[i] = 1;
}
}
else {
if (s2 == n / 2) {
mix2 = 0;
for (int j = 0; j < i; j++) {
if (f[v[j].second] == 2) {
if (mix2 < p[v[j].second]) {
x = v[j].second;
f[v[j].second] = 1;
mix2 = c[v[j].second];
}
}
}
ans -= c[x];
ans += p[x];
s1++;
ans += c[i];
f[i] = 2;
}
else {
s2++;
ans += c[i];
f[i] = 2;
}
}
}
cout << ans;
return 0;
}
Copy