Source Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[1000000];
int b[1000000];
vector<pair<int, int> > v;
int main() {
    ios_base::sync_with_stdio(0);
    int n;
    cin>>n;
    for (int i=1;i<=n;i++) {
        cin>>a[i]>>b[i];
        v.push_back({abs(a[i]-b[i]), i});
    }
    sort(v.begin(),v.end());
    reverse(v.begin(), v.end());
    ll ret = 0;
    int sz1 = 0, sz2 = 0;
    for (auto x:v) {
        int i = x.second;
        if (a[i] > b[i]) {
            if (sz1<n/2) sz1++, ret+=a[i];
            else sz2++, ret+=b[i];
        } else {
            if (sz2<n/2) sz2++, ret+=b[i];
            else sz1++, ret+=a[i];
        }
    }
    cout<<ret<<endl;
}
Copy
Two Teams RedNextCentury
GNU G++17
76 ms
4.3 MB
Accepted