제출 #1348448

#제출 시각아이디문제언어결과실행 시간메모리
1348448Born_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 ll MOD = 998244353, INF = 3e18 + 7;
const int maxn = 1e5 + 10;
int n;
ll a[maxn];
ll pref[maxn], suff[maxn];
ll d[maxn];
void solve(){
    cin >> n;
    for(int i=1; i<=n; ++i) cin >> a[i];
    for(int i=1; i<n; ++i) d[i] = a[i + 1] - a[i];
    for(int i=1; i<n; ++i){
        pref[i] = pref[i - 1];
        if(d[i] <= 0){
            pref[i] += 1ll - d[i];
        }
    }
    for(int i=n-1; i>=1; --i){
        suff[i] = suff[i + 1];
        if(d[i] >= 0){
            suff[i] += d[i] + 1ll;
        }
    }
    ll ans = min(pref[n - 1], suff[1]);
    for(int i=1; i<n-1; ++i){
        ans = min(ans, pref[i] + suff[i + 1]);
    }
    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...