Source Code
#include <bits/stdc++.h>
using namespace std;
int main()
{
   int n;
   scanf("%d",&n);
   int PS[n]={0};
   int CY[n]={0};
   int absdiff[n]={0};
   long long res=0;
    int test=0;
    int arrPS[n]={0};
    int arrCY[n]={0};
    int sPS=0;
    int sCY=0;
   for(int i=0;i<n;i++)
   {
       scanf("%d",&PS[i]);
       scanf("%d",&CY[i]);
       if(PS[i]>CY[i])
       {
           res+=PS[i];
           test++;
           arrPS[sPS]=PS[i]-CY[i];
           sPS++;
       }
       else if(CY[i]>PS[i])
       {
           res+=CY[i];
           test--;
           arrCY[sCY]=CY[i]-PS[i];
           sCY++;
       }

   }
   test/=2;
   sort(arrPS,arrPS+sPS-1);
   sort(arrCY,arrCY+sCY-1);
   sPS=0;
   sCY=0;
  for(int i=test;i>0;i--)
  {
      res-=arrPS[sPS];
      sPS++;
  }
  for(int i=test;i<0;i++)
  {
      res-=arrCY[sCY];
      sCY++;
  }
   printf("%lld\n",res);

}
Copy
Two Teams Doleh
GNU G++17
3 ms
868 KB
Runtime Error