제출 #1319671

#제출 시각아이디문제언어결과실행 시간메모리
1319671camil7Growing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
19 ms5116 KiB
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define ll long long
#define ull unsigned long long
#define lb lower_bound
#define ub upper_bound
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

void solve() {
    int n;
    cin >> n;
    int a[n + 1], pref[n + 1], suf[n + 1];
    for (int i = 1; i <= n; i++)cin >> a[i];
    pref[1] = 0;
    suf[n] = 0;
    for (int i = 2; i <= n; i++){
        if(a[i] < a[i - 1] + 1){
            pref[i] = pref[i - 1] + a[i - 1] + 1 - a[i];
        }
        else{
            pref[i] = pref[i - 1];
        }
    }
    for (int i = n - 1; i >= 1; i--){
        if(a[i + 1] + 1 > a[i]){
            suf[i] = suf[i + 1] + a[i + 1] + 1 - a[i];
        }
        else suf[i] = suf[i + 1];
    }
    int ans = LLONG_MAX;
    for (int i = 1; i <= n; i++){
        ans = min(ans, max(pref[i], suf[i]));
    }
    cout << ans << endl;
}
signed main() {
    IOS;
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...