Problem Statement
Takahashi, who works for a pizza restaurant, is making a delicious cheese pizza for staff meals.
There are NN kinds of cheese in front of him.
The deliciousness of the ii-th kind of cheese is A_iAi per gram, and B_iBi grams of this cheese are available.
The deliciousness of the pizza will be the total deliciousness of cheese he puts on top of the pizza.
However, using too much cheese would make his boss angry, so the pizza can have at most WW grams of cheese on top of it.
Under this condition, find the maximum possible deliciousness of the pizza.Constraints
- All values in input are integers.
- 1 \le N \le 3 \times 10^51≤N≤3×105
- 1 \le W \le 3 \times 10^81≤W≤3×108
- 1 \le A_i \le 10^91≤Ai≤109
- 1 \le B_i \le 10001≤Bi≤1000
Input
Input is given from Standard Input in the following format:
NN WW A_1A1 B_1B1 A_2A2 B_2B2 \vdots⋮ A_NAN B_NBNOutput
Print the answer as an integer.
Sample Input 1 Copy
Copy
3 5 3 1 4 2 2 3Sample Output 1 Copy
Copy
15The optimal choice is to use 11 gram of cheese of the first kind, 22 grams of the second kind, and 22 grams of the third kind.
The pizza will have a deliciousness of 1515.
Sample Input 2 Copy
Copy
4 100 6 2 1 5 3 9 8 7Sample Output 2 Copy
Copy
100There may be less than WW grams of cheese in total.
Sample Input 3 Copy
Copy
10 3141 314944731 649 140276783 228 578012421 809 878510647 519 925326537 943 337666726 611 879137070 306 87808915 39 756059990 244 228622672 291Sample Output 3 Copy
Copy
2357689932073
题目类型:贪心
题目目标:寻找Wgram内N种芝士可以组合出最大的美味度
注意点:注意一下数据范围,3+5+9 = 17, 10e17 ,long long
解题思路:1)一开始认为是容量为1的多重背包,然后RE了
2)后来发现有更简单的方法;
1)每种芝士只有数量和美味度两种性质,pair就可以了。读入。
2)排序,自己写cmp也可以,sort+reverse()也可以。
3)在容量允许的情况下,把当前美味度最大的芝士全放入,若容量不够,把剩余的容量都用来放当前美味度最大的芝士。容量为0,跳出输出。
AC代码:
#include <bits/stdc++.h>
#define rep(x, a, b) for(int x=a; x<=b; x++)
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int N= 3e5+10;
int n, w;
bool cmp(pair< ll, ll > a, pair< ll, ll > b)
{
return a.first > b.first;
}
int main()
{
ll ans = 0;
scanf("%d%d", &n,&w);
vector< pair< ll, ll > > v(n);
rep(i, 0, n-1)
cin>>v[i].first>>v[i].second;
sort(v.begin(), v.end(), cmp);
rep(i, 0, n-1)
{
if(!w) break;
if(w - v[i].second >= 0)
{
w -= v[i].second;
ans += v[i].first*v[i].second;
}
else if(w < v[i].second){
ans += v[i].first*w;
w = 0;
}
}
cout<<ans;
return 0;
}