Codeforces Beta Round #16 (Div. 2 Only) - B
题目翻译
一个窃贼进入火柴仓库,想偷尽可能多的火柴。仓库中有 m 个容器,第 i 个容器中有 ai 个火柴盒,每个火柴盒包含 bi 根火柴。所有的火柴盒大小都一样。窃贼的背包能准确地装下 n 个火柴盒。你的任务是找出窃贼能带走的火柴的最大数量。他没有时间重新排列火柴盒中的火柴,这就是为什么他只选择不超过n个火柴盒,这样火柴盒中的火柴总数就最大了。
输入
输入的第一行包含整数n(1 ≤ n ≤ 2*1e8)和整数m(1 ≤ m ≤ 20). 第 i+1 行包含一对数字 ai 和 bi(1 ≤ ai ≤ 1e8, 1≤ bi ≤ 10). 所有输入的数字都是整数。、
输出
输出问题的唯一数字 - 答案。
样例
输入
7 3
5 10
2 5
3 6
输出
62
解题思路
贪心模拟就好了,先排个序,肯定火柴盒里面火柴最多的先拿,才会使答案最优。
代码示例
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=202010;
int n,m;
int ans;
struct match{
int a;
int b;
}f[maxn];
bool cmp(const match &x,const match &y){return x.b>y.b;}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++) cin>>f[i].a>>f[i].b;
sort(f+1,f+1+m,cmp);
int cnt=0;
for(int i=1;i<=m;i++){
while(f[i].a){
if(cnt<n){
ans+=f[i].b;
f[i].a--;
cnt++;
}
else break;
}
}
cout<<ans<<endl;
return 0;
}