Submission #1109470

#TimeUsernameProblemLanguageResultExecution timeMemory
1109470DobromirAngelovGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
1 ms2384 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; const int MAXN=2e5+5; const long long INF=1e18+5; int n; int a[MAXN]; int l[MAXN], r[MAXN]; long long pref[MAXN], suff[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=2;i<=n;i++) l[i]=max(0, a[i-1]-a[i]+1); for(int i=n-1;i>=1;i--) r[i]=max(0, a[i+1]-a[i]+1); for(int i=2;i<=n;i++) pref[i]=pref[i-1]+l[i]; for(int i=n-1;i>=1;i--) suff[i]=suff[i+1]+r[i]; long long ans=INF; for(int i=1;i<=n;i++) { long long p=pref[i-1], s=suff[i+1], le=l[i], ri=r[i]; long long cur=0; if(le>s) le-=s; else cur+=le, s-=le; if(ri>p) ri-=p; else cur+=ri, p-=ri; cur+=max(p,s); cur+=max(le,ri); ans=min(ans, cur); } cout<<ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...