#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 12, inf = 1e17 ;
long long n, a[maxn], sum[maxn][2], nat[maxn][2];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i + 1];
for(int i = 1; i <= n; i++){
sum[i][0] = sum[i - 1][0] + (a[i - 1] + 1 - a[i]) * (a[i - 1] + 1 - a[i] >= 0);
nat[i][0] = max(nat[i - 1][0] + 1, a[i]);
}
for(int i = n; i >= 1; i--){
sum[i][1] = sum[i + 1][1] + (a[i + 1] + 1 - a[i]) * (a[i + 1] + 1 - a[i] >= 0);
nat[i][1] = max(nat[i + 1][1] + 1, a[i]);
}
long long ans = inf;
for(int i = 1; i <= n; i++){
long long mx = max(sum[i - 1][0], sum[i + 1][1]);
long long s = sum[i + 1][1] + a[i + 1], f = sum[i - 1][0] + a[i - 1];
ans = min(ans, mx + max(max(f, s) + 1 - mx - a[i], 0ll));
// cout << i << ' ' << f << ' ' << s << ' ' << sum[i - 1][0] << ' ' << sum[i + 1][1] << endl;
// cout << ans << endl;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
368 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
368 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
24 ms |
10060 KB |
Output is correct |
27 |
Correct |
25 ms |
10064 KB |
Output is correct |
28 |
Correct |
25 ms |
10060 KB |
Output is correct |
29 |
Correct |
25 ms |
10024 KB |
Output is correct |
30 |
Correct |
24 ms |
9368 KB |
Output is correct |
31 |
Correct |
27 ms |
10036 KB |
Output is correct |
32 |
Correct |
24 ms |
10060 KB |
Output is correct |
33 |
Correct |
25 ms |
10060 KB |
Output is correct |
34 |
Correct |
27 ms |
10004 KB |
Output is correct |
35 |
Correct |
19 ms |
9244 KB |
Output is correct |
36 |
Correct |
20 ms |
9396 KB |
Output is correct |
37 |
Correct |
20 ms |
9292 KB |
Output is correct |
38 |
Correct |
24 ms |
10072 KB |
Output is correct |
39 |
Correct |
24 ms |
10012 KB |
Output is correct |
40 |
Correct |
26 ms |
9964 KB |
Output is correct |
41 |
Correct |
26 ms |
10068 KB |
Output is correct |
42 |
Correct |
24 ms |
9804 KB |
Output is correct |
43 |
Correct |
33 ms |
10056 KB |
Output is correct |
44 |
Correct |
36 ms |
9980 KB |
Output is correct |
45 |
Correct |
27 ms |
9964 KB |
Output is correct |
46 |
Correct |
24 ms |
10000 KB |
Output is correct |
47 |
Correct |
25 ms |
10072 KB |
Output is correct |
48 |
Correct |
25 ms |
10028 KB |
Output is correct |