#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);
*/
ans=min(ans,max(L[i],R[i]));
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
94 ms |
6900 KB |
Output is correct |
27 |
Correct |
92 ms |
6888 KB |
Output is correct |
28 |
Correct |
93 ms |
6896 KB |
Output is correct |
29 |
Correct |
120 ms |
6904 KB |
Output is correct |
30 |
Correct |
70 ms |
6212 KB |
Output is correct |
31 |
Correct |
94 ms |
6852 KB |
Output is correct |
32 |
Correct |
98 ms |
6888 KB |
Output is correct |
33 |
Correct |
102 ms |
6900 KB |
Output is correct |
34 |
Correct |
94 ms |
6888 KB |
Output is correct |
35 |
Correct |
67 ms |
6120 KB |
Output is correct |
36 |
Correct |
70 ms |
6132 KB |
Output is correct |
37 |
Correct |
71 ms |
6252 KB |
Output is correct |
38 |
Correct |
93 ms |
6908 KB |
Output is correct |
39 |
Correct |
122 ms |
6828 KB |
Output is correct |
40 |
Correct |
92 ms |
6888 KB |
Output is correct |
41 |
Correct |
98 ms |
6884 KB |
Output is correct |
42 |
Correct |
86 ms |
6724 KB |
Output is correct |
43 |
Correct |
92 ms |
6884 KB |
Output is correct |
44 |
Correct |
92 ms |
6824 KB |
Output is correct |
45 |
Correct |
90 ms |
6904 KB |
Output is correct |
46 |
Correct |
111 ms |
6884 KB |
Output is correct |
47 |
Correct |
129 ms |
6896 KB |
Output is correct |
48 |
Correct |
92 ms |
6900 KB |
Output is correct |