//Segment Tree is a State of Mind
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr<<#x<<" is "<<x<<endl;
#define debugl(x) cerr <<'\n'<< #x << " is "; for(auto p : x) cerr << p << " "; cerr << endl;
#define debugpl(x) cerr<<"---\n";cerr<<#x<<" is:\n";for(auto p:x)cerr<<p.first<<" "<<p.second<<endl;cerr<<"---\n";cerr<<endl;
#define int long long
#define ld long double
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
int inf=4e18+5;
int mod=1e9+7;
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
//mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
int n;cin>>n;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
int l[n],r[n];
l[0]=0;
for(int i=1;i<n;i++){
l[i]=max(0LL,a[i-1]-a[i]+1);
}
r[n-1]=0;
int ls=0,rs=0;
for(int i=n-2;i>=0;i--){
r[i]=max(0LL,a[i+1]-a[i]+1);
rs+=r[i];
}
//debugl(l)debugl(r)
int ans=LLONG_MAX;
for(int i=0;i<n;i++){
ls+=l[i];
ans=min(ans,max(ls,rs));
rs-=r[i];
}
cout<<ans<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |