#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
signed main(){
int n; cin>>n;
vector<int> v(n); for(int& i : v) cin>>i;
vector<int> dp(n+1,inf), dp1(n+1,inf), mx(n+1,0), mx1(n+1,0);
dp[0] = 0;
mx[0] = 0;
vector<int> last(n+1,0), last1(n+1,0); // just add max(between act and last)
for(int i = 1; i<=n; i++){
// cerr<<i<<" "<<v[i-1]<<" "<<dp[i-1]<<" "<<mx[i-1]<<endl;
if(v[i-1] > mx[i-1]){
mx[i] = v[i-1];
dp[i] = dp[i-1]; continue;
}
int obj = mx[i-1] + 1;
mx[i] = obj;
dp[i] = dp[i-1] + abs(obj - v[i-1]);
}
dp1[n] = 0;
mx1[n] = 0;
for(int i = n-1; i>=0; i--){
// cerr<<"dp1 "<<i<<" "<<v[i]<<" "<<dp1[i+1]<<" "<<mx1[i+1]<<" ";
if(v[i] > mx1[i+1]){
// cerr<<"not"<<endl;
mx1[i] = v[i];
dp1[i] = dp1[i+1];
continue;
}
// cerr<<"yes ";
int obj = mx1[i+1] + 1;
// cerr<<obj<<" "<<v[i]<<endl;
dp1[i] = dp1[i+1] + abs(obj - v[i]);
mx1[i] = obj;
}
// mx1[n] = v[n-1]; mx[0] = v[0];
// for(int i : mx1) cout<<i<<" "; cout<<endl;
int ans = min(dp[n],dp1[0]);
// cerr<<dp[n]<<" "<<dp1[0]<<endl;
for(int i = 1; i<n; i++){
// cerr<<"HERE "<<i<<" "<<dp[i]<<" "<<dp1[i+1]<<" "<<mx1[i+1]<<" "<<mx[i]<<endl;
// if(mx[i] == mx1[i]) continue;
int sortleft = dp[i];
int sortright = dp1[i+1];
int shouldbe = max(mx[i], mx1[i+1])+1;
int have = v[i];
int sobren = abs(dp[i] - dp1[i+1]);
ans = min(ans, max(sortleft,sortright) + max(0ll,(shouldbe - have) - sobren));
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
412 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
412 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |