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 int long long
using namespace std;
signed main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin>>n;
int a[n],x[n],y[n]; for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n-1;i++)
{
if(a[i]>=a[i+1]) x[i]=a[i]-a[i+1]+1;
else x[i]=0;
if(i) x[i]+=x[i-1];
} y[n-1]=0;
for(int i=n-2;i>=0;i--)
{
if(a[i]<=a[i+1]) y[i]=a[i+1]-a[i]+1;
else y[i]=0;
y[i]+=y[i+1];
}
int ans=min(x[n-2],y[0]);
for(int i=1;i<n-1;i++) ans=min(ans,x[i-1]+y[i+1]);
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |