Submission #935274

#TimeUsernameProblemLanguageResultExecution timeMemory
935274weakweakweakGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
1 ms504 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
int n, a[310000];
ll pre[310000] = {0}, suf[310000] = {0};

int main () {
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    a[0] = a[n + 1] = -1;
    
    for (int i = 1; i <= n; i++) {
        pre[i] = pre[i - 1];
        if (a[i] <= a[i - 1]) pre[i] += a[i - 1] + 1 - a[i];
    }
    for (int i = n; i >= 1; i--) {
        suf[i] = suf[i + 1];
        if (a[i] <= a[i + 1]) suf[i] += a[i + 1] + 1 - a[i];
    }
    
    ll ans = min(pre[n], suf[1]);
    for (int i = 1; i <= n; i++) {
        ll zz = max(suf[i + 1],  a[i - 1] + 1LL - a[i])  +   max(pre[i - 1], a[i + 1] + 1LL - a[i]);
        ans = min(ans, zz);
    }
    cout << ans << '\n';
return 0;}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...