#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define ll long long
#define ull unsigned long long
#define lb lower_bound
#define ub upper_bound
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
void solve() {
int n;
cin >> n;
int a[n + 1], pref[n + 1], suf[n + 1];
for (int i = 1; i <= n; i++)cin >> a[i];
pref[1] = 0;
suf[n] = 0;
for (int i = 2; i <= n; i++){
if(a[i] < a[i - 1] + 1){
pref[i] = pref[i - 1] + a[i - 1] + 1 - a[i];
}
else{
pref[i] = pref[i - 1];
}
}
for (int i = n - 1; i >= 1; i--){
if(a[i + 1] + 1 > a[i]){
suf[i] = suf[i + 1] + a[i + 1] + 1 - a[i];
}
else suf[i] = suf[i + 1];
}
int ans = LLONG_MAX;
for (int i = 1; i <= n; i++){
ans = min(ans, max(pref[i], suf[i]));
}
cout << ans << endl;
}
signed main() {
IOS;
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |