Question: it takes time to brainwash T soldiers and ask them to brainwash their work. Just get out of work after A's brain and obey the work. The work time is B; how long does it take for all soldiers to finish their brains and finish all their work? Key Point: After training a soldier, the soldier has gone to do things. At this time, the next practice can be trained: Obviously, we need to find the longest time to do things. In this way, when doing things, he can train other soldiers to save time, so he first ranks the work time and then simulates various situations ~ Don't mess up, don't mess up! AC code:
#include<stdio.h> #include<algorithm> using namespace std; struct soldier { int A; int B; }s[1234]; int cmp(soldier a, soldier b) { return a.B > b.B; } int cas = 0; int main() { int T; int sum; while(scanf("%d", &T) != EOF && T) { int t1, t2; sum = 0; for(int i = 0; i < T; i++) scanf("%d %d", &s[i].A, &s[i].B); sort(s, s+T, cmp); for(int i = 0; i < T; i++) { if(i == 0) { sum += s[i].A; t1 = s[i].B; } if(i != 0) { t2 = s[i].A + s[i].B; if(t2 > t1) { sum += s[i].A; t1 = s[i].B; } else { t1 = t1 - s[i].A; sum += s[i].A; } } } sum += t1; printf("Case %d: ", ++cas); printf("%d\n", sum); } return 0; }