Submission #1324162

#TimeUsernameProblemLanguageResultExecution timeMemory
1324162ghammazhassanGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++20
100 / 100
16 ms1848 KiB
// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
using namespace std;
#define int long long
#define endl "\n"
#define fi first
#define se second
const int M=998244353;
const int inf = 1e14;
const int LOG=17;
const int N=5e3+5;
int n , m , c , w , k , t=1 , q=1 , x , y , z , l , r;
void solve(){
    cin >> n;
    vector<int>a(n);
    for (int i=0;i<n;i++){
        cin >> a[i];
    }
    c=0;
    int i=0,j=n-1;
    while (j-i>1){
        while (i<j and a[i]<a[i+1])i++;
        while (j>i and a[j]<a[j-1])j--;
        if (i==j)break;
        int u=max(0ll,min(a[i]-a[i+1]+1,a[j]-a[j-1]+1));
        a[i]-=u;
        a[j]-=u;
        c+=u;
        while (i<j and a[i]<a[i+1])i++;
        while (j>i and a[j]<a[j-1])j--;
    }
    if (i!=j and a[i]==a[j])c++;
    cout << c << endl;
}
signed main()    
{   
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt","r" ,stdin);
    // freopen("output.txt","w",stdout);
    // #endif
    ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
    cin.tie(0), cout.tie(0);//DO NOT USE IN INTERACTIVE
    cout << fixed << setprecision(9);
    srand(time(0));
    // int t=1;
    // cin >> t;
    for (int _=1;_<=t;_++){
        solve();
        q++;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...