#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define dd(x) cout<<#x<<" is "<<x<<endl;
typedef pair<int,int> pii;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
int a[n+2]= {};
for(int i=1; i<=n; i++) cin>>a[i];
int ans = LLONG_MAX;
int x=0, y=0;
for(int i=1; i<=n; i++){
if(a[i] <= a[i+1]) y+= (a[i+1]-a[i]+1);
}
ans = y;
for(int k=2; k<=n; k++){ //k is the tallest guy
if(a[k]>=a[k-1]) y -= (a[k]-a[k-1] +1);
if(a[k] <= a[k-1]) x += (a[k-1]-a[k] +1);
ans = min(ans,max(x,y));
}
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |