#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;
const ll N = 3e5 + 10;
const ll INF = 1e18;
struct dsu {
ll n = 0;
vector<ll> parent = {};
void resize(ll _n) {
n = _n;
parent.resize(n + 10); for(ll i = 1; i <= n; ++ i) parent[i] = i;
}
dsu() {};
dsu(ll _n) { this -> resize(_n); };
ll ancestor(ll u) {
if(parent[u] == u) return u;
return parent[u] = ancestor(parent[u]);
}
bool join(ll u, ll v) {
u = ancestor(u); v = ancestor(v);
if(u == v) return false;
parent[v] = u;
return true;
}
};
struct state {
ll val = 0, idx = 0;
state() {};
state(ll _val, ll _idx) : val(_val), idx(_idx) {};
bool operator < (const state& other) const {
return abs(this -> val) > abs(other.val);
}
};
ll n, k;
ll a[N];
ll nxt[N];
ll res;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> k;
for(ll i = 1; i <= n; ++ i) cin >> a[i];
vector<ll> List = {};
for(ll i = 1, j = 1; i <= n; i = j) {
ll s = 0;
while(j <= n && a[i] * a[j] >= 0) {
s += a[j];
++ j;
}
// cout << s << '\n';
List.push_back(s);
}
if(!List.empty() && List.back() < 0) List.pop_back();
if(!List.empty() && List.front() < 0) {
reverse(List.begin(), List.end());
List.pop_back();
}
n = (ll) List.size();
ll have_p = 0;
priority_queue<state> pq = {};
for(ll i = 1; i <= n; ++ i) {
a[i] = List[i - 1];
nxt[i] = i + 1;
if(a[i] >= 0) {
++ have_p;
}
pq.push(state(a[i], i));
}
dsu dsu(n);
while(!pq.empty() && have_p > k) {
ll val = pq.top().val, idx = pq.top().idx;
pq.pop();
if(val != a[idx]) continue;
if(val >= 0) -- have_p;
if(idx > 1) {
ll le = dsu.ancestor(idx - 1);
val += a[le];
if(a[le] >= 0) -- have_p;
dsu.join(le, idx);
a[idx] = - INF; nxt[le] = nxt[idx]; idx = le;
}
ll ri = nxt[idx];
if(ri <= n) {
val += a[ri];
if(a[ri] >= 0) -- have_p;
dsu.join(idx, ri);
a[ri] = - INF; nxt[idx] = nxt[ri];
}
if(val >= 0) ++ have_p;
a[idx] = val;
if(val < 0 && (idx == 1 || nxt[idx] == n + 1)) continue;
pq.push(state(val, idx));
}
ll res = 0;
while(!pq.empty()) {
ll val = pq.top().val, idx = pq.top().idx;
pq.pop();
if(val < 0 || val != a[idx]) continue;
// cout << val << ' ' << idx << '\n';
res += val;
}
cout << res;
return (0 ^ 0);
}
/// Code by vuavisao
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5320 KB |
Output is correct |
2 |
Correct |
25 ms |
5488 KB |
Output is correct |
3 |
Correct |
26 ms |
5504 KB |
Output is correct |
4 |
Correct |
24 ms |
5452 KB |
Output is correct |
5 |
Correct |
25 ms |
5368 KB |
Output is correct |
6 |
Correct |
23 ms |
5288 KB |
Output is correct |
7 |
Correct |
23 ms |
5324 KB |
Output is correct |
8 |
Correct |
25 ms |
5452 KB |
Output is correct |
9 |
Correct |
23 ms |
5316 KB |
Output is correct |
10 |
Correct |
24 ms |
4536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3192 KB |
Output is correct |
2 |
Correct |
15 ms |
2660 KB |
Output is correct |
3 |
Correct |
17 ms |
2536 KB |
Output is correct |
4 |
Correct |
17 ms |
2520 KB |
Output is correct |
5 |
Correct |
23 ms |
2568 KB |
Output is correct |
6 |
Correct |
15 ms |
2524 KB |
Output is correct |
7 |
Correct |
15 ms |
2556 KB |
Output is correct |
8 |
Correct |
24 ms |
2644 KB |
Output is correct |
9 |
Correct |
23 ms |
2516 KB |
Output is correct |
10 |
Correct |
16 ms |
2612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
9016 KB |
Output is correct |
2 |
Correct |
64 ms |
8916 KB |
Output is correct |
3 |
Correct |
67 ms |
9032 KB |
Output is correct |
4 |
Correct |
63 ms |
9032 KB |
Output is correct |
5 |
Correct |
76 ms |
9076 KB |
Output is correct |
6 |
Correct |
70 ms |
9072 KB |
Output is correct |
7 |
Correct |
65 ms |
9032 KB |
Output is correct |
8 |
Correct |
68 ms |
9008 KB |
Output is correct |
9 |
Correct |
73 ms |
9032 KB |
Output is correct |
10 |
Correct |
67 ms |
9080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 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 |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 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 |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5320 KB |
Output is correct |
2 |
Correct |
25 ms |
5488 KB |
Output is correct |
3 |
Correct |
26 ms |
5504 KB |
Output is correct |
4 |
Correct |
24 ms |
5452 KB |
Output is correct |
5 |
Correct |
25 ms |
5368 KB |
Output is correct |
6 |
Correct |
23 ms |
5288 KB |
Output is correct |
7 |
Correct |
23 ms |
5324 KB |
Output is correct |
8 |
Correct |
25 ms |
5452 KB |
Output is correct |
9 |
Correct |
23 ms |
5316 KB |
Output is correct |
10 |
Correct |
24 ms |
4536 KB |
Output is correct |
11 |
Correct |
15 ms |
3192 KB |
Output is correct |
12 |
Correct |
15 ms |
2660 KB |
Output is correct |
13 |
Correct |
17 ms |
2536 KB |
Output is correct |
14 |
Correct |
17 ms |
2520 KB |
Output is correct |
15 |
Correct |
23 ms |
2568 KB |
Output is correct |
16 |
Correct |
15 ms |
2524 KB |
Output is correct |
17 |
Correct |
15 ms |
2556 KB |
Output is correct |
18 |
Correct |
24 ms |
2644 KB |
Output is correct |
19 |
Correct |
23 ms |
2516 KB |
Output is correct |
20 |
Correct |
16 ms |
2612 KB |
Output is correct |
21 |
Correct |
65 ms |
9016 KB |
Output is correct |
22 |
Correct |
64 ms |
8916 KB |
Output is correct |
23 |
Correct |
67 ms |
9032 KB |
Output is correct |
24 |
Correct |
63 ms |
9032 KB |
Output is correct |
25 |
Correct |
76 ms |
9076 KB |
Output is correct |
26 |
Correct |
70 ms |
9072 KB |
Output is correct |
27 |
Correct |
65 ms |
9032 KB |
Output is correct |
28 |
Correct |
68 ms |
9008 KB |
Output is correct |
29 |
Correct |
73 ms |
9032 KB |
Output is correct |
30 |
Correct |
67 ms |
9080 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
324 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
0 ms |
340 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
336 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 |
2 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 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 |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
53 ms |
11972 KB |
Output is correct |
62 |
Correct |
53 ms |
12064 KB |
Output is correct |
63 |
Correct |
65 ms |
11976 KB |
Output is correct |
64 |
Correct |
56 ms |
12104 KB |
Output is correct |
65 |
Correct |
59 ms |
12152 KB |
Output is correct |
66 |
Correct |
68 ms |
12076 KB |
Output is correct |
67 |
Correct |
60 ms |
11984 KB |
Output is correct |
68 |
Correct |
69 ms |
12080 KB |
Output is correct |
69 |
Correct |
81 ms |
11904 KB |
Output is correct |
70 |
Correct |
64 ms |
11916 KB |
Output is correct |