Question A: Military Country X
Time limit: 1 Sec Memory Limit: 256 MB
Face questions
He declined to publicly face the question.
answer
Simple greed.
According to the number of soldiers surviving (ie, the number of soldiers involved in the fighting minus the number of soldiers killed in action) sort.
If the number of surviving soldiers of the same sort in accordance with the number of soldiers involved in the fighting.
Order again sweep statistical answer.
#include<bits/stdc++.h> #define int long long #define rint register int #define read(A) A=init() using namespace std; inline int init() { int a=0,b=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')b=-1;ch=getchar();} while(ch>='0'&&ch<='9'){a=(a<<3)+(a<<1)+ch-'0';ch=getchar();} return a*b; } int T,n,ans; struct node{int ds,ns,cs;}p[1000005]; inline bool cmp(node A,node B){ return (A.cs==B.cs)?A.ns<B.ns:A.cs<B.cs; } signed main() { // freopen("data.in","r",stdin); // freopen("std.out","w",stdout); read(T); while(T--) { read(n);ans=0; for(rint i=1,ind,inn;i<=n;++i) { read(ind),read(inn); p[i].ds=ind,p[i].ns=inn; p[i].cs=inn-ind; } sort(p+1,p+n+1,cmp);ans=p[1].ns; for(rint i=2;i<=n;++i)ans=max(ans+p[i].ds,p[i].ns); printf("%lld\n",ans); } }