Source Code
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define PI acos(-1)
#define fr first
#define se second
#define int long long
const int MAXN = 1001;
const int MOD = 1e9 + 7;

signed main() {

	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	priority_queue<pair<int,int>> pq1,pq2,pq11,pq22;
	int n;
	cin >> n;
	int mid = n / 2;
	for (int i = 0 ;  i < n ; i++){
		int a,b;
		cin >> a >> b;
		pq1.push({a,i});
		pq2.push({b,i});
		pq11.push({a,i});
		pq22.push({b,i});
	}
	int turn = 0;
	int sum1 =0, sum2 = 0, a = 0, b = 0;
	int vis[n+1];
	int ans = 0;
	memset(vis,0,sizeof(vis));
	while((int)pq1.size() and (int)pq2.size()){
		if (turn == 0){
			if (a < mid){
				while(pq1.size()){
					auto z = pq1.top();
					pq1.pop();
					if (vis[z.second]) continue;
					else {
						sum1 += z.first;
						a++;
						vis[z.second] = 1;
						break;
					}				
				}
			} else {
				while(pq2.size()){
					auto z = pq2.top();
					pq2.pop();
					if (vis[z.second]) continue;
					else {
						sum2 += z.first;
						b++;
						vis[z.second] = 1;
						break;
					}
				}
			}
		} else {
			if (b < mid){
				while(pq2.size()){
					auto z = pq2.top();
					pq2.pop();
					if (vis[z.second]) continue;
					else {
						sum2 += z.first;
						b++;
						vis[z.second] = 1;
						break;
					}				
				}
			} else {
				while(pq1.size()){
					auto z = pq1.top();
					pq1.pop();
					if (vis[z.second]) continue;
					else {
						sum1 += z.first;
						a++;
						vis[z.second] = 1;
						break;
					}
				}
			}
		}
		if (sum1 > sum2){
			turn = 1;
		} else turn = 0;
	}
	ans = sum1 + sum2;
	turn = 1;
	sum1 =0, sum2 = 0, a = 0, b = 0;
	memset(vis,0,sizeof(vis));
	while((int)pq11.size() and (int)pq22.size()){
		if (turn == 0){
			if (a < mid){
				while(pq11.size()){
					auto z = pq11.top();
					pq11.pop();
					if (vis[z.second]) continue;
					else {
						sum1 += z.first;
						a++;
						vis[z.second] = 1;
						break;
					}				
				}
			} else {
				while(pq22.size()){
					auto z = pq22.top();
					pq22.pop();
					if (vis[z.second]) continue;
					else {
						sum2 += z.first;
						b++;
						vis[z.second] = 1;
						break;
					}
				}
			}
		} else {
			if (b < mid){
				while(pq22.size()){
					auto z = pq22.top();
					pq22.pop();
					if (vis[z.second]) continue;
					else {
						sum2 += z.first;
						b++;
						vis[z.second] = 1;
						break;
					}				
				}
			} else {
				while(pq11.size()){
					auto z = pq11.top();
					pq11.pop();
					if (vis[z.second]) continue;
					else {
						sum1 += z.first;
						a++;
						vis[z.second] = 1;
						break;
					}
				}
			}
		}
		if (sum1 > sum2){
			turn = 1;
		} else turn = 0;
	}
	ans = max(ans, sum1+sum2);
	cout << ans << endl;

	/*
	
	


	*/


}
Copy
Two Teams MysteRrion
GNU G++17
3 ms
812 KB
Wrong Answer