#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1, MOD = 998244353;
typedef long long ll;
#define int long long
int n, k, a[N],p[N];
bool ok[N];
vector<ll> b, sum(N);
set<int> pos;
set<pair<ll,ll>> st;
ll res = 0,col = 0;
void del(int i){
if(i >= 0 && i < n){
st.erase({abs(sum[i]),i});
pos.erase(i);
ok[i] = 1;
if(sum[i] >= 0){
res -= sum[i];
col--;
}
}
}
void test()
{
cin >> n >> k;
ll lst = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
ok[i] = (a[i] >= 0);
}
ll cur = 0;
ok[0] = 1;
for (int i = 1; i <= n; i++)
{
if (ok[i] == ok[i - 1])
{
cur += a[i];
}
else
{
if(b.empty() && cur < 0){
}
else b.push_back(cur);
cur = a[i];
}
}
if(cur >= 0) b.push_back(cur);
n = (int)b.size();
for(int i = 0;i < n;i++){
p[i] = i;
sum[i] = b[i];
if(b[i] >= 0){
col++;
res += b[i];
}
pos.insert(i);
st.insert({abs(b[i]),i});
}
int it = 0;
while(col > k){
auto [x,y] = (*st.begin());
st.erase({x,y});
// cout << x << ' ' << y << ' ' << res << ' ' << col << '\n';
auto it = pos.upper_bound(y);
ll nv = sum[y];
del(y);
if(it != pos.end()){
nv += sum[*it];
del(*it);
}
auto it1 = pos.lower_bound(y);
if(it1 != pos.begin()){
it1--;
nv += sum[*it1];
del(*it1);
}
sum[y] = nv;
if(sum[y] >= 0){
pos.insert(y);
st.insert({abs(sum[y]),y});
col++;
res += sum[y];
}else{
if(!pos.empty() && *pos.begin() < y && *pos.rbegin() > y){
pos.insert(y);
st.insert({abs(sum[y]),y});
}
}
}
cout << res;
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1; // cin >> T;
while (T--)
test();
}
Compilation message
feast.cpp: In function 'void test()':
feast.cpp:26:8: warning: unused variable 'lst' [-Wunused-variable]
26 | ll lst = 0;
| ^~~
feast.cpp:60:9: warning: unused variable 'it' [-Wunused-variable]
60 | int it = 0;
| ^~
feast.cpp: At global scope:
feast.cpp:93:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
93 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
8796 KB |
Output is correct |
2 |
Correct |
23 ms |
8796 KB |
Output is correct |
3 |
Correct |
24 ms |
9052 KB |
Output is correct |
4 |
Correct |
23 ms |
9052 KB |
Output is correct |
5 |
Correct |
23 ms |
8912 KB |
Output is correct |
6 |
Correct |
24 ms |
8764 KB |
Output is correct |
7 |
Correct |
23 ms |
8796 KB |
Output is correct |
8 |
Correct |
24 ms |
8908 KB |
Output is correct |
9 |
Correct |
23 ms |
8844 KB |
Output is correct |
10 |
Correct |
24 ms |
9012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
8792 KB |
Output is correct |
2 |
Correct |
15 ms |
9304 KB |
Output is correct |
3 |
Correct |
16 ms |
8796 KB |
Output is correct |
4 |
Correct |
16 ms |
8796 KB |
Output is correct |
5 |
Correct |
23 ms |
8924 KB |
Output is correct |
6 |
Correct |
15 ms |
8796 KB |
Output is correct |
7 |
Correct |
16 ms |
8924 KB |
Output is correct |
8 |
Correct |
28 ms |
8792 KB |
Output is correct |
9 |
Correct |
23 ms |
8796 KB |
Output is correct |
10 |
Correct |
17 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
26104 KB |
Output is correct |
2 |
Correct |
266 ms |
26088 KB |
Output is correct |
3 |
Correct |
282 ms |
26188 KB |
Output is correct |
4 |
Correct |
248 ms |
25920 KB |
Output is correct |
5 |
Correct |
291 ms |
26020 KB |
Output is correct |
6 |
Correct |
282 ms |
26208 KB |
Output is correct |
7 |
Correct |
275 ms |
26568 KB |
Output is correct |
8 |
Correct |
255 ms |
26060 KB |
Output is correct |
9 |
Correct |
259 ms |
26388 KB |
Output is correct |
10 |
Correct |
288 ms |
26460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
1 ms |
8484 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8280 KB |
Output is correct |
8 |
Correct |
1 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8284 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
1 ms |
8484 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8280 KB |
Output is correct |
8 |
Correct |
1 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8284 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8284 KB |
Output is correct |
12 |
Correct |
2 ms |
8280 KB |
Output is correct |
13 |
Correct |
2 ms |
8284 KB |
Output is correct |
14 |
Correct |
2 ms |
8496 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
2 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8284 KB |
Output is correct |
20 |
Correct |
2 ms |
8516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
1 ms |
8484 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8280 KB |
Output is correct |
8 |
Correct |
1 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8284 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8284 KB |
Output is correct |
12 |
Correct |
2 ms |
8280 KB |
Output is correct |
13 |
Correct |
2 ms |
8284 KB |
Output is correct |
14 |
Correct |
2 ms |
8496 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
2 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8284 KB |
Output is correct |
20 |
Correct |
2 ms |
8516 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
2 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
3 ms |
8496 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8536 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
8796 KB |
Output is correct |
2 |
Correct |
23 ms |
8796 KB |
Output is correct |
3 |
Correct |
24 ms |
9052 KB |
Output is correct |
4 |
Correct |
23 ms |
9052 KB |
Output is correct |
5 |
Correct |
23 ms |
8912 KB |
Output is correct |
6 |
Correct |
24 ms |
8764 KB |
Output is correct |
7 |
Correct |
23 ms |
8796 KB |
Output is correct |
8 |
Correct |
24 ms |
8908 KB |
Output is correct |
9 |
Correct |
23 ms |
8844 KB |
Output is correct |
10 |
Correct |
24 ms |
9012 KB |
Output is correct |
11 |
Correct |
16 ms |
8792 KB |
Output is correct |
12 |
Correct |
15 ms |
9304 KB |
Output is correct |
13 |
Correct |
16 ms |
8796 KB |
Output is correct |
14 |
Correct |
16 ms |
8796 KB |
Output is correct |
15 |
Correct |
23 ms |
8924 KB |
Output is correct |
16 |
Correct |
15 ms |
8796 KB |
Output is correct |
17 |
Correct |
16 ms |
8924 KB |
Output is correct |
18 |
Correct |
28 ms |
8792 KB |
Output is correct |
19 |
Correct |
23 ms |
8796 KB |
Output is correct |
20 |
Correct |
17 ms |
9048 KB |
Output is correct |
21 |
Correct |
260 ms |
26104 KB |
Output is correct |
22 |
Correct |
266 ms |
26088 KB |
Output is correct |
23 |
Correct |
282 ms |
26188 KB |
Output is correct |
24 |
Correct |
248 ms |
25920 KB |
Output is correct |
25 |
Correct |
291 ms |
26020 KB |
Output is correct |
26 |
Correct |
282 ms |
26208 KB |
Output is correct |
27 |
Correct |
275 ms |
26568 KB |
Output is correct |
28 |
Correct |
255 ms |
26060 KB |
Output is correct |
29 |
Correct |
259 ms |
26388 KB |
Output is correct |
30 |
Correct |
288 ms |
26460 KB |
Output is correct |
31 |
Correct |
2 ms |
8284 KB |
Output is correct |
32 |
Correct |
1 ms |
8484 KB |
Output is correct |
33 |
Correct |
2 ms |
8284 KB |
Output is correct |
34 |
Correct |
2 ms |
8284 KB |
Output is correct |
35 |
Correct |
2 ms |
8284 KB |
Output is correct |
36 |
Correct |
2 ms |
8284 KB |
Output is correct |
37 |
Correct |
2 ms |
8280 KB |
Output is correct |
38 |
Correct |
1 ms |
8284 KB |
Output is correct |
39 |
Correct |
2 ms |
8284 KB |
Output is correct |
40 |
Correct |
2 ms |
8284 KB |
Output is correct |
41 |
Correct |
2 ms |
8284 KB |
Output is correct |
42 |
Correct |
2 ms |
8280 KB |
Output is correct |
43 |
Correct |
2 ms |
8284 KB |
Output is correct |
44 |
Correct |
2 ms |
8496 KB |
Output is correct |
45 |
Correct |
2 ms |
8284 KB |
Output is correct |
46 |
Correct |
2 ms |
8284 KB |
Output is correct |
47 |
Correct |
2 ms |
8284 KB |
Output is correct |
48 |
Correct |
2 ms |
8284 KB |
Output is correct |
49 |
Correct |
2 ms |
8284 KB |
Output is correct |
50 |
Correct |
2 ms |
8516 KB |
Output is correct |
51 |
Correct |
2 ms |
8540 KB |
Output is correct |
52 |
Correct |
2 ms |
8540 KB |
Output is correct |
53 |
Correct |
2 ms |
8540 KB |
Output is correct |
54 |
Correct |
2 ms |
8540 KB |
Output is correct |
55 |
Correct |
2 ms |
8540 KB |
Output is correct |
56 |
Correct |
2 ms |
8540 KB |
Output is correct |
57 |
Correct |
3 ms |
8496 KB |
Output is correct |
58 |
Correct |
2 ms |
8540 KB |
Output is correct |
59 |
Correct |
2 ms |
8536 KB |
Output is correct |
60 |
Correct |
4 ms |
8540 KB |
Output is correct |
61 |
Correct |
154 ms |
28884 KB |
Output is correct |
62 |
Correct |
109 ms |
29552 KB |
Output is correct |
63 |
Correct |
257 ms |
28876 KB |
Output is correct |
64 |
Correct |
158 ms |
29388 KB |
Output is correct |
65 |
Correct |
212 ms |
29388 KB |
Output is correct |
66 |
Correct |
214 ms |
29236 KB |
Output is correct |
67 |
Correct |
203 ms |
28876 KB |
Output is correct |
68 |
Correct |
253 ms |
29388 KB |
Output is correct |
69 |
Correct |
261 ms |
28816 KB |
Output is correct |
70 |
Correct |
241 ms |
28872 KB |
Output is correct |