NEC Programming Contest 2021(AtCoder Beginner Contest 229) C - Cheese

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300 points

Problem Statement
Takahashi, who works for a pizza restaurant, is making a delicious cheese pizza for staff meals.
There are N kinds of cheese in front of him.
The deliciousness of the i-th kind of cheese is Ai per gram, and Bi 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 W 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≤N≤3×10^5
1≤W≤3×10^8
1≤Ai≤10^9

1≤Bi≤1000
Input
Input is given from Standard Input in the following format:

N W
A1 B1
A2 B2
​⋮
AN BN

Output
Print the answer as an integer.

Sample Input 1
3 5
3 1
4 2
2 3
Sample Output 1
15

The optimal choice is to use 1 gram of cheese of the first kind, 2 grams of the second kind, and 2 grams of the third kind.
The pizza will have a deliciousness of 15.

Sample Input 2
4 100
6 2
1 5
3 9
8 7
Sample Output 2
100
There may be less than W grams of cheese in total.

Sample Input 3
10 3141
314944731 649
140276783 228
578012421 809
878510647 519
925326537 943
337666726 611
879137070 306
87808915 39
756059990 244
228622672 291
Sample Output 3
2357689932073

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=4e5+7;
ll a[maxn],b[maxn],w;
struct node
{
	ll a,b;
}aa[maxn];
bool cmp(node m,node n)
{
	return m.a>n.a;
}
int main()
{
	ll i,n;
	long long ans=0;
	scanf("%ld%ld",&n,&w);
	for(i=1;i<=n;i++)
		scanf("%ld%ld",&aa[i].a,&aa[i].b);
	sort(aa+1,aa+n+1,cmp);
	for(i=1;i<=n;i++)
	{
		if(w>=aa[i].b){
			ans+=aa[i].a*aa[i].b;
			w-=aa[i].b;
			continue;
		}	
		if(w<aa[i].b&&w>0)
			ans+=aa[i].a*w,w=0;
		if(w==0){
			printf("%lld",ans);
			return 0;
		}	
	}
	printf("%lld",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

耗子煨汁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值