제출 #1110188

#제출 시각아이디문제언어결과실행 시간메모리
1110188LucasLeGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
19 ms8952 KiB
#include <bits/stdc++.h>

const int maxn = 1e6 + 5;

int n;
int a[maxn + 5];
long long pref[maxn + 5], suff[maxn + 5];

void solve() {
    std::cin >> n;
    for (int i = 1; i <= n; ++i) std::cin >> a[i];
    for (int i = 1; i <= n; ++i) {
        pref[i] = pref[i - 1];
        if (a[i] <= a[i - 1]) 
            pref[i] += a[i - 1] - a[i] + 1;
    }
    for (int i = n; i >= 1; --i) {
        suff[i] = suff[i + 1];
        if (a[i] <= a[i + 1]) 
            suff[i] += a[i + 1] - a[i] + 1;
    }
    long long ans = 1e15;
    for (int i = 1; i <= n; ++i) {
        ans = std::min(ans, std::max(pref[i], suff[i]));
    }
    std::cout << ans;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int tc = 1;
    // std::cin >> tc;
    // init();
    while (tc--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...