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>
#define suiii ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define co cout<<
//#pragma GCC optimize("O3,Ofast,unroll-loops")
//#pragma GCC target("avx2,sse3,sse4,avx")
using namespace std;
//stuff
ll n;
ll arr[1000001];
ll pref[100001];
ll suff[1000001];
ll val[1000001];
ll val1[1000001];
ll mn=1e18;
ll cnt=0;
void solve(){
cin>>n;
for(int i=1;i<=n;i++) cin>>arr[i];
for(int i=2;i<=n;i++){
ll last=arr[i-1]+val[i-1];
ll diff=(last+1)-arr[i];
val[i]=max(diff,0ll);
}
for(int i=1;i<=n;i++){
pref[i]=pref[i-1];
if(val[i]>val[i-1]) pref[i]+=val[i]-val[i-1];
}
for(int i=n-1;i>=1;i--){
ll last=arr[i+1]+val1[i+1];
ll diff=(last+1)-arr[i];
val1[i]=max(diff,0ll);
}
for(int i=n;i>=1;i--){
suff[i]=suff[i+1];
if(val1[i]>val1[i+1]) suff[i]+=val1[i]-val1[i+1];
}
for(int i=1;i<=n;i++){
if(i==1) mn=min(mn,pref[i]);
else if(i==n) mn=min(mn,suff[i]);
else{
ll ans=max(pref[i],suff[i]);
if(pref[i]==suff[i]) ans++;
mn=min(mn,ans);
}
}
co mn;
}
int main()
{
suiii
int tt=1;
// cin>>tt;
while(tt--){
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |