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 ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll mn,mx,x,n,m,k,a[100005],b[100005],l,d,ans,pref[100005],suf[1000005],y;
bool ok,okk;
pair <ll,ll> dp[100005];
int main(){
cin>>n;
for (int i=1; i<=n; i++){
cin>>a[i];
b[i]=a[i];
}
pref[0]=0;
for (int i=2; i<=n; i++){
if (a[i-1]<a[i]) {
pref[i]=pref[i-1]; continue;
}
pref[i]=pref[i-1]+a[i-1]+1-a[i];
}
suf[n]=0;
for (int i=n-1; i>=1; i--){
if (a[i+1]<a[i]) {
suf[i]=suf[i+1]; continue;
}
suf[i]=suf[i+1]+a[i+1]+1-a[i];
}
ans=1e18;
for (int i=1; i<=n; i++){
ans=min(ans,max(pref[i],suf[i]));
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |