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;
long long n,a[200005],pref[200005][2],suf[200005][2],ans=1999999999;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
pref[i][1]=max((a[i-1]+pref[i-1][1])-a[i]+1,0ll);
pref[i][0]=pref[i-1][0]+max(pref[i][1]-pref[i-1][1],0ll);
// cout<<pref[i][0]<<' ';
}
// cout<<endl;
for(int i=n;i>=1;i--){
suf[i][1]=max((a[i+1]+suf[i+1][1])-a[i]+1,0ll);
suf[i][0]=suf[i+1][0]+max(suf[i][1]-suf[i+1][1],0ll);
// cout<<suf[i][0]<<' ';
}
// cout<<endl;
for(int i=0;i<=n;i++){
// cout<<i<<' '<<pref[i]<<' '<<suf[i+1]<<endl;
if(a[i]+pref[i][1]==a[i+1]+suf[i+1][1]) ans=min(ans,max(pref[i][0],suf[i+1][0])+1);
else ans=min(ans,max(pref[i][0],suf[i+1][0]));
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |