제출 #896634

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

using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    long long ans = 1e18 + 7;
    long long start = 0, finish = 0;
    for (int i = 0; i < n - 1; ++i) {
        if (a[i] <= a[i + 1]) {
            finish += a[i + 1] - a[i] + 1;
        }
    }
    for (int i = 0; i < n; ++i) {
        ans = min(ans, max(start, finish));
        if (i + 1 < n) {
            if (a[i] >= a[i + 1]) {
                start += a[i] - a[i + 1] + 1;
            }
            if (a[i] <= a[i + 1]) {
                finish -= a[i + 1] - a[i] + 1;
            }
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...