Source Code
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin>>n;
	vector<int> p(n),c(n);
	vector<pair<int,pair<int,int>>> d;
	// d.second = p->0 OR c->1
	int cc = n/2;
	int pp = n/2;
	for(int i=0; i<n; i++){
		cin>>p[i]>>c[i];
		d.push_back({p[i]-c[i],{0,i}});
		d.push_back({c[i]-p[i],{1,i}});
	}
	sort(d.rbegin(),d.rend());
	vector<int> taken (n,0);
	int ans =0;
	for(int i=0;i<2*n;++i){
		int choice = d[i].second.first;
		int index  = d[i].second.second;
		if(taken[index]){
			continue;
		}
		if(choice == 0){
			// p
			if(pp == 0)
				continue;
			else{
				pp--;
				taken[index] = 1;
				ans += p[index];
			}
		}else{
			// c
			if(cc == 0)
				continue;
			else{
				cc--;
				taken[index] = 1;
				ans += c[index];
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}
Copy
Two Teams Saq
GNU G++17
3 ms
932 KB
Wrong Answer