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
| #include<cstdio> #include<stack> #define N 30
struct Coin { int d, v; }coin[N];
int main() { bool first = true; int t, w; while (scanf("%d%d", &t, &w) != EOF) { if (first) first = false; else putchar('\n');
int n, i, j; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d%d", &coin[i].d, &coin[i].v);
int dp[1001] = {}; bool get[N][1001] = {}; for (i = 0; i < n; i++) { int need = coin[i].d * 3 * w; for (j = t; j >= need; j--) if (dp[j] < dp[j - need] + coin[i].v) { dp[j] = dp[j - need] + coin[i].v; get[i][j] = true; } }
std::stack<int> s; for (i = n - 1, j = t; i >= 0; i--) if (get[i][j]) { s.push(i); j -= coin[i].d * 3 * w; }
printf("%d\n%d\n", dp[t], s.size()); while (!s.empty()) { i = s.top(), s.pop(); printf("%d %d\n", coin[i].d, coin[i].v); } }
return 0; }
|