#include <bits/stdc++.h>
using namespace std;
const int MX = 305;
const int INF = 1e9 + 7;
int N, h[MX], memo[MX][MX][MX];
int dp(int i, int p1, int p2){
if(i == N){
return min(p1, p2) * 2 + max(p1, p2) * 3;
return 0;
}
if(memo[i][p1][p2] != -1) return memo[i][p1][p2];
if(p2 == 0) return dp(i + 1, h[i], p1);
int mn = INF;
if(p1 > 0 && p2 > 0) mn = min(mn, dp(i, p1 - 1, p2 - 1) + 5);
int tot = 0;
int rem3 = min(h[i], min(p1, p2));
int currh = h[i] - rem3, currp1 = p1 - rem3, currp2 = p2 - rem3;
tot += 7 * rem3;
int rem2 = min(currp1, currp2);
currp1 -= rem2;
currp2 -= rem2;
tot += 5 * rem2;
tot += 3 * currp2;
mn = min(mn, dp(i + 1, currh, currp1) + tot);
return memo[i][p1][p2] = mn;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
for(int i = 0; i < N; i++) cin >> h[i];
if(N == 1){
cout << h[0] * 3 << '\n';
return 0;
}else if(N == 2){
cout << min(h[0], h[1]) * 5 + (max(h[0], h[1]) - min(h[0], h[1])) * 3 << '\n';
return 0;
}
memset(memo, -1, sizeof memo);
cout << dp(2, h[1], h[0]) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
35 ms |
111248 KB |
Output is correct |
4 |
Correct |
35 ms |
111292 KB |
Output is correct |
5 |
Correct |
36 ms |
111284 KB |
Output is correct |
6 |
Correct |
37 ms |
111288 KB |
Output is correct |
7 |
Correct |
38 ms |
111312 KB |
Output is correct |
8 |
Correct |
37 ms |
111308 KB |
Output is correct |
9 |
Correct |
38 ms |
111272 KB |
Output is correct |
10 |
Correct |
42 ms |
111316 KB |
Output is correct |
11 |
Correct |
41 ms |
111756 KB |
Output is correct |
12 |
Correct |
38 ms |
111444 KB |
Output is correct |
13 |
Correct |
41 ms |
111712 KB |
Output is correct |
14 |
Correct |
45 ms |
111924 KB |
Output is correct |
15 |
Correct |
85 ms |
113092 KB |
Output is correct |
16 |
Correct |
96 ms |
112972 KB |
Output is correct |
17 |
Correct |
111 ms |
113620 KB |
Output is correct |
18 |
Correct |
104 ms |
113616 KB |
Output is correct |
19 |
Correct |
100 ms |
113604 KB |
Output is correct |
20 |
Correct |
106 ms |
113612 KB |
Output is correct |