#include <iostream>
#include <cstring>
using namespace std;
/*
dp[i][carry][carry, pass forward] = min cost for range [i...n]
*/
const int INF = 1e9;
int n, h[305], dp[305][205][205];
int brutus(int i, int j, int k) {
if (i > n + 1) return 0;
if (dp[i][j][k] != -1) return dp[i][j][k];
if (j == 0) dp[i][j][k] = brutus(i + 1, k, h[i + 1]);
else {
dp[i][j][k] = brutus(i, j - 1, k) + 3;
if (min(j, k) > 0) dp[i][j][k] = min(dp[i][j][k], brutus(i, j - 1, k - 1) + 5);
if (j <= k and j <= h[i + 1]) dp[i][j][k] = min(dp[i][j][k], brutus(i + 1, k - j, h[i + 1] - j) + 7 * j);
}
return dp[i][j][k];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(dp, -1, sizeof(dp));
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
h[0] = 0, h[n + 1] = 0;
cout << brutus(1, h[0], h[1]) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
50388 KB |
Output is correct |
2 |
Correct |
20 ms |
50388 KB |
Output is correct |
3 |
Correct |
19 ms |
50388 KB |
Output is correct |
4 |
Correct |
20 ms |
50516 KB |
Output is correct |
5 |
Correct |
20 ms |
50476 KB |
Output is correct |
6 |
Correct |
21 ms |
50388 KB |
Output is correct |
7 |
Correct |
20 ms |
50484 KB |
Output is correct |
8 |
Correct |
21 ms |
50416 KB |
Output is correct |
9 |
Correct |
23 ms |
50540 KB |
Output is correct |
10 |
Correct |
25 ms |
50656 KB |
Output is correct |
11 |
Correct |
40 ms |
51876 KB |
Output is correct |
12 |
Correct |
24 ms |
50744 KB |
Output is correct |
13 |
Correct |
35 ms |
51924 KB |
Output is correct |
14 |
Correct |
43 ms |
52496 KB |
Output is correct |
15 |
Correct |
108 ms |
54876 KB |
Output is correct |
16 |
Correct |
103 ms |
54548 KB |
Output is correct |
17 |
Correct |
152 ms |
56128 KB |
Output is correct |
18 |
Correct |
179 ms |
55964 KB |
Output is correct |
19 |
Correct |
150 ms |
55600 KB |
Output is correct |
20 |
Correct |
151 ms |
56104 KB |
Output is correct |