Submission #747727

#TimeUsernameProblemLanguageResultExecution timeMemory
747727Sami_MassahGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
36 ms10072 KiB
#include <bits/stdc++.h>
using namespace std;


const long long maxn = 2e5 + 12, inf = 1e17 ;
long long n, a[maxn], sum[maxn][2], nat[maxn][2];

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> a[i + 1];
    for(int i = 1; i <= n; i++){
        sum[i][0] = sum[i - 1][0] + (a[i - 1] + 1 - a[i]) * (a[i - 1] + 1 - a[i] >= 0);
        nat[i][0] = max(nat[i - 1][0] + 1, a[i]);
   }
    for(int i = n; i >= 1; i--){
        sum[i][1] = sum[i + 1][1] + (a[i + 1] + 1 - a[i]) * (a[i + 1] + 1 - a[i] >= 0);
        nat[i][1] = max(nat[i + 1][1] + 1, a[i]);
        }

    long long ans = inf;
    for(int i = 1; i <= n; i++){
        long long mx = max(sum[i - 1][0], sum[i + 1][1]);
        long long s = sum[i + 1][1] + a[i + 1], f = sum[i - 1][0] + a[i - 1];
        ans = min(ans, mx + max(max(f, s) + 1 - mx - a[i], 0ll));
     //   cout << i << ' ' << f << ' ' << s << ' ' << sum[i - 1][0] << ' ' << sum[i + 1][1] << endl;
       // cout << ans << endl;

    }
    cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...