#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200200;
int a[N];
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++) {
        cin >> a[i];
    }
    ll ans = 0;
    int pl = 1, pr = n;
    while (pl < pr) {
        if (pl + 1 == pr) {
            if (a[pl] == a[pr]) {
                ans++;
            }
            break;
        }
        if (a[pl] < a[pl + 1]) pl++;
        else if (a[pr - 1] > a[pr]) pr--;
        else if (a[pl] - a[pl + 1] < a[pr] - a[pr - 1]) {
            ans += a[pl] - a[pl + 1] + 1;
            a[pr] -= a[pl] - a[pl + 1] + 1;
            pl++;
        }
        else {
            ans += a[pr] - a[pr - 1] + 1;
            a[pl] -= a[pr] - a[pr - 1] + 1;
            pr--;
        }
    }
    cout << ans << '\n';
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |