Submission #1328031

#TimeUsernameProblemLanguageResultExecution timeMemory
1328031AgageldiGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
20 ms6708 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define N 500005

const int inf = 1e18;

int n, a[N], l[N], r[N], ans = inf, pf[N], sf[N], b[N];

int32_t main() {
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        b[i] = a[i];
    }
    for(int i = 1; i <= n; i++) {
        l[i] = l[i - 1] + max(0ll, a[i - 1] - a[i] + 1);
    }
    int op = 0, mx = 0;
    for(int i = n; i >= 1; i--) {
        r[i] = r[i + 1] + max(0ll, a[i + 1] - a[i] + 1);
    }
    for(int i = 1; i <= n; i++) {
        ans = min(ans,max(l[i], r[i])); 
    }
    cout << ans << endl;
    return 0;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...