제출 #1338318

#제출 시각아이디문제언어결과실행 시간메모리
1338318kawhietGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
30 ms1092 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int64_t l = 0, r = 0;
    for (int i = 0; i < n - 1; i++) {
        if (a[i] <= a[i + 1]) {
            r += a[i + 1] - a[i] + 1;
        }
    }
    int64_t ans = max(l, r);
    for (int i = 0; i < n - 1; i++) {
        if (a[i] <= a[i + 1]) {
            r -= a[i + 1] - a[i] + 1;
        } 
        if (a[i] >= a[i + 1]) {
            l += a[i] - a[i + 1] + 1;
        }
        ans = min(ans, max(l, r));
    }
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...