#include <bits/stdc++.h>
#define int long long
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define SORT(c) sort(all(c))
#define rSORT(c) sort(rall(c))
#define f first
#define s second
#define pb push_back
#define endl "\n"
using namespace std;
void solve();
bool pr[2000001];//sieve(2000001);
int fact[100000];//factorial();
int choose(int n, int r){return r<0||r>n?0:(r==0||r==n)?1:choose(n-1,r-1)+choose(n-1,r);}
void factorial(){fact[0]=1;for(int i=1;i<10001;i++)fact[i]=fact[i-1]*i%7901;}
void sieve(int n){for(int i=2;i<=n;i++)pr[i]=1;for(int i=2;i<=n;i++)if(pr[i])for(int j=i*2;j<=n;j+=i)pr[j]=0;}
int countSubarrays(int s, int k){if(s<k)return 0;return(s-k+1)*(s-k+2)/2;}
int power(int b, int e){int r=1;while(e>0){if(e % 2 != 0)r*=b;b*= b;e/=2;}return r;}
int bitsNeeded(int n){int count = 0;while (n > 0){count++;n >>= 1;}return count;}
int32_t main(){ios::sync_with_stdio(false);cin.tie(0);int t = 1;int i = 1;
//cin >> t;
while(t--){//cout<<"_________________________________\n";cout<<"Test case Number: "<<i++<<endl<<endl;
solve();
}
}
void solve(){
int n;cin >> n;
int l = 0;
int r = 0;
vector<int> v(n);
int ans = 0;
for(int i = 0;i<n;i++){
int x,y;cin >> x >> y;
if(x >= y){
ans += x;
l++;
}
else{
ans += y;
r++;
}
v[i] = x-y;
}
if(l < r){
int all = (r-l)/2;
rSORT(v);
for(int i = 0;i<n;i++){
if(all == 0) break;
if(v[i] <= 0){
ans += v[i];
all--;
}
if(all == 0) break;
}
}
else{
int all = (l-r)/2;
SORT(v);
for(int i = 0;i<n;i++){
cout << v[i] << " ";
}
}
cout << ans;
}
Copy