#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, ans = INF;
cin >> n;
int a[n], lt[n], rt[n], dp[2*n+1][n], pref[n];
for(int i = 0; i < n; i++) cin >> a[i];
lt[0] = a[0]==-1?0:INF;
for(int i = 1; i < n; i++) {
lt[i] = lt[i-1];
if(a[i] == -1) lt[i] = i;
}
rt[n-1] = a[n-1]==-1?n-1:INF;
for(int i = n-2; i >= 0; i--) {
rt[i] = rt[i+1];
if(a[i] == -1) rt[i] = i;
}
pref[0] = max(a[0], 0LL);
for(int i = 1; i < n; i++) pref[i] = pref[i-1]+max(a[i], 0LL);
for(int i = 0; i <= 2*n; i++) {
for(int j = 0; j < n; j++) {
dp[i][j] = INF;
}
}
for(int i = 0; i <= 2*n; i++) {
for(int j = 0; j < n; j++) {
if(a[j] == -1) continue;
if(i < j) continue;
dp[i][j] = min(dp[i][j], pref[j]-1);
int sm = 0;
for(int k = j; k < n; k++) {
if(k > j) sm += max(a[k], 0LL);
if(a[k] == -1) continue;
int dist = min(abs(j-lt[j])+abs(k-lt[j]), abs(rt[j]-j)+abs(rt[j]-k));
if(dist+i > 2*n) continue;
if(dist+i > 2*k) continue;
if(k == j) dp[dist+i][k] = min(dp[dist+i][k], max(dp[i][j]-1, 0LL));
else dp[dist+i][k] = min(dp[dist+i][k], dp[i][j]+max(sm-1, 0LL));
}
ans = min(ans, dp[i][j]+pref[n-1]-pref[j]);
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |