hdu 2111 Saving HDU(贪心)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 using namespace std;
5 struct node
6 {
7 int p,m;
8 }arr[110];
9 int cmp(node x,node y)
10 {
11 return x.p>y.p;
12 }
13 int main()
14 {
15 int v,n;
16 while(scanf("%d",&v)!=EOF)
17 {
18 if(v==0)
19 break;
20 scanf("%d",&n);
21 for(int i=1;i<=n;i++)
22 scanf("%d %d",&arr[i].p,&arr[i].m);
23 sort(arr+1,arr+1+n,cmp);
24 int ans=0;
25 for(int i=1;i<=n;i++)
26 {
27 if(v!=0)
28 {
29 if(v>=arr[i].m)
30 {
31 ans+=arr[i].m*arr[i].p;
32 v-=arr[i].m;
33 }
34 else
35 {
36 ans+=v*arr[i].p;
37 v=0;
38 }
39
40 }
41 else
42 break;
43 }
44 printf("%d\n",ans);
45 }
46 system("pause");
47 return 0;
48
2 #include<cstdio>
3 #include<algorithm>
4 using namespace std;
5 struct node
6 {
7 int p,m;
8 }arr[110];
9 int cmp(node x,node y)
10 {
11 return x.p>y.p;
12 }
13 int main()
14 {
15 int v,n;
16 while(scanf("%d",&v)!=EOF)
17 {
18 if(v==0)
19 break;
20 scanf("%d",&n);
21 for(int i=1;i<=n;i++)
22 scanf("%d %d",&arr[i].p,&arr[i].m);
23 sort(arr+1,arr+1+n,cmp);
24 int ans=0;
25 for(int i=1;i<=n;i++)
26 {
27 if(v!=0)
28 {
29 if(v>=arr[i].m)
30 {
31 ans+=arr[i].m*arr[i].p;
32 v-=arr[i].m;
33 }
34 else
35 {
36 ans+=v*arr[i].p;
37 v=0;
38 }
39
40 }
41 else
42 break;
43 }
44 printf("%d\n",ans);
45 }
46 system("pause");
47 return 0;
48