题目:P1208 [USACO1.3] 混合牛奶 Mixing Milk
请先读题!!!
以下是代码:
#include<bits/stdc++.h>
using namespace std;
long long n,m;
struct S{
int x,y;
}a[2000002];
int cmp(S a,S b){
return a.x<b.x;
}
int main(){
cin>>m>>n;
for(int i=0;i<n;i++)cin>>a[i].x>>a[i].y;
sort(a,a+n,cmp);
int sum=0;
for(int i=0;i<n;i++){
if(m-a[i].y>=0){
m-=a[i].y;
sum+=a[i].x*a[i].y;
}
else{
sum+=a[i].x*m;
break;
}
}
cout<<sum;
return 0;
}
讲解在评论区,如有问题私信作者!!!