USACO Mixing Milk

Code

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
/*
ID: wcr19961
PROG: milk
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 5010;
int tmp[maxn];
struct Node {
int num, v;
bool operator < (const Node& rhs) const {
return v < rhs.v;
}
} a[maxn];

int main() {
freopen("milk.in", "r", stdin);
freopen("milk.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
scanf("%d%d", &a[i].v, &a[i].num);
}
sort(a, a + m);
LL sum = 0;
for (int i = 0; i < m; ++i) {
if (n >= 0) {
if (n > a[i].num) {
sum += a[i].v * a[i].num;
n -= a[i].num;
} else {
sum += a[i].v * n;
n = 0;
}
} else {
break;
}
}
printf("%lld\n", sum);
return 0;
}