# 1.3.1 Mixing Milk 混合牛奶

1.3.1 Mixing Milk 混合牛奶

``````100 5
5 20
9 40
3 10
8 80
6 30``````

``````630
``````

``````#include<stdio.h>
#include<stdlib.h>
#define MAX 5005
typedef struct{
int p,a;
}milk;
int cmp(const void *a,const void *b)
{
return (*(milk*)a).p-(*(milk*)b).p;
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)==2){
milk p[MAX];
int i,j,sum=0;
for(i=1;i<=m;i++)
scanf("%d %d",&p[i].p,&p[i].a);
qsort(&p[1],m,sizeof(milk),cmp);
i=1;
while(n&&i<=m){
if(n>=p[i].a){
sum+=p[i].a*p[i].p;
n-=p[i].a;
}
else{
sum+=p[i].p*n;
n-=n;
}
i++;
}
printf("%d\n",sum);
}
return 0;
}``````

``````#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
struct ab
{
int money;
long int num;
}d[5000];
bool cmp(ab a, ab b)
{
return a.money<b.money;
}
int main()
{
long int s;
int n;
cin>>s>>n;
for(int i=0;i<n;i++)
cin>>d[i].money>>d[i].num;
sort(d,d+n,cmp);
long int k=0;
for(int i=0;i<n;i++)
{
if(d[i].num>s)
{
k+=s*d[i].money;
s=0;
}
else k+=d[i].money*d[i].num,s-=d[i].num;
if(s==0)break;
}
cout<<k<<endl;
return 0;
}
``````