This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N;
int arr[200005];
/*
int mnpre[200005];
long long sumpre[200005];
int mnsuf[200005];
long long sumsuf[200005];
*/
int mxpre[200005];
int mxsuf[200005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for(int i = 1; i <= N; i++){
cin >> arr[i];
}
/*
for(int i = 1; i <= N; i++){
mnpre[i] = max(arr[i], mnpre[i-1] + 1);
sumpre[i] = sumpre[i-1] + mnpre[i] - arr[i];
}
for(int i = N; i; i--){
mnsuf[i] = max(arr[i], mnsuf[i+1] + 1);
sumsuf[i] = sumsuf[i+1] + mnsuf[i] - arr[i];
}
long long ans = LLONG_MAX/2;
for(int i = 1; i <= N; i++){
int lft = mnpre[i-1];
int rht = mnsuf[i+1];
long long tot = sumpre[i-1] + sumsuf[i+1];
tot += max({lft, rht, arr[i]}) - arr[i];
ans = min(ans, tot);
}
cout << ans;
*/
for(int i = 1; i <= N; i++){
mxpre[i] = max(0, arr[i-1] + 1 - arr[i]);
mxpre[i] += mxpre[i-1];
}
for(int i = N; i; i--){
mxsuf[i] = max(0, arr[i+1] + 1 - arr[i]);
mxsuf[i] += mxsuf[i+1];
}
int ans = INT_MAX;
for(int i = 1; i <= N; i++){
ans = min(ans, max(mxsuf[i], mxpre[i]));
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |