-
Notifications
You must be signed in to change notification settings - Fork 0
/
B.cpp
65 lines (54 loc) · 1.35 KB
/
B.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
60
61
62
63
64
65
/*
B - XHD来寻宝
*/
#include <bits/stdc++.h>
using namespace std;
// #define DEBUG
using Pair = pair<int, int>;
using Item = vector<Pair>;
struct {
bool operator()( const Pair &a, const Pair &b ) const {
return a.first < b.first;
}
} compare_first;
int main() {
int v; // capacity of pocket
while ( scanf( "%d", &v ) != EOF && v != 0 ) {
int n; // kinds of treasure
scanf( "%d", &n );
Item item( n );
for ( int i = 0; i < n; ++i )
scanf( "%d%d", &item[ i ].first,
&item[ i ].second ); // first-price, second-volume
std::sort( item.rbegin(), item.rend(),
compare_first ); // sort by item price
#ifdef DEBUG
printf( "\n" );
for ( int i = 0; i < n; ++i )
printf( "%d %d\n", item[ i ].first, item[ i ].second );
printf( "\n" );
#endif
int total_value = 0;
for ( int i = 0; v > 0 && i < n; ++i ) {
int w = ( v < item[ i ].second )
? v
: item[ i ].second; // the volume can take
total_value += item[ i ].first * w;
v -= w;
#ifdef DEBUG
printf( "[%d] %d %d | %d\n", i, w, v, total_value );
#endif
}
printf( "%d\n", total_value );
}
return 0;
}
/*
Sample Input
2 2
3 1
2 3
0
Sample Output
5
*/