#include <bits/stdc++.h>
#define pii pair<long long, int>
#define abval first
#define pos second
#define ABS(n) (n < 0 ? -n : n)
using namespace std;
int N, K, P;
long long A[300005];
int L[300005], R[300005];
long long ans = 0;
set<pii> seto;
int main() {
// freopen("inp", "r", stdin);
scanf("%d %d", &N, &K);
long long tmp;
while(N--) {
scanf("%lld", &tmp);
if(P == 0) {
if(tmp <= 0) continue;
else {
A[++P] = tmp;
}
}else if(!((A[P] > 0) ^ (tmp > 0))) {
A[P] += tmp;
}else {
A[++P] = tmp;
}
}
if(A[P] < 0) {
A[P] = 0;
P--;
}
for(int i = 1; i <= P; i++) {
seto.insert({ABS(A[i]), i});
L[i] = i - 1;
R[i] = i + 1;
}
R[P] = 0;
int cnt = (P >> 1) + 1;
long long l, m, r, tmpval;
while(cnt > K) {
pii now = *seto.begin();
seto.erase(now);
m = now.pos, l = L[m], r = R[m];
tmpval = A[m];
if(A[m] > 0)cnt--;
if(l != 0) {
if(A[l] > 0) cnt--;
seto.erase({ABS(A[l]), l});
tmpval += A[l];
if(L[l] != 0) R[L[l]] = m;
}
if(r != 0) {
if(A[r] > 0) cnt--;
seto.erase({ABS(A[r]), r});
tmpval += A[r];
if(R[r] != 0) L[R[r]] = m;
}
A[m] = tmpval;
A[l] = 0, A[r] = 0;
L[m] = L[l], R[m] = R[r];
if(L[m] == 0 && A[m] < 0) {
L[R[m]] = 0;
A[m] = 0;
}else if(R[m] == 0 && A[m] < 0) {
R[L[m]] = 0;
A[m] = 0;
}else seto.insert({ABS(tmpval), m});
cnt += (A[m] > 0);
// for(int i = 1; i <= P; i++) {
// cout << A[i] << " ";
// }
// cout << "\n";
}
for(auto i : seto) {
ans += A[i.pos] > 0 ? A[i.pos] : 0;
}
printf("%lld\n", ans);
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:16:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~~
feast.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &tmp);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
2040 KB |
Output is correct |
2 |
Correct |
57 ms |
1920 KB |
Output is correct |
3 |
Correct |
56 ms |
1912 KB |
Output is correct |
4 |
Correct |
58 ms |
2040 KB |
Output is correct |
5 |
Correct |
65 ms |
2040 KB |
Output is correct |
6 |
Correct |
56 ms |
1912 KB |
Output is correct |
7 |
Correct |
55 ms |
1912 KB |
Output is correct |
8 |
Correct |
56 ms |
1912 KB |
Output is correct |
9 |
Correct |
56 ms |
2040 KB |
Output is correct |
10 |
Correct |
57 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
1528 KB |
Output is correct |
2 |
Correct |
41 ms |
1528 KB |
Output is correct |
3 |
Correct |
42 ms |
1528 KB |
Output is correct |
4 |
Correct |
40 ms |
1532 KB |
Output is correct |
5 |
Correct |
55 ms |
1912 KB |
Output is correct |
6 |
Correct |
42 ms |
1528 KB |
Output is correct |
7 |
Correct |
40 ms |
1536 KB |
Output is correct |
8 |
Correct |
57 ms |
1912 KB |
Output is correct |
9 |
Correct |
57 ms |
1912 KB |
Output is correct |
10 |
Correct |
40 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
13560 KB |
Output is correct |
2 |
Correct |
246 ms |
13432 KB |
Output is correct |
3 |
Correct |
240 ms |
13432 KB |
Output is correct |
4 |
Correct |
247 ms |
13304 KB |
Output is correct |
5 |
Correct |
238 ms |
13432 KB |
Output is correct |
6 |
Correct |
245 ms |
13560 KB |
Output is correct |
7 |
Correct |
241 ms |
13692 KB |
Output is correct |
8 |
Correct |
237 ms |
13432 KB |
Output is correct |
9 |
Correct |
251 ms |
13696 KB |
Output is correct |
10 |
Correct |
248 ms |
13652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
2040 KB |
Output is correct |
2 |
Correct |
57 ms |
1920 KB |
Output is correct |
3 |
Correct |
56 ms |
1912 KB |
Output is correct |
4 |
Correct |
58 ms |
2040 KB |
Output is correct |
5 |
Correct |
65 ms |
2040 KB |
Output is correct |
6 |
Correct |
56 ms |
1912 KB |
Output is correct |
7 |
Correct |
55 ms |
1912 KB |
Output is correct |
8 |
Correct |
56 ms |
1912 KB |
Output is correct |
9 |
Correct |
56 ms |
2040 KB |
Output is correct |
10 |
Correct |
57 ms |
1912 KB |
Output is correct |
11 |
Correct |
44 ms |
1528 KB |
Output is correct |
12 |
Correct |
41 ms |
1528 KB |
Output is correct |
13 |
Correct |
42 ms |
1528 KB |
Output is correct |
14 |
Correct |
40 ms |
1532 KB |
Output is correct |
15 |
Correct |
55 ms |
1912 KB |
Output is correct |
16 |
Correct |
42 ms |
1528 KB |
Output is correct |
17 |
Correct |
40 ms |
1536 KB |
Output is correct |
18 |
Correct |
57 ms |
1912 KB |
Output is correct |
19 |
Correct |
57 ms |
1912 KB |
Output is correct |
20 |
Correct |
40 ms |
1528 KB |
Output is correct |
21 |
Correct |
251 ms |
13560 KB |
Output is correct |
22 |
Correct |
246 ms |
13432 KB |
Output is correct |
23 |
Correct |
240 ms |
13432 KB |
Output is correct |
24 |
Correct |
247 ms |
13304 KB |
Output is correct |
25 |
Correct |
238 ms |
13432 KB |
Output is correct |
26 |
Correct |
245 ms |
13560 KB |
Output is correct |
27 |
Correct |
241 ms |
13692 KB |
Output is correct |
28 |
Correct |
237 ms |
13432 KB |
Output is correct |
29 |
Correct |
251 ms |
13696 KB |
Output is correct |
30 |
Correct |
248 ms |
13652 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
4 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
4 ms |
384 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
4 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
5 ms |
512 KB |
Output is correct |
50 |
Correct |
5 ms |
384 KB |
Output is correct |
51 |
Correct |
6 ms |
384 KB |
Output is correct |
52 |
Correct |
5 ms |
512 KB |
Output is correct |
53 |
Correct |
5 ms |
384 KB |
Output is correct |
54 |
Correct |
5 ms |
384 KB |
Output is correct |
55 |
Correct |
5 ms |
384 KB |
Output is correct |
56 |
Correct |
5 ms |
384 KB |
Output is correct |
57 |
Correct |
6 ms |
384 KB |
Output is correct |
58 |
Correct |
5 ms |
384 KB |
Output is correct |
59 |
Correct |
6 ms |
384 KB |
Output is correct |
60 |
Correct |
6 ms |
384 KB |
Output is correct |
61 |
Correct |
175 ms |
14712 KB |
Output is correct |
62 |
Correct |
163 ms |
15036 KB |
Output is correct |
63 |
Correct |
249 ms |
14584 KB |
Output is correct |
64 |
Correct |
183 ms |
14968 KB |
Output is correct |
65 |
Correct |
222 ms |
15012 KB |
Output is correct |
66 |
Correct |
218 ms |
14840 KB |
Output is correct |
67 |
Correct |
207 ms |
14840 KB |
Output is correct |
68 |
Correct |
247 ms |
15012 KB |
Output is correct |
69 |
Correct |
239 ms |
14656 KB |
Output is correct |
70 |
Correct |
244 ms |
14716 KB |
Output is correct |