제출 #1267878

#제출 시각아이디문제언어결과실행 시간메모리
1267878gayGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
17 ms4936 KiB
#include <bits/stdc++.h>
#include <experimental/random>
#include <random>

using namespace std;
using ll = long long;
using ld = long double;

const ll INF = 1e18, MOD = 998244353;

void solve();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int q = 1;
    //cin >> q;
    while (q--) {
        solve();
    }
}

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

    ll ans = INF;
    for (int i = 0; i < n; i++) {
        ans = min(ans, max(pref[i], suf[i]));
    }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...