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
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb push_back
using namespace std;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n;cin>>n;
vector <int> a(n+1),pr(n+1),sf(n+1);
for(int i=1;i<=n;i++)cin>>a[i];
a.pb(0);
int mn=1e18;
for(int j=1;j<=n;j++){
pr[j]=pr[j-1]+max(0ll,a[j-1]-a[j]+1);
}
for(int j=n;j>=1;j--){
sf[j]=sf[j+1]+max(0ll,a[j+1]-a[j]+1);
}
for(int i=1;i<=n;i++){
mn=min(mn,max(pr[i],sf[i]));
}
cout<<mn<<"\n";
}
/*
5
3 2 2 3 1
5
9 7 5 3 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |