#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;
#define ii pair <long long, int>
#define fi first
#define se second
const int MAXN = 3e5 + 5;
const long long INF = 1e18 + 7;
int N, K, M = 0, L[MAXN], R[MAXN];
long long a[MAXN];
void solve(void) {
cin >> N >> K;
FOR(i, 1, N) {
int x; cin >> x;
if (M == 0) {if (x > 0) a[++M] = x; continue;}
if ((a[M] > 0) == (x > 0)) a[M] += x;
else a[++M] = x;
}
while (a[M] <= 0) M--;
int num_positive = 0;
N = M; set <ii> s;
FOR(i, 1, N) {
num_positive += a[i] > 0;
L[i] = i - 1; R[i] = i + 1;
s.insert(ii(abs(a[i]), i));
}
while (num_positive > K) {
int id = (*s.begin()).se; s.erase(s.begin());
long long val = a[id]; int l = L[id], r = R[id];
if (a[id] > 0) num_positive--;
if (l) {
if (L[l]) R[L[l]] = id;
s.erase(ii(abs(a[l]), l));
val += a[l];
num_positive -= a[l] > 0;
}
if (r) {
if (R[r]) L[R[r]] = id;
s.erase(ii(abs(a[r]), r));
val += a[r];
num_positive -= a[r] > 0;
}
a[id] = val;
L[id] = L[l]; R[id] = R[r];
if (L[id] == 0 && a[id] < 0) L[R[id]] = a[id] = 0;
else if (R[id] == 0 && a[id] < 0) R[L[id]] = a[id] = 0;
else s.insert(ii(abs(a[id]), id));
num_positive += a[id] > 0;
}
long long ans = 0;
for (auto x : s) ans += max(0ll, a[x.se]);
cout << ans;
}
signed main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
//file("TASK");
//int T; cin >> T; while (T--)
solve();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
/*
6 2
1 2 3 -10 5 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
3108 KB |
Output is correct |
2 |
Correct |
33 ms |
3116 KB |
Output is correct |
3 |
Correct |
36 ms |
3112 KB |
Output is correct |
4 |
Correct |
31 ms |
3108 KB |
Output is correct |
5 |
Correct |
49 ms |
3160 KB |
Output is correct |
6 |
Correct |
32 ms |
3108 KB |
Output is correct |
7 |
Correct |
31 ms |
3104 KB |
Output is correct |
8 |
Correct |
36 ms |
3108 KB |
Output is correct |
9 |
Correct |
31 ms |
3108 KB |
Output is correct |
10 |
Correct |
38 ms |
3104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
1428 KB |
Output is correct |
2 |
Correct |
21 ms |
1492 KB |
Output is correct |
3 |
Correct |
29 ms |
1460 KB |
Output is correct |
4 |
Correct |
19 ms |
1364 KB |
Output is correct |
5 |
Correct |
55 ms |
3104 KB |
Output is correct |
6 |
Correct |
39 ms |
1456 KB |
Output is correct |
7 |
Correct |
19 ms |
1448 KB |
Output is correct |
8 |
Correct |
37 ms |
3104 KB |
Output is correct |
9 |
Correct |
33 ms |
2996 KB |
Output is correct |
10 |
Correct |
39 ms |
1456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
14888 KB |
Output is correct |
2 |
Correct |
244 ms |
14668 KB |
Output is correct |
3 |
Correct |
244 ms |
14864 KB |
Output is correct |
4 |
Correct |
253 ms |
14708 KB |
Output is correct |
5 |
Correct |
238 ms |
14804 KB |
Output is correct |
6 |
Correct |
273 ms |
14932 KB |
Output is correct |
7 |
Correct |
256 ms |
14944 KB |
Output is correct |
8 |
Correct |
232 ms |
14828 KB |
Output is correct |
9 |
Correct |
220 ms |
15032 KB |
Output is correct |
10 |
Correct |
264 ms |
15048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
3108 KB |
Output is correct |
2 |
Correct |
33 ms |
3116 KB |
Output is correct |
3 |
Correct |
36 ms |
3112 KB |
Output is correct |
4 |
Correct |
31 ms |
3108 KB |
Output is correct |
5 |
Correct |
49 ms |
3160 KB |
Output is correct |
6 |
Correct |
32 ms |
3108 KB |
Output is correct |
7 |
Correct |
31 ms |
3104 KB |
Output is correct |
8 |
Correct |
36 ms |
3108 KB |
Output is correct |
9 |
Correct |
31 ms |
3108 KB |
Output is correct |
10 |
Correct |
38 ms |
3104 KB |
Output is correct |
11 |
Correct |
20 ms |
1428 KB |
Output is correct |
12 |
Correct |
21 ms |
1492 KB |
Output is correct |
13 |
Correct |
29 ms |
1460 KB |
Output is correct |
14 |
Correct |
19 ms |
1364 KB |
Output is correct |
15 |
Correct |
55 ms |
3104 KB |
Output is correct |
16 |
Correct |
39 ms |
1456 KB |
Output is correct |
17 |
Correct |
19 ms |
1448 KB |
Output is correct |
18 |
Correct |
37 ms |
3104 KB |
Output is correct |
19 |
Correct |
33 ms |
2996 KB |
Output is correct |
20 |
Correct |
39 ms |
1456 KB |
Output is correct |
21 |
Correct |
255 ms |
14888 KB |
Output is correct |
22 |
Correct |
244 ms |
14668 KB |
Output is correct |
23 |
Correct |
244 ms |
14864 KB |
Output is correct |
24 |
Correct |
253 ms |
14708 KB |
Output is correct |
25 |
Correct |
238 ms |
14804 KB |
Output is correct |
26 |
Correct |
273 ms |
14932 KB |
Output is correct |
27 |
Correct |
256 ms |
14944 KB |
Output is correct |
28 |
Correct |
232 ms |
14828 KB |
Output is correct |
29 |
Correct |
220 ms |
15032 KB |
Output is correct |
30 |
Correct |
264 ms |
15048 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
328 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
324 KB |
Output is correct |
40 |
Correct |
1 ms |
324 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
324 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
352 KB |
Output is correct |
53 |
Correct |
2 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
2 ms |
340 KB |
Output is correct |
60 |
Correct |
2 ms |
344 KB |
Output is correct |
61 |
Correct |
139 ms |
14672 KB |
Output is correct |
62 |
Correct |
110 ms |
15032 KB |
Output is correct |
63 |
Correct |
199 ms |
14640 KB |
Output is correct |
64 |
Correct |
134 ms |
14940 KB |
Output is correct |
65 |
Correct |
154 ms |
14948 KB |
Output is correct |
66 |
Correct |
160 ms |
14804 KB |
Output is correct |
67 |
Correct |
170 ms |
14664 KB |
Output is correct |
68 |
Correct |
214 ms |
14860 KB |
Output is correct |
69 |
Correct |
189 ms |
14680 KB |
Output is correct |
70 |
Correct |
234 ms |
14632 KB |
Output is correct |