#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;
for(int k=1; k<=n; k++){ //k is the tallest guy
stack<int>l,r;
for(int i=1; i<=k; i++){
if(a[i] <= a[i-1]) l.push(a[i-1]-a[i]+1);
}
for(int i=k; i<=n; i++){
if(a[i] <= a[i+1]) r.push(a[i+1]-a[i]+1);
}
int sum =0;
while(l.size() and r.size()){
int x = l.top(); l.pop();
int y = r.top(); r.pop();
if(x > y){
l.push(x-y);
sum += y;
} else if(y>x) {
r.push(y-x);
sum += x;
} else sum += x;
}
while(l.size()){
int x = l.top(); l.pop(); sum += x;
}
while(r.size()){
int x = r.top(); r.pop(); sum += x;
}
ans = min(ans, sum);
}
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |