2019 Multi-University Training Contest 8 Roundgod and Milk Tea

本文探讨了一个有趣的问题:在南京大学的奶茶节上,如何最大化可以让学生品尝到奶茶的人数。通过精心设计的算法,我们解决了班级间奶茶分配的挑战,确保每个学生都能享受到一杯来自其他班级的手制奶茶。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Roundgod and Milk Tea

Problem Description

Roundgod 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.

Sample Input

1
2
3 4
2 1

Sample Output

3

代码

#include<bits/stdc++.h>
using namespace std;
typedef struct {
    long long a,b;
}node;
bool cmp(node a,node b){
    return a.b<b.b;
}
node m[1000006];
long long s[1000006];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        long long sumn=0,sum=0;;
        for(int i=0;i<n;i++){
            scanf("%d%d",&m[i].a,&m[i].b);
            sumn+=(long long)m[i].a;
        }
        for(int i=0;i<n;i++){
            s[i]=sumn-m[i].a;
        }
        for(int i=0;i<n;i++){
            sum+=min(s[i],m[i].b);
        }
        printf("%lld\n",min(sum,sumn));
    }
    return 0; 
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值