#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n;
cin >> n;
int a[n + 1];
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
int pref[n + 1];
pref[1] = 0;
for(int i = 2; i <= n; i++) {
pref[i] = pref[i - 1];
if(a[i - 1] >= a[i]) {
pref[i] += (a[i - 1] - a[i] + 1);
}
}
int suf[n + 1];
suf[n] = 0;
for(int i = n - 1; i >= 1; i--) {
suf[i] = suf[i + 1];
if(a[i + 1] >= a[i]) {
suf[i] += (a[i + 1] - a[i] + 1);
}
}
int ans = 1e18;
for(int i = 1; i <= n; i++) {
ans = min({ans, max(pref[i], suf[i])});
}
cout << ans << endl;
}
signed main(){
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |