This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<int> a(n), pref(n), suf(n);
for(auto &i: a) cin >> i;
int ma=a[0];
vector<int> pref_ma(n), suf_ma(n);
pref_ma=a, suf_ma=a;
for(int i=1; i<n; i++){
pref[i]+=max(0LL, ma+1-(a[i]+pref[i-1]));
ma=max(a[i]+pref[i-1], ma+1);
pref_ma[i]=ma;
pref[i]+=pref[i-1];
}
ma=a[n-1];
for(int i=n-2; i>=0; i--){
suf[i]+=max(0LL, ma+1-(a[i]+suf[i+1]));
ma=max(a[i]+suf[i+1], ma+1);
suf_ma[i]=ma;
suf[i]+=suf[i+1];
}
int ans=max(pref[0], suf[0]);
for(int i=1; i<n; i++){
ans=min(ans, max(pref[i], suf[i]));
}
cout<<ans<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |