#include <bits/stdc++.h>
using namespace std;
int n;
int a[5'000'05];
long long pre[5'000'05], suf[5'000'05];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++){
pre[i] = pre[i - 1] + max(0, a[i - 1] - a[i] + 1);
}
for(int i = n; i >= 1; i--){
suf[i] = suf[i + 1] + max(0, a[i + 1] - a[i] + 1);
}
long long ans = 1e18;
for(int i = 1; i <= n; i++){
ans = min(ans, max(pre[i], suf[i]));
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |