Submission #854126

#TimeUsernameProblemLanguageResultExecution timeMemory
854126BrineTwGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
1 ms348 KiB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

#define debug(x) cerr << #x << ' ' << x << '\n'
#define endl '\n'
#define printv(v) cerr << #v << ": "; for (auto& n: v) cerr << setw(3) << n << " \n"[&n==&v.back()]

using namespace std;

const int M = 1e9 + 7;
typedef long long ll;


int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int N;
    cin >> N;

    vector<ll> v(N);
    for (auto& n: v) cin >> n;

    int l = 1, r = N - 1; 
    ll ans = 0;
    
    while (r - l) {
        while (v[l] > v[l - 1]) l++;
        while (v[r] > v[r + 1]) r--;
        if (r == l) break;
        
        int d = min(v[l - 1] + 1 - v[l], v[r + 1] + 1 - v[r]);
        v[l - 1] -= d;
        v[r + 1] -= d;
        ans += d;
    }
    ans += min(v[l - 1] - v[l] + 1, v[r + 1] - v[r] + 1);
    cout << ans << '\n';

    return 0;   
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...