#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
//~ #define int int64_t
const int N = 5005;
const int inf = 1e9;
int dp[N][N], old[N][N], a[N];
int l[N], r[N], back[N][N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=0;i<n;i++){
l[i] = n;
cin>>a[i];
if(a[i] == -1) l[i] = 0;
else if(i) l[i] = l[i-1] + 1;
}
for(int i=n-1;~i;i--){
r[i] = n;
if(a[i] == -1) r[i] = 0;
else if(i + 1 < n) r[i] = r[i+1] + 1;
}
memset(dp, 63, sizeof dp);
memset(back, 63, sizeof back);
dp[0][0] = 0;
int res = 0;
for(int i=0;i<=n;i++){
multiset<int> ss;
for(int x=0;x<=n;x++){
if(a[i] > 0){
old[i][x] = dp[i][x] - x * 2 * min(l[i], r[i]);
if(x){
int tmp = inf;
if(*ss.begin() + (x - 1) * 2 * min(l[i], r[i]) <= 2 * i){
dp[i][x] = min(dp[i][x], *ss.begin() + x * 2 * min(l[i], r[i]));
tmp = *ss.begin() + (x - 1) * 2 * min(l[i], r[i]);
}
if(i + r[i] < N){
back[i + r[i]][x] = min(back[i + r[i]][x], tmp + r[i]);
}
}
ss.insert(old[i][x]);
if(x >= a[i]){
ss.erase(ss.find(old[i][x - a[i]]));
}
}
if(i + r[i] < N) dp[i][x] = min(dp[i][x], back[i + r[i]][x] + r[i]);
dp[i+1][x] = min(dp[i+1][x], dp[i][x] + 1);
if(dp[i][x] < inf){
//~ cout<<i<<" "<<x<<" "<<dp[i][x]<<endl;;
res = max(res, x);
if(dp[i][x] < 2 * i) res = max(res, x + 1);
}
}
}
int tot = 0;
for(int i=0;i<n;i++){
if(~a[i]) tot += a[i];
}
cout<<tot - res<<"\n";
}
/*
7
4 2 0 2 5 2 -1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
77 ms |
196428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
77 ms |
196428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
77 ms |
196428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
77 ms |
196428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
77 ms |
196428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |