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;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
void solve(){
int n;
cin>>n;
ll a[n+1],d[n+1];
for(int i=0;i<n;i++)cin>>a[i];
a[n]=0;
d[0]=a[0];
for(int i=1;i<=n;i++)d[i]=a[i]-a[i-1];
ll pre[n],suf[n];
for(int i=0;i<=n;i++){
pre[i]=max(0LL,1-d[i]);
if(i)pre[i]+=pre[i-1];
}
for(int i=n;i>-1;i--){
suf[i]=max(0LL,d[i]+1);
if(i<n)suf[i]+=suf[i+1];
}
ll ans=1e18;
for(int i=0;i<n;i++)ans=min(ans,max(pre[i],suf[i+1]));
cout<<ans<<'\n';
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |