Codeforces Round #657 (Div. 2) C. Choosing flowers

探讨了在有限类型但无限数量的花卉中,如何通过智能算法为特定场合选择n朵花,以最大化接收者的幸福感。算法基于贪心策略,通过对比不同类型的花带来的额外幸福感,实现最优选择。

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

C. Choosing flowers

time limit per test  1 second   memory limit per test  512 megabytes

 

Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly n flowers.

Vladimir went to a flower shop, and he was amazed to see that there are m types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the i-th type happiness of his wife increases by ai and after receiving each consecutive flower of this type her happiness increases by bi. That is, if among the chosen flowers there are xi>0 flowers of type i, his wife gets ai+(xi−1)⋅bi additional happiness (and if there are no flowers of type i, she gets nothing for this particular type).

Please help Vladimir to choose exactly n flowers to maximize the total happiness of his wife.

Input

The first line contains the only integer t (1≤t≤10000), the number of test cases. It is followed by t descriptions of the test cases.

Each test case description starts with two integers n and m (1≤n≤109, 1≤m≤100000 ), the number of flowers Vladimir needs to choose and the number of types of available flowers.

The following m lines describe the types of flowers: each line contains integers ai and bi (0≤ai,bi≤109) for i -th available type of flowers.

The test cases are separated by a blank line. It is guaranteed that the sum of values m among all test cases does not exceed 100000.

Output

For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly n

flowers optimally.

Example

Input

2
4 3
5 0
1 4
2 2

5 3
5 2
4 2
3 1

Output

14
16

Note

In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5+(1+2⋅4)=14.

In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5+1⋅2)+(4+1⋅2)+3=16.

 

解题思路

我们可以枚举每个物品,如果这个物品的bi大于aj的个数大于等于n个,也就是说全部取a最优,就可以把前n大的a累加作为答案,如果个数不足n个的话,就把符合条件的所有aj累加后再加上,bi乘剩余所需件数,作为答案进行比较

主要就是贪心的思想

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int t;
vector < pair<long long ,long long> >v;
vector <long long> fir;
long long suf[maxn];
int main()
{
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    scanf("%d",&t);
    while(t--)
    {
        fir.clear(); v.clear();
        long long n,m,x,y;
        scanf("%lld%lld",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%lld%lld",&x,&y);
            v.push_back(make_pair(x,y));
            fir.push_back(x);
        }
        sort(fir.begin(),fir.end());
        for(int i=0;i<m;i++) suf[i]=fir[i];
        for(int i=1;i<m;i++)
            suf[i]+=suf[i-1];
        long long ans=0;
        for(int i=0;i<m;i++)
        {
            int cntn=n;
            long long fi=v[i].first;
            long long se=v[i].second;
            int pos=upper_bound(fir.begin(),fir.end(),se)-fir.begin();
            if(m-pos>=cntn)
            {
                long long aans=suf[m-1];
                if(m-cntn>=1)
                    aans-=suf[m-cntn-1];
                ans=max(ans,aans);
            }
            else
            {
                long long aans=suf[m-1];
                if(pos>0)
                     aans-=suf[pos-1];
                cntn-=m-pos;
                if(pos!=m)
                {
                    if(fi<fir[pos])
                    {
                        aans+=fi;
                        cntn--;
                    }
                }
                else
                {
                    aans+=fi;
                    cntn--;
                }
                aans+=(se*cntn);
                ans=max(ans,aans);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值