제출 #1348444

#제출 시각아이디문제언어결과실행 시간메모리
1348444Born_To_LaughGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
0 / 100
0 ms344 KiB
// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const int MOD = 998244353, INF = 1e9 + 7;

const int maxn = 1e5 + 10;
int n;
int a[maxn];
ll pref[maxn], suff[maxn];
void solve(){
    cin >> n;
    for(int i=1; i<=n; ++i) cin >> a[i];
    for(int i=1; i<n; ++i){
        pref[i] = pref[i - 1];
        if(a[i + 1] <= a[i]){
            pref[i] += (1 - (a[i + 1] - a[i]));
        }
    }
    for(int i=n-1; i>=1; --i){
        suff[i] = suff[i + 1];
        if(a[i] <= a[i + 1]){
            suff[i] += ((a[i + 1] - a[i]) + 1);
        }
    }
    ll ans = INF;
    for(int i=1; i<=n; ++i){
        ans = min(ans, pref[i - 1] + suff[i]);
    }
    cout << ans << '\n';
}
signed main(){
    // freopen("inp.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...