#include <bits/stdc++.h>
#define int long long
using namespace std;
int a;
int z[1000005];
int cost[1000005];
int cur[1000005];
int cost1[1000005];
int cur1[1000005];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a;
for (int i=1;i<=a;i++){
cin >> z[i];
}
for (int i=1;i<=a;i++){
cost[i]=cost[i-1]+max(0LL,z[i-1]-z[i]+1);
}
for (int i=a;i>=1;i--){
cost1[i]=cost1[i+1]+max(0LL,z[i+1]-z[i]+1);
}
int min1=min(cost[a],cost1[1]);
// cout << cost[a] << "\n";
for (int i=1;i<=a;i++){
int pre=z[i]+cost[i-1]+cost1[i+1];
int val=max(cost[i],cost1[i]);
int p=max(cur[i-1],cur1[i+1]);
min1=min(min1,val);
}
cout << min1 << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |