Submission #1318142

#TimeUsernameProblemLanguageResultExecution timeMemory
1318142cavid_07Growing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
19 ms4920 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    vector<int> a(n + 1), pref(n + 1, 0), suff(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    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] < a[i + 1] + 1) {
            suff[i] = suff[i + 1] + a[i + 1] + 1 - a[i];
        }
        else {
            suff[i] = suff[i + 1];
        }
    }
    int ans = 1e18;
    for (int i = 1; i <= n; i++) {
        ans = min(ans, max(pref[i], suff[i]));
    }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...