###### Section 1.3 Mixing Milk

/*
ID: alexyua2
PROG: milk
LANG: C++
*/

#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;

ifstream fin("milk.in");
ofstream fout("milk.out");

//ifstream fin("in.txt");
//ofstream fout("out.txt");

struct Farmer
{
int price;
int amount;
};

bool mycmp(Farmer,Farmer);
int main()
{
//input
int N,M;
fin>>N >>M;
int i;
vector<Farmer>farmers;
Farmer temp;
for(i=0;i<M;i++)
{
fin>>temp.price >>temp.amount;
farmers.push_back(temp);
}
//sort farmers by the price
sort(farmers.begin(),farmers.end(),mycmp);
//greedy algorithm
int cost = 0;
for(i=0;i<M;i++)
{
if(N >= farmers[i].amount)
{
N -= farmers[i].amount;
cost += farmers[i].amount * farmers[i].price;
}
else
{
cost += N * farmers[i].price;
break;
}
}
fout<<cost <<endl;
//close files
fin.close();
fout.close();
return 0;
}

bool mycmp(Farmer a,Farmer b)
{
return a.price<b.price;
}

#### 1.3.1 Mixing Milk 混合牛奶

2014-12-03 13:39:03

#### USACO Section 1.3 Mixing Milk

2013-11-25 21:46:08

#### USCAO section 1.3 Mixing Milk

2012-07-10 17:17:35

#### USACO 1.3 Mixing Milk（贪心）

2014-11-25 19:36:55

#### Mixing Milk USACO

2013-04-18 13:04:26

#### usaco Mixing Milk 解题报告

2012-12-07 17:12:05

#### P1208 [USACO1.3]混合牛奶 Mixing Milk

2016-10-22 17:02:11

#### S 1.3 milk C程序

2012-11-15 13:09:22

#### USACO 1.3 Mixing Milk (milk)

2014-02-19 13:57:18

#### USACO section 1.3 Mixing Milk[贪心算法]

2017-05-26 16:37:37

## 不良信息举报

Section 1.3 Mixing Milk