#include<bits/stdc++.h>
using namespace std;
int dp[305][205][205];
int n;
int a[305];
int op3(int i, int &n1, int &n2) {
int x = n1, y = n2, z = a[i+2];
int ret = 0;
int take = min({x, y, z});
ret = take * 7;
x -= take;
y -= take;
z -= take;
if(x) {
if(y) {
take = min(x, y);
ret += take * 5;
x -= take;
y -= take;
if(x) {
ret += x * 3;
}
} else {
ret += x * 3;
}
}
n1 = y, n2 = z;
return ret;
}
int solve(int i, int n1, int n2) {
if(i>n) return 0;
int &ret = dp[i][n1][n2];
if(ret!=-1) return ret;
if(!n1) return ret = solve(i+1, n2, a[i+2]);
if(n1) ret = solve(i, n1-1, n2) + 3;
if(n1 && n2) ret = solve(i, n1-1, n2-1) + 5;
int c = op3(i, n1, n2);
ret = min(ret, c+solve(i+1, n1, n2));
return ret;
}
void PlayGround() {
cin>>n;
for(int i=1; i<=n; ++i) {
cin>>a[i];
}
memset(dp, -1, sizeof dp);
cout<<solve(1, a[1], a[2])<<'\n';
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
50396 KB |
Output is correct |
2 |
Correct |
21 ms |
50440 KB |
Output is correct |
3 |
Correct |
23 ms |
50384 KB |
Output is correct |
4 |
Correct |
22 ms |
50464 KB |
Output is correct |
5 |
Correct |
23 ms |
50464 KB |
Output is correct |
6 |
Correct |
20 ms |
50388 KB |
Output is correct |
7 |
Correct |
21 ms |
50388 KB |
Output is correct |
8 |
Correct |
24 ms |
50456 KB |
Output is correct |
9 |
Correct |
25 ms |
50464 KB |
Output is correct |
10 |
Correct |
26 ms |
50648 KB |
Output is correct |
11 |
Correct |
36 ms |
51660 KB |
Output is correct |
12 |
Correct |
30 ms |
50720 KB |
Output is correct |
13 |
Correct |
39 ms |
52004 KB |
Output is correct |
14 |
Correct |
63 ms |
52456 KB |
Output is correct |
15 |
Correct |
155 ms |
54868 KB |
Output is correct |
16 |
Correct |
135 ms |
54548 KB |
Output is correct |
17 |
Correct |
153 ms |
56152 KB |
Output is correct |
18 |
Correct |
157 ms |
56084 KB |
Output is correct |
19 |
Correct |
141 ms |
55636 KB |
Output is correct |
20 |
Correct |
154 ms |
56108 KB |
Output is correct |