1 3 15 5 10 2 8 3 9
65
#include<stdio.h> #include<stdlib.h> #include<algorithm> using namespace std; struct record //结构体 { int jia; int zhong; }num[11]; bool cmp(record a,record b) { return a.jia>b.jia; //按价钱从大到小排序 } int main() { int n,m,j,i,t,sum,k; scanf("%d",&n); while(n--) { sum=0; scanf("%d %d",&m,&j); for(i=0;i<m;i++) { scanf("%d %d",&num[i].jia,&num[i].zhong); } sort(num,num+m,cmp); for(i=0;i<m&&j>0;i++) { j-=num[i].zhong; if(j<0) { sum=sum+(j+num[i].zhong)*num[i].jia; //超出的重量不参加计算 break; } sum+=num[i].jia*num[i].zhong; //sum+=min(j,num[i].zhong)*num[i].jia; } printf("%d\n",sum); } return 0; }
原文:http://www.cnblogs.com/tonghao/p/4358323.html