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>
#define endl '\n'
using namespace std;
const int MAXN=2e5+5;
const long long INF=1e18+5;
int n;
int a[MAXN];
int l[MAXN], r[MAXN];
long long pref[MAXN], suff[MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=2;i<=n;i++) l[i]=max(0, a[i-1]-a[i]+1);
for(int i=n-1;i>=1;i--) r[i]=max(0, a[i+1]-a[i]+1);
for(int i=2;i<=n;i++) pref[i]=pref[i-1]+l[i];
for(int i=n-1;i>=1;i--) suff[i]=suff[i+1]+r[i];
long long ans=INF;
for(int i=1;i<=n;i++)
{
long long p=pref[i-1], s=suff[i+1], le=l[i], ri=r[i];
long long cur=0;
if(le>s) le-=s;
else cur+=le, s-=le;
if(ri>p) ri-=p;
else cur+=ri, p-=ri;
cur+=max(p,s);
cur+=max(le,ri);
ans=min(ans, cur);
}
cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |