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 int long long
#define pb push_back
#define fi first
#define se second
#define X first
#define Y second
#define ist insert
typedef long long ll;
//typedef pair<int,int> pii;
using namespace std;
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
const int INF=17000000000000000;//2147483647;
const int MOD=998244353;//1000000007;
const int N=200005;
int n;
int a[N];
int calc(int k){
vector<int> vc(n+5);
int res=0,s1=0,s2=0;
vc[0]=vc[n+1]=0;
for (int i=1;i<=n;i++)
vc[i]=a[i];
for (int i=2;i<k;i++){
s1+=max(0,vc[i-1]+1-vc[i]);
vc[i]=max(vc[i-1]+1,vc[i]);
}
for (int i=n-1;i>k;i--){
s2+=max(0,vc[i+1]+1-vc[i]);
vc[i]=max(vc[i+1]+1,vc[i]);
}
vc[k]+=max(s1,s2);
return max(s1,s2)+max(0,max(a[k-1],a[k+1])-a[k]);
}
void sol(){
cin >>n;
for (int i=1;i<=n;i++)
cin >>a[i];
int ans=INF;
for (int i=1;i<=n;i++)
ans=min(ans,calc(i));
cout <<ans<<'\n';
/*for (int i=1;i<=n;i++)
cout <<a[i]-a[i-1]<<' ';
cout <<"\n";
for (int i=n;i>=1;i--)
cout <<a[i]-a[i+1]<<' ';
cout <<"\n";*/
}
signed main()
{
int _=1;
//cin >>_;
while (_--) sol();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'long long int calc(long long int)':
Main.cpp:22:9: warning: unused variable 'res' [-Wunused-variable]
22 | int res=0,s1=0,s2=0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |