#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 3e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
struct block {
int l, r;
ll sum;
} b[MAX_N];
int num = 0;
void solve() {
int n, k; cin >> n >> k;
vector<int> a(n);
for (int &x : a) cin >> x;
for (int i = 0; i < n; i++) {
b[num] = {num - 1, num + 1, 0};
int j = i;
while (j < n && (ll)a[i] * a[j] >= 0) b[num].sum += a[j++];
i = j - 1;
if (!(b[num].sum <= 0 && num == 0)) num++;
}
if (num > 0 && b[num - 1].sum <= 0) num--;
if (num == 0) {
cout << 0 << "\n";
return;
}
b[0].l = b[num - 1].r = -1;
int cnt = num / 2 + 1;
priority_queue<ar<ll,2>, vector<ar<ll,2>>, greater<ar<ll,2>>> pq;
for (int i = 0; i < num; i++) pq.push({abs(b[i].sum), i});
vector<bool> vis(num);
while (cnt > k) {
auto [v, i] = pq.top(); pq.pop();
if (vis[i]) continue;
vis[i] = true;
cnt--;
if (b[i].l == -1) {
vis[b[i].r] = true;
b[b[b[i].r].r].l = -1;
}
else if (b[i].r == -1) {
vis[b[i].l] = true;
b[b[b[i].l].l].r = -1;
}
else {
vis[b[i].r] = vis[b[i].l] = true;
b[i].sum += b[b[i].l].sum + b[b[i].r].sum;
if (b[b[i].l].l != -1) b[b[b[i].l].l].r = i;
if (b[b[i].r].r != -1) b[b[b[i].r].r].l = i;
b[i].l = b[b[i].l].l;
b[i].r = b[b[i].r].r;
vis[i] = 0;
pq.push({abs(b[i].sum), i});
}
}
ll ans = 0;
for (int i = 0; i < num; i++) {
if (vis[i] || b[i].sum <= 0) continue;
ans += b[i].sum;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
4204 KB |
Output is correct |
2 |
Correct |
35 ms |
4332 KB |
Output is correct |
3 |
Correct |
35 ms |
4332 KB |
Output is correct |
4 |
Correct |
35 ms |
4332 KB |
Output is correct |
5 |
Correct |
35 ms |
4332 KB |
Output is correct |
6 |
Correct |
35 ms |
4332 KB |
Output is correct |
7 |
Correct |
36 ms |
4332 KB |
Output is correct |
8 |
Correct |
35 ms |
4332 KB |
Output is correct |
9 |
Correct |
35 ms |
4332 KB |
Output is correct |
10 |
Correct |
35 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
1516 KB |
Output is correct |
2 |
Correct |
23 ms |
2668 KB |
Output is correct |
3 |
Correct |
23 ms |
2540 KB |
Output is correct |
4 |
Correct |
27 ms |
2668 KB |
Output is correct |
5 |
Correct |
34 ms |
4332 KB |
Output is correct |
6 |
Correct |
23 ms |
2540 KB |
Output is correct |
7 |
Correct |
23 ms |
2668 KB |
Output is correct |
8 |
Correct |
35 ms |
4332 KB |
Output is correct |
9 |
Correct |
34 ms |
4204 KB |
Output is correct |
10 |
Correct |
23 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
11108 KB |
Output is correct |
2 |
Correct |
93 ms |
10980 KB |
Output is correct |
3 |
Correct |
93 ms |
11108 KB |
Output is correct |
4 |
Correct |
94 ms |
10992 KB |
Output is correct |
5 |
Correct |
95 ms |
11108 KB |
Output is correct |
6 |
Correct |
98 ms |
11108 KB |
Output is correct |
7 |
Correct |
105 ms |
11108 KB |
Output is correct |
8 |
Correct |
95 ms |
11108 KB |
Output is correct |
9 |
Correct |
95 ms |
11108 KB |
Output is correct |
10 |
Correct |
103 ms |
11108 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 |
384 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 |
384 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 |
384 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 |
384 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 |
384 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 |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
4204 KB |
Output is correct |
2 |
Correct |
35 ms |
4332 KB |
Output is correct |
3 |
Correct |
35 ms |
4332 KB |
Output is correct |
4 |
Correct |
35 ms |
4332 KB |
Output is correct |
5 |
Correct |
35 ms |
4332 KB |
Output is correct |
6 |
Correct |
35 ms |
4332 KB |
Output is correct |
7 |
Correct |
36 ms |
4332 KB |
Output is correct |
8 |
Correct |
35 ms |
4332 KB |
Output is correct |
9 |
Correct |
35 ms |
4332 KB |
Output is correct |
10 |
Correct |
35 ms |
4332 KB |
Output is correct |
11 |
Correct |
22 ms |
1516 KB |
Output is correct |
12 |
Correct |
23 ms |
2668 KB |
Output is correct |
13 |
Correct |
23 ms |
2540 KB |
Output is correct |
14 |
Correct |
27 ms |
2668 KB |
Output is correct |
15 |
Correct |
34 ms |
4332 KB |
Output is correct |
16 |
Correct |
23 ms |
2540 KB |
Output is correct |
17 |
Correct |
23 ms |
2668 KB |
Output is correct |
18 |
Correct |
35 ms |
4332 KB |
Output is correct |
19 |
Correct |
34 ms |
4204 KB |
Output is correct |
20 |
Correct |
23 ms |
2668 KB |
Output is correct |
21 |
Correct |
97 ms |
11108 KB |
Output is correct |
22 |
Correct |
93 ms |
10980 KB |
Output is correct |
23 |
Correct |
93 ms |
11108 KB |
Output is correct |
24 |
Correct |
94 ms |
10992 KB |
Output is correct |
25 |
Correct |
95 ms |
11108 KB |
Output is correct |
26 |
Correct |
98 ms |
11108 KB |
Output is correct |
27 |
Correct |
105 ms |
11108 KB |
Output is correct |
28 |
Correct |
95 ms |
11108 KB |
Output is correct |
29 |
Correct |
95 ms |
11108 KB |
Output is correct |
30 |
Correct |
103 ms |
11108 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 |
384 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 |
384 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 |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
364 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
1 ms |
364 KB |
Output is correct |
55 |
Correct |
1 ms |
492 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
55 ms |
10980 KB |
Output is correct |
62 |
Correct |
49 ms |
11108 KB |
Output is correct |
63 |
Correct |
82 ms |
10980 KB |
Output is correct |
64 |
Correct |
56 ms |
11108 KB |
Output is correct |
65 |
Correct |
65 ms |
11108 KB |
Output is correct |
66 |
Correct |
66 ms |
11108 KB |
Output is correct |
67 |
Correct |
63 ms |
10980 KB |
Output is correct |
68 |
Correct |
94 ms |
11140 KB |
Output is correct |
69 |
Correct |
91 ms |
10980 KB |
Output is correct |
70 |
Correct |
92 ms |
11108 KB |
Output is correct |