-
Notifications
You must be signed in to change notification settings - Fork 0
/
milk.cpp
59 lines (50 loc) · 1.21 KB
/
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
52
53
54
55
56
57
58
59
/*
ID: bbsunch2
PROG: milk
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <map>
using namespace std;
int main()
{
ofstream fout ("milk.out");
ifstream fin ("milk.in");
map<int, int> price_farmerAmount;
int totalAmount = 0;
int farmerNum = 0;
int totalPrice = 0;
fin >> totalAmount >> farmerNum;
for(int i = 0; i < farmerNum; i++)
{
int price;
int farmerAmount;
fin >> price >> farmerAmount;
pair<map<int,int>::iterator,bool> ret = price_farmerAmount.insert(pair<int, int>(price, farmerAmount));
if(!ret.second)
{
ret.first->second += farmerAmount;
}
}
map<int, int>::iterator it;
for( it = price_farmerAmount.begin(); it != price_farmerAmount.end(); it++)
{
int price = it->first;
int farmerAmount = it->second;
if(totalAmount > farmerAmount)
{
totalPrice = totalPrice + price * farmerAmount;
totalAmount -= farmerAmount;
}else
{
totalPrice = totalPrice + price * totalAmount;
break;
}
}
fout << totalPrice << endl;
return 0;
}