Submission #1159623

#TimeUsernameProblemLanguageResultExecution timeMemory
1159623tvgkGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
13 ms3492 KiB
#include<bits/stdc++.h>
using namespace std;
#define task "a"
#define se second
#define fi first
#define ll long long
#define ii pair<ll, ll>
const long mxN = 2e5 + 7;
const ll inf = 1e18 + 7;

ll a[mxN], sum[mxN];
int n;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen(task".INP", "r", stdin);
    //freopen(task".OUT", "w", stdout);

    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];

        sum[i] = sum[i - 1];
        if (a[i] <= a[i - 1])
            sum[i] += a[i - 1] + 1 - a[i];
    }

    ll cur = 0, ans = inf;
    for (int i = n; i >= 1; i--)
    {
        if (a[i] <= a[i + 1])
            cur += a[i + 1] + 1 - a[i];
        ans = min(ans, max(cur, sum[i]));
    }
    cout << ans;
}

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