제출 #625136

#제출 시각아이디문제언어결과실행 시간메모리
625136boris_mihovGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
100 / 100
34 ms6168 KiB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

typedef long long llong;
const int MAXN = 200000 + 10;
const int INF  = 1e9;

int a[MAXN], n;
llong prefix[MAXN];
llong suffix[MAXN];
void solve()
{
    prefix[1] = suffix[n] = 0;
    for (int i = 2 ; i <= n ; ++i)
    {
        prefix[i] = prefix[i-1] + std::max(0, a[i-1] - a[i] + 1);
    }

    for (int i = n-1 ; i >= 1 ; --i)
    {
        suffix[i] = suffix[i+1] + std::max(0, a[i+1] - a[i] + 1);
    }

    llong ans = 1LL * INF * INF;
    for (int i = 1 ; i <= n ; ++i)
    {
        ans = std::min(ans, std::max(prefix[i], suffix[i]));
    }

    std::cout << ans << '\n';
}

void read()
{
    std::cin >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> a[i];
    }
}

void fastIO()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIO();
    read();
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...