题目如下:
代码如下:
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; struct kk{ int x,y; }a[5000]; bool cmp(kk c,kk d){ return c.x<d.x; } int main(){ int n,m; scanf("%d%d",&n,&m) ; for(int i=0;i<m;i++) scanf("%d%d",&a[i].x,&a[i].y); sort(a,a+m,cmp); int sum=0; int j=0; while(n!=0){ if(a[j].y!=0){ //这个地方 我把if改成while 并且去掉 下面的 else 死活不出答案 奇怪 n--; sum+=a[j].x; a[j].y--; } else j++;//就是去掉这个else } printf("%d",sum); return 0; }
贪心法!