Submission #632164

#TimeUsernameProblemLanguageResultExecution timeMemory
632164Ooops_sorryGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
100 / 100
30 ms6224 KiB
#include<bits/stdc++.h>

using namespace std;

mt19937 rnd(51);

#define ll long long
#define pb push_back
#define ld long double

const ll INF = 1e18;

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    vector<ll> pr(n), sf(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        pr[i] = pr[i - 1];
        if (a[i - 1] >= a[i]) {
            pr[i] += a[i - 1] - a[i] + 1;
        }
    }
    for (int i = n - 2; i >= 0; i--) {
        sf[i] = sf[i + 1];
        if (a[i] <= a[i + 1]) {
            sf[i] += a[i + 1] - a[i] + 1;
        }
    }
    ll res = INF;
    for (int i = 0; i < n; i++) {
        ll cnt0 = (i > 0 ? pr[i - 1] : 0), cnt1 = (i + 1 < n ? sf[i + 1] : 0);
        ll need = -1;
        if (i > 0) {
            need = max(need, a[i - 1] + cnt0 + 1);
        }
        if (i + 1 < n) {
            need = max(need, a[i + 1] + cnt1 + 1);
        }
        res = min(res, max(0ll, need - a[i] - max(cnt0, cnt1)) + max(cnt0, cnt1));
    }
    cout << res << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...