#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];
if (cur[i-1]>=z[i]+cost[i]){
cost[i]+=(cur[i-1]-(z[i]+cost[i]))+1;
}
cur[i]=z[i]+cost[i];
}
for (int i=a;i>=1;i--){
cost1[i]=cost1[i+1];
if (cur1[i+1]>=z[i]+cost1[i]){
cost1[i]+=(cur1[i+1]-(z[i]+cost1[i]))+1;
}
cur1[i]=z[i]+cost1[i];
}
int min1=min(cost[a],cost1[1]);
// cout << cost[a] << "\n";
for (int i=2;i<a;i++){
int pre=z[i]+cost[i-1]+cost1[i+1];
int val=cost[i-1]+cost1[i+1];
int p=max(cur[i-1],cur1[i+1]);
if (pre<=p){
val+= (p-pre)+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... |