#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using ll = long long;
const int maxn = 300100;
typedef pair<ll, int> ii;
int N, M, K;
ll A[maxn], L[maxn], R[maxn];
set<ii> s;
int main() {
// freopen("feast.inp", "r", stdin);
// freopen("feast.out", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
while (N--) {
ll V; cin >> V;
if (M == 0) {
if (V > 0) A[++M] = V;
continue;
}
if ((A[M] > 0) == (V > 0)) A[M] += V;
else A[++M] = V;
}
if (A[M] < 0) {
A[M] = 0;
--M;
}
int Cn = 0;
for (int i = 1; i <= M; ++i) {
Cn += (A[i] > 0);
L[i] = i - 1; R[i] = i + 1;
s.insert(ii(abs(A[i]), i));
}
R[M] = 0;
while (Cn > K) {
ii Cur = *s.begin(); s.erase(Cur);
int i = Cur.y;
ll Val = A[i], LL = L[i], RR = R[i];
if (A[i] > 0) Cn--;
if (LL) {
if (A[LL] > 0) Cn--;
s.erase(ii(abs(A[LL]), LL));
Val += A[LL];
if (L[LL]) R[L[LL]] = i;
}
if (RR) {
if (A[RR] > 0) Cn--;
s.erase(ii(abs(A[RR]), RR));
Val += A[RR];
if (R[RR]) L[R[RR]] = i;
}
A[i] = Val;
A[LL] = A[RR] = 0;
L[i] = L[LL]; R[i] = R[RR];
if (L[i] == 0 && A[i] < 0) {
L[R[i]] = 0;
A[i] = 0;
} else if (R[i] == 0 && A[i] < 0) {
R[L[i]] = 0;
A[i] = 0;
} else s.insert(ii(abs(A[i]), i));
Cn += (A[i] > 0);
}
ll Ans = 0;
for (auto i : s) {
Ans += max(A[i.y], 0ll);
}
cout << Ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
364 KB |
Output is correct |
2 |
Correct |
33 ms |
364 KB |
Output is correct |
3 |
Correct |
33 ms |
364 KB |
Output is correct |
4 |
Correct |
34 ms |
492 KB |
Output is correct |
5 |
Correct |
33 ms |
364 KB |
Output is correct |
6 |
Correct |
32 ms |
364 KB |
Output is correct |
7 |
Correct |
32 ms |
364 KB |
Output is correct |
8 |
Correct |
33 ms |
364 KB |
Output is correct |
9 |
Correct |
32 ms |
364 KB |
Output is correct |
10 |
Correct |
32 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
384 KB |
Output is correct |
2 |
Correct |
23 ms |
1516 KB |
Output is correct |
3 |
Correct |
22 ms |
1516 KB |
Output is correct |
4 |
Correct |
24 ms |
1516 KB |
Output is correct |
5 |
Correct |
34 ms |
3228 KB |
Output is correct |
6 |
Correct |
22 ms |
1516 KB |
Output is correct |
7 |
Correct |
23 ms |
1516 KB |
Output is correct |
8 |
Correct |
34 ms |
3180 KB |
Output is correct |
9 |
Correct |
33 ms |
3180 KB |
Output is correct |
10 |
Correct |
22 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
13036 KB |
Output is correct |
2 |
Correct |
255 ms |
16108 KB |
Output is correct |
3 |
Correct |
268 ms |
16208 KB |
Output is correct |
4 |
Correct |
254 ms |
15852 KB |
Output is correct |
5 |
Correct |
250 ms |
15980 KB |
Output is correct |
6 |
Correct |
263 ms |
16108 KB |
Output is correct |
7 |
Correct |
257 ms |
16236 KB |
Output is correct |
8 |
Correct |
259 ms |
16108 KB |
Output is correct |
9 |
Correct |
272 ms |
16236 KB |
Output is correct |
10 |
Correct |
262 ms |
16364 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 |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 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 |
492 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 |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
364 KB |
Output is correct |
2 |
Correct |
33 ms |
364 KB |
Output is correct |
3 |
Correct |
33 ms |
364 KB |
Output is correct |
4 |
Correct |
34 ms |
492 KB |
Output is correct |
5 |
Correct |
33 ms |
364 KB |
Output is correct |
6 |
Correct |
32 ms |
364 KB |
Output is correct |
7 |
Correct |
32 ms |
364 KB |
Output is correct |
8 |
Correct |
33 ms |
364 KB |
Output is correct |
9 |
Correct |
32 ms |
364 KB |
Output is correct |
10 |
Correct |
32 ms |
364 KB |
Output is correct |
11 |
Correct |
22 ms |
384 KB |
Output is correct |
12 |
Correct |
23 ms |
1516 KB |
Output is correct |
13 |
Correct |
22 ms |
1516 KB |
Output is correct |
14 |
Correct |
24 ms |
1516 KB |
Output is correct |
15 |
Correct |
34 ms |
3228 KB |
Output is correct |
16 |
Correct |
22 ms |
1516 KB |
Output is correct |
17 |
Correct |
23 ms |
1516 KB |
Output is correct |
18 |
Correct |
34 ms |
3180 KB |
Output is correct |
19 |
Correct |
33 ms |
3180 KB |
Output is correct |
20 |
Correct |
22 ms |
1516 KB |
Output is correct |
21 |
Correct |
284 ms |
13036 KB |
Output is correct |
22 |
Correct |
255 ms |
16108 KB |
Output is correct |
23 |
Correct |
268 ms |
16208 KB |
Output is correct |
24 |
Correct |
254 ms |
15852 KB |
Output is correct |
25 |
Correct |
250 ms |
15980 KB |
Output is correct |
26 |
Correct |
263 ms |
16108 KB |
Output is correct |
27 |
Correct |
257 ms |
16236 KB |
Output is correct |
28 |
Correct |
259 ms |
16108 KB |
Output is correct |
29 |
Correct |
272 ms |
16236 KB |
Output is correct |
30 |
Correct |
262 ms |
16364 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 |
1 ms |
492 KB |
Output is correct |
52 |
Correct |
1 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 |
492 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 |
1 ms |
492 KB |
Output is correct |
60 |
Correct |
1 ms |
492 KB |
Output is correct |
61 |
Correct |
172 ms |
15852 KB |
Output is correct |
62 |
Correct |
135 ms |
16236 KB |
Output is correct |
63 |
Correct |
265 ms |
15852 KB |
Output is correct |
64 |
Correct |
175 ms |
16108 KB |
Output is correct |
65 |
Correct |
207 ms |
16108 KB |
Output is correct |
66 |
Correct |
223 ms |
16196 KB |
Output is correct |
67 |
Correct |
213 ms |
15964 KB |
Output is correct |
68 |
Correct |
252 ms |
16180 KB |
Output is correct |
69 |
Correct |
250 ms |
16108 KB |
Output is correct |
70 |
Correct |
252 ms |
15852 KB |
Output is correct |