Submission #937326

#TimeUsernameProblemLanguageResultExecution timeMemory
937326TrustfulcomicGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
1 ms348 KiB
#include<bits/stdc++.h>

using namespace std;

int main(){
    int n; cin >> n;
    vector<int> h(n);
    for (int i = 0; i<n; i++){
        cin >> h[i];
    }

    int res = 0;

    int l = 0; 
    int r = n-1;
    while (l < r){
        while (h[l] < h[l+1]) l++;
        while (h[r-1] > h[r]) r--;

        if (l >= r) break;
        if (l == r-1){
            if (h[l] == h[r]) res++;
            break;
        }

        if (h[l] - h[l+1] > h[r] - h[r-1]){
            res += h[r] - h[r-1] + 1;
            h[l] -= (h[r] - h[r-1] + 1);
            h[r] = h[r-1] - 1;
        } else {
            res += h[l] - h[l+1] + 1;
            h[r] -= h[l] - h[l+1] + 1;
            h[l] = h[l+1] - 1;
        }
    }

    cout << res << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...