#include<bits/stdc++.h>
#define int long long
#define all(v) v.begin(), v.end()
#define SZ(x) (int)x.size()
#define pii pair<int, int>
#define X first
#define Y second
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;//    998244353;
const int llmx = 1e18;
void sol(){
    int n; cin >> n;
    vector< int > v(n + 2), pre(n + 2), suf(n + 2);
    for(int i = 1; i <= n; ++i){
        cin >> v[i];
    }
    for(int i = 1; i <= n; ++i){
        pre[i] = pre[i - 1] + max(0LL, v[i - 1] + 1 - v[i]);
    }
    for(int i = n; i > 0; --i){
        suf[i] = suf[i + 1] + max(0LL, v[i + 1] + 1 - v[i]);
    }
    // for(int i = 1; i <= n; ++i) cerr << pre[i] << " \n"[i == n];
    // for(int i = 1; i <= n; ++i) cerr << suf[i] << " \n"[i == n];
    int ans = llmx;
    for(int i = 1; i <= n; ++i){
        ans = min(ans, max(pre[i], suf[i]));
    }
    cout << ans << "\n";
}
/*
5
3 2 2 3 1
// 3
5
9 7 5 3 1
// 0
2
2021 2021
// 1
8
12 2 34 85 4 91 29 85
// 93
*/
signed main(){
    ios::sync_with_stdio(0), cin.tie(0), cerr.tie(0);
    int t = 1; //cin >> t;
    while(t--) sol();
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |