#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct block {
ll val;
block *prev, *next;
block(ll val): val(val) {
prev = next = nullptr;
}
};
using blockpt = block*;
struct compare {
bool operator () (const blockpt &x,
const blockpt &y) const {
return abs(x->val) != abs(y->val) ?
abs(x->val) < abs(y->val) : x < y;
}
};
const int N = 3e5 + 5;
set <block*, compare> blocks;
int a[N]; block *b[N];
bool sign(const int &x) {
return x <= 0 ? 0 : 1;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k, m = 0, p = 0;
cin >> n >> k;
for (int i = 1; i <= n; cin >> a[i++]);
for (int i = 1; i <= n; ) {
int j = i; ll sum = 0;
while (j <= n && sign(a[i])
== sign(a[j])) sum += a[j++];
if (sum > 0 || m > 0)
b[m++] = new block(sum);
i = j; p += sum > 0;
}
if (m > 0 && b[m - 1]->val < 0) m--;
for (int i = 0; i < m; i++)
blocks.insert(b[i]);
for (int i = 1; i < m; i++) {
b[i]->prev = b[i - 1];
b[i - 1]->next = b[i];
}
while (p > k && blocks.size()) {
blockpt it = *blocks.begin();
blocks.erase(it);
if (it->val > 0) p--;
if (it->prev) {
blocks.erase(it->prev);
if (it->prev->val > 0) p--;
it->val += it->prev->val;
it->prev = it->prev->prev;
if (it->prev)
it->prev->next = it;
}
if (it->next) {
blocks.erase(it->next);
if (it->next->val > 0) p--;
it->val += it->next->val;
it->next = it->next->next;
if (it->next)
it->next->prev = it;
}
if (it->val <= 0) {
if (!it->prev)
it->next->prev = 0;
else if (!it->next)
it->prev->next = 0;
else blocks.insert(it);
}
else {
blocks.insert(it); p++;
}
}
ll res = 0;
for (auto it : blocks)
res += max(0ll, it->val);
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
1516 KB |
Output is correct |
2 |
Correct |
33 ms |
1516 KB |
Output is correct |
3 |
Correct |
34 ms |
1516 KB |
Output is correct |
4 |
Correct |
34 ms |
1516 KB |
Output is correct |
5 |
Correct |
33 ms |
1516 KB |
Output is correct |
6 |
Correct |
35 ms |
1644 KB |
Output is correct |
7 |
Correct |
32 ms |
1516 KB |
Output is correct |
8 |
Correct |
37 ms |
1516 KB |
Output is correct |
9 |
Correct |
33 ms |
1516 KB |
Output is correct |
10 |
Correct |
34 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
1516 KB |
Output is correct |
2 |
Correct |
23 ms |
1516 KB |
Output is correct |
3 |
Correct |
22 ms |
1516 KB |
Output is correct |
4 |
Correct |
28 ms |
1516 KB |
Output is correct |
5 |
Correct |
43 ms |
1516 KB |
Output is correct |
6 |
Correct |
23 ms |
1516 KB |
Output is correct |
7 |
Correct |
24 ms |
1644 KB |
Output is correct |
8 |
Correct |
34 ms |
1516 KB |
Output is correct |
9 |
Correct |
35 ms |
1644 KB |
Output is correct |
10 |
Correct |
24 ms |
1644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
375 ms |
14316 KB |
Output is correct |
2 |
Correct |
372 ms |
14188 KB |
Output is correct |
3 |
Correct |
373 ms |
14188 KB |
Output is correct |
4 |
Correct |
369 ms |
14208 KB |
Output is correct |
5 |
Correct |
362 ms |
14188 KB |
Output is correct |
6 |
Correct |
378 ms |
14316 KB |
Output is correct |
7 |
Correct |
391 ms |
14316 KB |
Output is correct |
8 |
Correct |
386 ms |
14444 KB |
Output is correct |
9 |
Correct |
371 ms |
14316 KB |
Output is correct |
10 |
Correct |
398 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
1516 KB |
Output is correct |
2 |
Correct |
33 ms |
1516 KB |
Output is correct |
3 |
Correct |
34 ms |
1516 KB |
Output is correct |
4 |
Correct |
34 ms |
1516 KB |
Output is correct |
5 |
Correct |
33 ms |
1516 KB |
Output is correct |
6 |
Correct |
35 ms |
1644 KB |
Output is correct |
7 |
Correct |
32 ms |
1516 KB |
Output is correct |
8 |
Correct |
37 ms |
1516 KB |
Output is correct |
9 |
Correct |
33 ms |
1516 KB |
Output is correct |
10 |
Correct |
34 ms |
1516 KB |
Output is correct |
11 |
Correct |
22 ms |
1516 KB |
Output is correct |
12 |
Correct |
23 ms |
1516 KB |
Output is correct |
13 |
Correct |
22 ms |
1516 KB |
Output is correct |
14 |
Correct |
28 ms |
1516 KB |
Output is correct |
15 |
Correct |
43 ms |
1516 KB |
Output is correct |
16 |
Correct |
23 ms |
1516 KB |
Output is correct |
17 |
Correct |
24 ms |
1644 KB |
Output is correct |
18 |
Correct |
34 ms |
1516 KB |
Output is correct |
19 |
Correct |
35 ms |
1644 KB |
Output is correct |
20 |
Correct |
24 ms |
1644 KB |
Output is correct |
21 |
Correct |
375 ms |
14316 KB |
Output is correct |
22 |
Correct |
372 ms |
14188 KB |
Output is correct |
23 |
Correct |
373 ms |
14188 KB |
Output is correct |
24 |
Correct |
369 ms |
14208 KB |
Output is correct |
25 |
Correct |
362 ms |
14188 KB |
Output is correct |
26 |
Correct |
378 ms |
14316 KB |
Output is correct |
27 |
Correct |
391 ms |
14316 KB |
Output is correct |
28 |
Correct |
386 ms |
14444 KB |
Output is correct |
29 |
Correct |
371 ms |
14316 KB |
Output is correct |
30 |
Correct |
398 ms |
14444 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
2 ms |
492 KB |
Output is correct |
52 |
Correct |
2 ms |
492 KB |
Output is correct |
53 |
Correct |
1 ms |
492 KB |
Output is correct |
54 |
Correct |
1 ms |
492 KB |
Output is correct |
55 |
Correct |
1 ms |
512 KB |
Output is correct |
56 |
Correct |
1 ms |
492 KB |
Output is correct |
57 |
Correct |
1 ms |
492 KB |
Output is correct |
58 |
Correct |
1 ms |
492 KB |
Output is correct |
59 |
Correct |
2 ms |
492 KB |
Output is correct |
60 |
Correct |
1 ms |
492 KB |
Output is correct |
61 |
Correct |
252 ms |
16976 KB |
Output is correct |
62 |
Correct |
183 ms |
17388 KB |
Output is correct |
63 |
Correct |
348 ms |
17024 KB |
Output is correct |
64 |
Correct |
250 ms |
17244 KB |
Output is correct |
65 |
Correct |
283 ms |
17260 KB |
Output is correct |
66 |
Correct |
313 ms |
17260 KB |
Output is correct |
67 |
Correct |
287 ms |
17004 KB |
Output is correct |
68 |
Correct |
365 ms |
17388 KB |
Output is correct |
69 |
Correct |
409 ms |
17004 KB |
Output is correct |
70 |
Correct |
359 ms |
16876 KB |
Output is correct |