有m种花,要买n朵花,然后每种花可以买无数朵。
每朵花有两个权值 a i a_i ai , b i ,b_i ,bi,第一次买时价值是 a i a_i ai,再次买该种花时价值是 b i b_i bi,要使价值和最大。
然后根据题意,我们可以读出可能是有某种花会多次选择,这种花的 b b b一定是比不选择的 a a a要大的。
然后我们去枚举可能的 b b b,二分 a a a,找到所有比 b b b大的 a a a。
然后在枚举时判断,选这个 b b b的时候是否选择了这个 a a a,也就是函数中的 j j j和 x x x的关系。
#include
#define INF 0x3f3f3f3f
#define DOF 0x7f7f7f7f
#define endl '\n'
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(case,x); cout<
#define open freopen("ii.txt","r",stdin)
#define close freopen("oo.txt","w",stdout)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
using namespace std;
#define int long long
#define lson rt<<1
#define rson rt<<1|1
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> PII;
const int maxn = 1e5 + 10;
int n,m;
struct node{
int a,b;
node(int x=0,int y=0){a=x,b=y;}
bool operator <(const node x)const {
return a<x.a;
}
}e[maxn];
int sum[maxn];
int getx(int x){
int j=lower_bound(e+1,e+1+m,node(e[x].b,0))-e;
if(m-j+1>=n)return sum[m-n+1];
ll res=n-(m-j+1);
if(j>x){
return sum[j]+e[x].a+(res-1)*e[x].b;
}else{
return sum[j]+res*e[x].b;
}
}
void solve() {
cin>>n>>m;
for(int i=1;i<=m;++i){
cin>>e[i].a>>e[i].b;
}
sort(e+1,e+1+m);
sum[m+1]=0;
for(int i=m;i>=1;--i){
sum[i]=sum[i+1]+e[i].a;
}
int ans=0;
for(int i=1;i<=m;++i){
ans=max(ans,getx(i));
}
cout<<ans<<endl;
}
signed main() {
int __;
cin >> __;
// scanf("%d",&__);
while(__--) {
solve();
}
}
C. Choosing flowers
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.