原题:
DescriptionRoundgod is a famous milk tea lover at Nanjing University second to none. This year, he plans to conduct a milk tea festival. There will be n classes participating in this festival, where the ith class has ai students and will make bi cups of milk tea.Roundgod wants more students to savor milk tea, so he stipulates that every student can taste at most one cup of milk tea. Moreover, a student can't drink a cup of milk tea made by his class. The problem is, what is the maximum number of students who can drink milk tea?
Input
The first line of input consists of a single integer T (1≤T≤25), denoting the number of test cases.
Each test case starts with a line of a single integer n (1≤n≤106), the number of classes. For the next n lines, each containing two integers a,b (0≤a,b≤109), denoting the number of students of the class and the number of cups of milk tea made by this class, respectively.
It is guaranteed that the sum of n over all test cases does not exceed 6×106.
Output
For each test case, print the answer as a single integer in one line.
解题思路:
AC代码(核心部分):
注意ans可能很大(1015),所以开long long。
long long a[6000010],b[6000010],t,n,tot,ans,x,y,z; int main() { int i; scanf("%d",&t); while(t--) { tot=ans=0; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d%d",&a[i],&b[i]); tot+=b[i]; } tt=tot; for(i=1;i<=n;i++) { x=tot-b[i],y=min(x,a[i]); if((tt-y)<0) y=tt; tt-=y,ans+=y; } printf("%lld\n",ans); } }