Submission #1337734

#TimeUsernameProblemLanguageResultExecution timeMemory
1337734BolatuluGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
17 ms5120 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define int ll

#define all(x) (x).begin(), (x).end()
#define md ((tl + tr) >> 1)
#define TL v + v, tl, md
#define TR v + v + 1, md + 1, tr
#define F first
#define S second
#define pii pair<int, int>

const int maxn = 5e5 + 7;
const int maxw = 1e6 + 7;
const ll inf = 1e18 + 7;
const double PI = atan2(0.0, -1.0);

int n, a[maxn], pref[maxn], suf[maxn];

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    
    for (int i = 2; i <= n; i++) {
        pref[i] = pref[i - 1] + max(a[i], a[i - 1] + 1) - a[i];
    }
    for (int i = n - 1; i >= 1; i--) {
        suf[i] = suf[i + 1] + max(a[i], a[i + 1] + 1) - a[i];
    }

    int ans = inf;
    for (int i = 1; i <= n; i++)
        ans = min(ans, max(pref[i], suf[i]));
    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int test = 1;
    // cin >> test;
    while (test--) {
        solve();
        // cout << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...