-
Notifications
You must be signed in to change notification settings - Fork 6
/
coke.cpp
60 lines (47 loc) · 1.82 KB
/
coke.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
#include <bits/stdc++.h>
using namespace std;
int C[151][151][51] {{{0}}};
int dp(int N, int n5, int n10, int total) {
for (int n = 1; n <= N; n++) {
for (int i = 0; i <= n5 + n10; i++) {
for (int j = 0; j <= n10; j++) {
int mc = INT_MAX;
// n1 from input can be recovered here
int n1 = total - 5*i - 10*j;
// Can I buy a coke with 8 1c coins?
if (n1 >= 8)
mc = min(mc, C[n-1][i][j] + 8);
// Can I buy a coke with 3 1c and 1 5c coins?
if (i >= 1 && n1 >= 3)
mc = min(mc, C[n-1][i-1][j] + 4);
// Can I buy a coke with 2 5c coins?
if (i >= 2)
mc = min(mc, C[n-1][i-2][j] + 2);
// Can I buy a coke with 1 10c coin?
if (j >= 1)
mc = min(mc, C[n-1][i][j-1] + 1);
// Can I buy 2 cokes with (1+1+1+10 -> 1coke+5 and 5+1+1+1-> 1coke)?
// Compare this with 8 1c -> 1coke
if (j >= 1 && n >= 2 && C[n-1][i][j-1] - C[n-2][i][j-1] == 8)
mc = min(mc, C[n-1][i][j-1]);
// End of loop
if (n == N && i == n5 && j == n10)
return mc;
// DP!
C[n][i][j] = mc;
}
}
}
return C[N][n5][n10];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q, b, n1, n5, n10;
cin >> q;
while (q--) {
cin >> b >> n1 >> n5 >> n10;
cout << dp(b, n5, n10, n1 + 5*n5 + 10*n10) << '\n';
}
return 0;
}