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 double long double
using namespace std;
const double eps=1e-6;
int32_t main()
{
int n; cin >> n;
vector<int> h(n+1),L(n+2,0),R(n+2,0);
for(int i=1;i<=n;++i) cin >> h[i];
for(int i=n;i>=1;--i){
R[i]=R[i+1];
if(i+1<=n && h[i+1]>=h[i])
R[i]+=h[i+1]-h[i]+1;
}
for(int i=1;i<=n;++i){
L[i]=L[i-1];
if(i-1>=1 && h[i-1]>=h[i])
L[i]+=h[i-1]-h[i]+1;
}
int ans=1e18;
for(int i=1;i<=n;++i){
int tmpL=0,tmpR=0;
if(i-1>=1 && h[i]+R[i+1]<=h[i-1])
tmpL=h[i-1]-(h[i]+R[i+1])+1;
if(i+1<=n && h[i]+L[i-1]<=h[i+1])
tmpR=h[i+1]-(h[i]+L[i-1])+1;
ans=min(ans,R[i+1]+L[i-1]+tmpL+tmpR);
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |