#include <bits/stdc++.h>
#define int long long
using namespace std;
#define pii pair<int, int>
set<pii> nums;
set<pii> absnums;
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, k; cin >> n >> k;
bool ispos = true;
bool acceptneg = false;
int s = 0;
int idx = 0;
for (int i = 0; i < n; i++){
int g; cin >> g;
if (g > 0) {
acceptneg = true;
if (!ispos){
nums.insert({idx, s});
absnums.insert({llabs(s), idx});
s = 0;
ispos = true;
idx++;
}
s += g;
} else {
if (!acceptneg) continue;
if (ispos){
nums.insert({idx, s});
absnums.insert({llabs(s), idx});
s = 0;
ispos = false;
idx++;
}
s += g;
}
}
if (s > 0){
nums.insert({idx, s});
absnums.insert({s, idx});
}
while (nums.size() / 2 + 1 > k){
auto [s, idx] = *absnums.begin(); absnums.erase(absnums.begin());
auto itr = nums.lower_bound({idx - 1, 1e18});
auto [curridx, val] = *itr;
itr++;
if (itr != nums.end()){
auto [idx, nval] = *itr;
val += nval;
itr--;
absnums.erase({llabs(nval), idx});
nums.erase({idx, nval});
} else {
itr--;
}
if (itr != nums.begin()){
itr--;
auto [idx, pval] = *itr;
val += pval;
itr++;
absnums.erase({llabs(pval), idx});
nums.erase({idx, pval});
}
nums.erase(itr);
nums.insert({curridx, val});
if (val <= 0){
auto itr = nums.find({curridx, val});
if (itr == nums.begin()){
nums.erase(itr);
} else if ((++itr) == nums.end()){
--itr;
nums.erase(itr);
} else {
absnums.insert({llabs(val), curridx});
}
} else {
absnums.insert({llabs(val), curridx});
}
}
int ans = 0;
for (auto [idx, num] : nums){
if (num > 0) ans += num;
}
cout << ans << '\n';
}
Compilation message
feast.cpp: In function 'int32_t main()':
feast.cpp:42:32: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
42 | while (nums.size() / 2 + 1 > k){
| ~~~~~~~~~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 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 |
364 KB |
Output is correct |
5 |
Correct |
33 ms |
364 KB |
Output is correct |
6 |
Correct |
32 ms |
364 KB |
Output is correct |
7 |
Correct |
34 ms |
364 KB |
Output is correct |
8 |
Correct |
32 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 |
364 KB |
Output is correct |
2 |
Correct |
22 ms |
364 KB |
Output is correct |
3 |
Correct |
22 ms |
492 KB |
Output is correct |
4 |
Correct |
23 ms |
1516 KB |
Output is correct |
5 |
Correct |
34 ms |
3180 KB |
Output is correct |
6 |
Correct |
22 ms |
1516 KB |
Output is correct |
7 |
Correct |
25 ms |
1516 KB |
Output is correct |
8 |
Correct |
34 ms |
3180 KB |
Output is correct |
9 |
Correct |
33 ms |
3200 KB |
Output is correct |
10 |
Correct |
22 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
469 ms |
18924 KB |
Output is correct |
2 |
Correct |
479 ms |
21760 KB |
Output is correct |
3 |
Correct |
445 ms |
21884 KB |
Output is correct |
4 |
Correct |
449 ms |
21724 KB |
Output is correct |
5 |
Correct |
446 ms |
21792 KB |
Output is correct |
6 |
Correct |
458 ms |
21996 KB |
Output is correct |
7 |
Correct |
472 ms |
22124 KB |
Output is correct |
8 |
Correct |
445 ms |
21828 KB |
Output is correct |
9 |
Correct |
450 ms |
22380 KB |
Output is correct |
10 |
Correct |
459 ms |
22124 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 |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
2 ms |
492 KB |
Output is correct |
28 |
Correct |
2 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 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 |
364 KB |
Output is correct |
5 |
Correct |
33 ms |
364 KB |
Output is correct |
6 |
Correct |
32 ms |
364 KB |
Output is correct |
7 |
Correct |
34 ms |
364 KB |
Output is correct |
8 |
Correct |
32 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 |
364 KB |
Output is correct |
12 |
Correct |
22 ms |
364 KB |
Output is correct |
13 |
Correct |
22 ms |
492 KB |
Output is correct |
14 |
Correct |
23 ms |
1516 KB |
Output is correct |
15 |
Correct |
34 ms |
3180 KB |
Output is correct |
16 |
Correct |
22 ms |
1516 KB |
Output is correct |
17 |
Correct |
25 ms |
1516 KB |
Output is correct |
18 |
Correct |
34 ms |
3180 KB |
Output is correct |
19 |
Correct |
33 ms |
3200 KB |
Output is correct |
20 |
Correct |
22 ms |
1516 KB |
Output is correct |
21 |
Correct |
469 ms |
18924 KB |
Output is correct |
22 |
Correct |
479 ms |
21760 KB |
Output is correct |
23 |
Correct |
445 ms |
21884 KB |
Output is correct |
24 |
Correct |
449 ms |
21724 KB |
Output is correct |
25 |
Correct |
446 ms |
21792 KB |
Output is correct |
26 |
Correct |
458 ms |
21996 KB |
Output is correct |
27 |
Correct |
472 ms |
22124 KB |
Output is correct |
28 |
Correct |
445 ms |
21828 KB |
Output is correct |
29 |
Correct |
450 ms |
22380 KB |
Output is correct |
30 |
Correct |
459 ms |
22124 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 |
1 ms |
492 KB |
Output is correct |
53 |
Correct |
1 ms |
492 KB |
Output is correct |
54 |
Correct |
2 ms |
492 KB |
Output is correct |
55 |
Correct |
2 ms |
492 KB |
Output is correct |
56 |
Correct |
2 ms |
492 KB |
Output is correct |
57 |
Correct |
2 ms |
492 KB |
Output is correct |
58 |
Correct |
2 ms |
492 KB |
Output is correct |
59 |
Correct |
2 ms |
492 KB |
Output is correct |
60 |
Correct |
2 ms |
492 KB |
Output is correct |
61 |
Correct |
290 ms |
21484 KB |
Output is correct |
62 |
Correct |
213 ms |
22252 KB |
Output is correct |
63 |
Correct |
448 ms |
21484 KB |
Output is correct |
64 |
Correct |
308 ms |
22124 KB |
Output is correct |
65 |
Correct |
372 ms |
21996 KB |
Output is correct |
66 |
Correct |
396 ms |
21868 KB |
Output is correct |
67 |
Correct |
377 ms |
21612 KB |
Output is correct |
68 |
Correct |
490 ms |
21996 KB |
Output is correct |
69 |
Correct |
461 ms |
21612 KB |
Output is correct |
70 |
Correct |
448 ms |
21536 KB |
Output is correct |