Source Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()

const int inf = 2e9;
const ll infll = 1e18;
const int M = 1000000007;
const int N = 2e5 + 10;

int n;
pair<int, int> arr[N];

bool cmp(pair<int, int> a, pair<int, int> b){
    return abs(a.first - a.second) > abs(b.first - b.second);
}

int main(){
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> arr[i].first >> arr[i].second;
    }
    sort(arr, arr + n, cmp);
    int a = 0, b = 0;
    long long ans = 0;
    for(int i = 0; i < n; ++i){
        if(a == n / 2){
            ans += arr[i].second;
        }else if(b == n / 2){
            ans += arr[i].first;
        }else if(arr[i].first > arr[i].second){
            ans += arr[i].first ;
            ++a;
        }else{
            ans += arr[i].second;
            ++b;
        }
    }
    cout << ans;
}

Copy
Two Teams Hambubger
GNU G++17
146 ms
2.7 MB
Accepted