제출 #756874

#제출 시각아이디문제언어결과실행 시간메모리
756874Niko_shmikoGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++17
100 / 100
79 ms2744 KiB
#include<bits/stdc++.h>
using namespace std;

long long n, i, j, ans, k;

int main(){
    cin >> n;
    long long a[n];
    for(i=0; i<n; i++){
        cin >> a[i];
        if(i>0){
            a[i-1]=a[i]-a[i-1];
        }
    }
    i=0;
    j=n-2;
    while(i<j){
        if(a[i]>0){
            i++;
            continue;
        }
        if(a[j]<0){
            j--;
            continue;
        }
        k=min(1-a[i], 1+a[j]);
        a[i]+=k;
        a[j]-=k;
        ans+=k;
    }
    if(a[i]==0){
        ans++;
    }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...