#include <iostream>
#include <bitset>
#include <map>
#include <vector>
#include <set>
#include <queue>
#define ll long long
using namespace std;
int main()
{
int n,x,y;
ll sum1=0,sum2=0;
cin >> n;
multiset<pair<int, int>, greater<pair<int, int>>> p, c, a, b;
for (int i = 0; i < n; i++)
{
cin >> x >> y;
p.insert({x, -y});
c.insert({y, -x});
}
a=p,b=c;
for (int i = 0; i < n / 2; i++)
{
if (sum1 > sum2)
{
auto q = *c.begin();
sum2 += q.first;
c.erase(c.begin());
swap(q.first, q.second);
q.first *= -1, q.second *= -1;
p.erase(p.find(q));
q = *p.begin();
sum1 += q.first;
p.erase(p.begin());
swap(q.first, q.second);
q.first *= -1, q.second *= -1;
c.erase(c.find(q));
}
else
{
auto q = *p.begin();
sum1 += q.first;
p.erase(p.begin());
swap(q.first, q.second);
q.first *= -1, q.second *= -1;
c.erase(c.find(q));
q = *c.begin();
sum2 += q.first;
c.erase(c.begin());
swap(q.first, q.second);
q.first *= -1, q.second *= -1;
p.erase(p.find(q));
}
}
p=a,c=b;
ll mx1=sum1,mx2=sum2;
sum1=0,sum2=0;
for (int i = 0; i < n / 2; i++)
{
if (sum1 >= sum2)
{
auto q = *c.begin();
sum2 += q.first;
c.erase(c.begin());
swap(q.first, q.second);
q.first *= -1, q.second *= -1;
p.erase(p.find(q));
q = *p.begin();
sum1 += q.first;
p.erase(p.begin());
swap(q.first, q.second);
q.first *= -1, q.second *= -1;
c.erase(c.find(q));
}
else
{
auto q = *p.begin();
sum1 += q.first;
p.erase(p.begin());
swap(q.first, q.second);
q.first *= -1, q.second *= -1;
c.erase(c.find(q));
q = *c.begin();
sum2 += q.first;
c.erase(c.begin());
swap(q.first, q.second);
q.first *= -1, q.second *= -1;
p.erase(p.find(q));
}
}
cout<<max(mx1+mx2,sum1+sum2);
}
Copy