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;
void solve(){
cin>>n;
for(int i=1;i<=n;i++) cin>>arr[i];
for(int i=2;i<=n;i++){
pref[i]=pref[i-1];
val[i]=0;
ll last=arr[i-1]+val[i-1];
if(arr[i]<last+1){
ll adding=0;
ll diff=(last+1)-arr[i];
if(diff<=val[i-1]){
adding=0;
val[i]=diff;
}
else{
adding=diff-val[i-1];
val[i]=diff;
}
pref[i]+=adding;
}
}
for(int i=n-1;i>=1;i--){
suff[i]=suff[i+1];
val1[i]=0;
ll last=arr[i+1]+val1[i+1];
if(arr[i]<last+1){
ll adding=0;
ll diff=(last+1)-arr[i];
if(diff<=val1[i+1]){
adding=0;
val1[i]=diff;
}
else{
adding=diff-val1[i+1];
val1[i]=diff;
}
suff[i]+=adding;
}
}
for(int i=1;i<=n;i++){
if(pref[i]>suff[i]){
ll mynum=arr[i]+suff[i];
ll num1=arr[i-1]+pref[i-1]+1;
mn=min(mn,pref[i-1]+max(0ll,num1-mynum));
}
else{
ll mynum=arr[i]+pref[i];
ll num1=arr[i+1]+suff[i+1]+1;
mn=min(mn,suff[i+1]+max(0ll,num1-mynum));
}
// co i<<' '<<mn<<'\n';
// if(i==5) co pref[i]<<' '<<suff[i]<<'\n';
}
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... |