-
Notifications
You must be signed in to change notification settings - Fork 0
/
milk.cpp
51 lines (43 loc) · 916 Bytes
/
milk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
ID: cjdyx202
PROG: milk
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <utility> //for pair
#include <algorithm> //for sort
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b){
return (a.first<b.first);
}
int main(){
freopen("milk.in","r",stdin);
freopen("milk.out","w",stdout);
int n,m; //n : the amount of milk, m : number of farmers
cin>>n>>m;
vector<pair<int, int> > pa;
int p, a;
for(int i = 0; i < m; i++){
cin>>p>>a;
pa.push_back(make_pair(p,a));
}
sort(pa.begin(), pa.end(), cmp);
int cost = 0;
int left = n; //the amount of milk left
int i = 0;
while(left){
if(pa[i].second <= left){
cost += pa[i].first * pa[i].second;
left -= pa[i].second;
i++;
}
else{
cost += pa[i].first * left;
left = 0;
}
}
cout<<cost<<endl;
return 0;
}