#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int arr[200007];
pair<int, int> solve(int tp){
int linc = 0;
for (int x = 1; x <= tp; x++){
if (arr[x-1] >= arr[x]){
//enforce an increase
linc += arr[x-1] - arr[x] + 1;
}
}
int rinc = 0;
for (int x = n-2; x >= tp; x--){
if (arr[x+1] >= arr[x]){
//enforce an increase
rinc += arr[x+1] - arr[x] + 1;
}
}
//cout << tp << ' ' << linc << ' ' << rinc << '\n';
return make_pair(linc, rinc);
}
main(){
cin >> n; for (int x = 0; x < n; x++) cin >> arr[x];
pair<int, int> lr = solve(0);
int ans = max(lr.first, lr.second);
for (int x = 1; x < n; x++) {
if (arr[x-1] >= arr[x]){
//enforce an increase
lr.first += arr[x-1] - arr[x] + 1;
}
if (x == n-1){
lr.second = 0;
}
else if (arr[x] >= arr[x-1]){
//enforce an increase
lr.second -= arr[x] - arr[x-1] + 1;
}
ans = min(ans, max(lr.first, lr.second));
}
cout << ans;
}
Compilation message
Main.cpp:27:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
27 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
73 ms |
1756 KB |
Output is correct |
27 |
Correct |
78 ms |
1872 KB |
Output is correct |
28 |
Correct |
74 ms |
1728 KB |
Output is correct |
29 |
Correct |
73 ms |
1740 KB |
Output is correct |
30 |
Correct |
54 ms |
1768 KB |
Output is correct |
31 |
Correct |
76 ms |
1740 KB |
Output is correct |
32 |
Correct |
75 ms |
1840 KB |
Output is correct |
33 |
Correct |
74 ms |
1840 KB |
Output is correct |
34 |
Correct |
74 ms |
1772 KB |
Output is correct |
35 |
Correct |
51 ms |
1752 KB |
Output is correct |
36 |
Correct |
53 ms |
1796 KB |
Output is correct |
37 |
Correct |
52 ms |
1744 KB |
Output is correct |
38 |
Correct |
74 ms |
1832 KB |
Output is correct |
39 |
Correct |
75 ms |
1768 KB |
Output is correct |
40 |
Correct |
91 ms |
1836 KB |
Output is correct |
41 |
Correct |
72 ms |
1796 KB |
Output is correct |
42 |
Correct |
66 ms |
1744 KB |
Output is correct |
43 |
Correct |
77 ms |
1740 KB |
Output is correct |
44 |
Correct |
71 ms |
1740 KB |
Output is correct |
45 |
Correct |
73 ms |
1816 KB |
Output is correct |
46 |
Correct |
76 ms |
1736 KB |
Output is correct |
47 |
Correct |
72 ms |
1740 KB |
Output is correct |
48 |
Correct |
72 ms |
1800 KB |
Output is correct |