#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define int ll
const int N = 2e5 + 10;
int a[N];
bool cur[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int all = 0;
for (int i = 1; i <= (n + 1) / 2; i++) {
pair<int, int> mx = {-1e18, 0};
for (int j = 1; j <= n; j++) {
if (!cur[j] && !cur[j - 1] && !cur[j + 1]) {
mx = max(mx, make_pair(a[j], j));
}
}
pair<int, pair<int, int>> mx2 = {-1e18, {0, 0}};
int ptr = 1;
while (ptr <= n) {
if (!cur[ptr]) {
ptr++;
continue;
}
int to = ptr;
while (to + 2 <= n && cur[to + 2]) {
to += 2;
}
if (ptr > 1 && to < n) {
int sum = 0;
for (int j = ptr - 1; j <= to + 1; j += 2) {
sum += a[j];
}
for (int j = ptr; j <= to; j += 2) {
sum -= a[j];
}
mx2 = max(mx2, make_pair(sum, make_pair(ptr, to)));
}
ptr = to + 2;
}
if (mx.F > mx2.F) {
cur[mx.S] = true;
all += mx.F;
}
else {
all += mx2.F;
for (int j = mx2.S.F; j <= mx2.S.S; j += 2) {
cur[j] = false;
}
for (int j = mx2.S.F - 1; j <= mx2.S.S + 1; j += 2) {
cur[j] = true;
}
}
cout << all << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
384 KB |
Output is correct |
2 |
Correct |
17 ms |
384 KB |
Output is correct |
3 |
Correct |
17 ms |
384 KB |
Output is correct |
4 |
Correct |
18 ms |
384 KB |
Output is correct |
5 |
Correct |
17 ms |
256 KB |
Output is correct |
6 |
Correct |
18 ms |
384 KB |
Output is correct |
7 |
Correct |
17 ms |
384 KB |
Output is correct |
8 |
Correct |
17 ms |
384 KB |
Output is correct |
9 |
Correct |
14 ms |
256 KB |
Output is correct |
10 |
Correct |
14 ms |
384 KB |
Output is correct |
11 |
Correct |
20 ms |
512 KB |
Output is correct |
12 |
Correct |
17 ms |
384 KB |
Output is correct |
13 |
Correct |
17 ms |
384 KB |
Output is correct |
14 |
Correct |
17 ms |
384 KB |
Output is correct |
15 |
Correct |
17 ms |
384 KB |
Output is correct |
16 |
Correct |
17 ms |
384 KB |
Output is correct |
17 |
Correct |
17 ms |
384 KB |
Output is correct |
18 |
Correct |
17 ms |
384 KB |
Output is correct |
19 |
Correct |
14 ms |
384 KB |
Output is correct |
20 |
Correct |
14 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
384 KB |
Output is correct |
2 |
Correct |
17 ms |
384 KB |
Output is correct |
3 |
Correct |
17 ms |
384 KB |
Output is correct |
4 |
Correct |
18 ms |
384 KB |
Output is correct |
5 |
Correct |
17 ms |
256 KB |
Output is correct |
6 |
Correct |
18 ms |
384 KB |
Output is correct |
7 |
Correct |
17 ms |
384 KB |
Output is correct |
8 |
Correct |
17 ms |
384 KB |
Output is correct |
9 |
Correct |
14 ms |
256 KB |
Output is correct |
10 |
Correct |
14 ms |
384 KB |
Output is correct |
11 |
Correct |
20 ms |
512 KB |
Output is correct |
12 |
Correct |
17 ms |
384 KB |
Output is correct |
13 |
Correct |
17 ms |
384 KB |
Output is correct |
14 |
Correct |
17 ms |
384 KB |
Output is correct |
15 |
Correct |
17 ms |
384 KB |
Output is correct |
16 |
Correct |
17 ms |
384 KB |
Output is correct |
17 |
Correct |
17 ms |
384 KB |
Output is correct |
18 |
Correct |
17 ms |
384 KB |
Output is correct |
19 |
Correct |
14 ms |
384 KB |
Output is correct |
20 |
Correct |
14 ms |
384 KB |
Output is correct |
21 |
Execution timed out |
5069 ms |
4512 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |