Submission #637185

#TimeUsernameProblemLanguageResultExecution timeMemory
637185danikoynovGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
100 / 100
83 ms5372 KiB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 2e5 + 10;
int n;
ll a[maxn], b[maxn];
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
        cin >> a[i];

    for (int i = 1; i < n; i ++)
        b[i] = a[i + 1] - a[i];

    int l = 1, r = n - 1;
    ll ans = 0;
    while(true)
    {
        while(l < n && b[l] > 0)
            l ++;
        while(r > 0 && b[r] < 0)
            r --;
        if (l > r)
            break;
        if (l == r)
        {
            if (b[l] == 0)
                ans ++;
            break;
        }

        ll s = min(-b[l] + 1, b[r] + 1);
        b[l] += s;
        b[r] -= s;
        ans += s;
    }
    cout << ans << endl;

}

int main()
{
    solve();
    return 0;
}

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