#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=300+5;
const int inf=1e9;
const int mxv = 210;
int n, h[mxn];
int dp[mxn][mxv][mxv];
int gp[mxn][mxv][mxv];
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> h[i];
}
if(n == 1) {
cout<<h[1] * 3<<endl;
return 0;
}
for(int i = 0; i <= n; i++) for(int j = 0; j <= mxv; j++) for(int k = 0; k <= mxv; k++) {
dp[i][j][k] = inf;
gp[i][j][k] = inf;
}
for(int i = 0; i <= min(h[1], h[2]); i++) {
dp[2][i][0] = 5 * i + 3 * (h[2] - i) + 3 * (h[1] - i);
gp[2][i][h[2] - i] = min(gp[2][i][h[2] - i], dp[2][i][0]);
}
for(int i = h[2]; i >= 0; i--) {
for(int j = h[2]; j >= 0; j--) {
gp[2][i][j] = min({gp[2][i][j], gp[2][i + 1][j], gp[2][i][j + 1]});
}
}
for(int p = 3; p <= n; p++) {
for(int i = 0; i <= min(h[p], h[p - 1]); i++) {
for(int j = 0; j <= min(h[p] - i, h[p - 1] - i); j++) {
dp[p][i][j] = gp[p - 1][j][i] + 2 * (i + j) + 3 * (h[p] - i - j);
gp[p][i][h[p] - i - j] = min(gp[p][i][h[p] - i - j], dp[p][i][j]);
}
}
for(int i = h[p]; i >= 0; i--) {
for(int j = h[p]; j >= 0; j--) {
gp[p][i][j] = min({gp[p][i][j], gp[p][i + 1][j], gp[p][i][j + 1]});
}
}
}
cout<<gp[n][0][0]<<endl;
}
Compilation message
skyline.cpp: In function 'int main()':
skyline.cpp:23:15: warning: iteration 210 invokes undefined behavior [-Waggressive-loop-optimizations]
23 | dp[i][j][k] = inf;
| ~~~~~~~~~~~~^~~~~
skyline.cpp:22:77: note: within this loop
22 | for(int i = 0; i <= n; i++) for(int j = 0; j <= mxv; j++) for(int k = 0; k <= mxv; k++) {
| ~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
1328 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
17 ms |
24708 KB |
Output is correct |
5 |
Correct |
2 ms |
2736 KB |
Output is correct |
6 |
Correct |
2 ms |
4016 KB |
Output is correct |
7 |
Correct |
2 ms |
3668 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
7 ms |
8956 KB |
Output is correct |
11 |
Correct |
34 ms |
49332 KB |
Output is correct |
12 |
Correct |
10 ms |
10964 KB |
Output is correct |
13 |
Correct |
38 ms |
57928 KB |
Output is correct |
14 |
Correct |
54 ms |
69320 KB |
Output is correct |
15 |
Correct |
99 ms |
95184 KB |
Output is correct |
16 |
Correct |
87 ms |
85464 KB |
Output is correct |
17 |
Correct |
132 ms |
104104 KB |
Output is correct |
18 |
Correct |
127 ms |
103492 KB |
Output is correct |
19 |
Correct |
122 ms |
99720 KB |
Output is correct |
20 |
Correct |
130 ms |
104208 KB |
Output is correct |