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 main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<long long>arr(n);
for (int i = 0;i<n;++i){
cin>>arr[i];
}
vector<long long>pref(n),suff(n);
vector<long long>prefmax(n),suffmax(n);
vector<long long>maxxy(n,0),maxxy2(n,0);
prefmax[0] = arr[0];
for (int i = 1;i<n;++i){
prefmax[i] = max(prefmax[i - 1] + 1,arr[i]);
}
suffmax[n - 1] = arr[n - 1];
for (int i = n - 2;i>=0;--i){
suffmax[i] = max(suffmax[i + 1] + 1,arr[i]);
}
pref[0] = 0;
long long h = 0;
for (int i =1;i<n;++i){
long long needed = max(0LL,prefmax[i - 1] + 1 - arr[i]);
pref[i] = pref[i - 1] + max(0LL,prefmax[i - 1] + 1 - arr[i] - h);
maxxy[i] = max(maxxy[i - 1],max(0LL,prefmax[i - 1] + 1 - arr[i] - h));
h = needed;
}
h = 0;
suff[n - 1] = 0;
for (int i =n - 2;i>=0;--i){
long long needed = max(0LL,suffmax[i + 1] + 1 - arr[i]);
suff[i] = suff[i + 1] + max(0LL,suffmax[i + 1] + 1 - arr[i] - h);
maxxy2[i] = max(maxxy2[i + 1],max(0LL,suffmax[i + 1] + 1 - arr[i] - h));
h = needed;
}
long long ans = 1e15;
for (int i = 1;i<n - 1;++i){
ans = min(ans,pref[i - 1] + suff[i + 1] + max(0LL,suffmax[i] + 1 - prefmax[i - 1]) - min(maxxy[i - 1],maxxy2[i + 1]));
}
ans = min(ans,pref[n - 1]);
ans = min(ans,suff[0]);
cout<<ans<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |