#include <bits/stdc++.h>
#define int long long
using namespace std;
const int O = 3e5 + 5;
const int inf = 1e18;
int n, k, a[O], lazy[O * 4];
struct Node{
int sum, l, r, w, pref, suf, lsuf, rpref;
} tree[O * 4], rtree[O * 4];
Node Merg(Node x, Node y){
Node root; root.w = -inf;
root.sum = x.sum + y.sum;
root.pref = x.pref;
root.rpref = x.rpref;
if (x.sum + y.pref > root.pref){
root.pref = x.sum + y.pref;
root.rpref = y.rpref;
}
root.suf = y.suf;
root.lsuf = y.lsuf;
if (y.sum + x.suf > root.suf){
root.suf = y.sum + x.suf;
root.lsuf = x.lsuf;
}
if (x.w > root.w){
root.w = x.w;
root.l = x.l;
root.r = x.r;
}
if (y.w > root.w){
root.w = y.w;
root.l = y.l;
root.r = y.r;
}
if (x.suf + y.pref >= root.w){
root.w = x.suf + y.pref;
root.l = x.lsuf;
root.r = y.rpref;
}
return root;
}
void Push(int id){
int &x = lazy[id];
if (x == 0) return;
for (int i = 2 * id; i <= 2 * id + 1; ++ i){
swap(tree[i], rtree[i]);
lazy[i] ^= x;
}
x = 0; return;
}
void Build(int id, int l, int r){
if (l == r){
tree[id] = {a[l], r, l, a[r], a[l], a[r], l, r};
rtree[id] = {-a[r], l, r, -a[l], -a[r], -a[l], r, l};
return;
}
int mid = (l + r) / 2;
Build(id << 1, l, mid);
Build(id << 1 | 1, mid + 1, r);
tree[id] = Merg(tree[id << 1], tree[id << 1 | 1]);
rtree[id] = Merg(rtree[id << 1], rtree[id << 1 | 1]);
}
void Update(int id, int l, int r, int u, int v){
if (l > v || r < u) return;
if (u <= l && r <= v){
swap(tree[id], rtree[id]);
lazy[id] ^= 1;
return;
}
int mid = (l + r) / 2; Push(id);
Update(id << 1, l, mid, u, v);
Update(id << 1 | 1, mid + 1, r, u, v);
tree[id] = Merg(tree[id << 1], tree[id << 1 | 1]);
rtree[id] = Merg(rtree[id << 1], rtree[id << 1 | 1]);
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++ i) cin >> a[i];
Build(1, 1, n);
int res = 0;
for (int i = 1; i <= k; ++ i){
if (tree[1].w <= 0) break;
res += tree[1].w;
Update(1, 1, n, tree[1].l, tree[1].r);
}
cout << res;
return 0;
}
Compilation message
feast.cpp:93:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
93 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
133928 KB |
Output is correct |
2 |
Correct |
80 ms |
133884 KB |
Output is correct |
3 |
Correct |
81 ms |
133916 KB |
Output is correct |
4 |
Correct |
80 ms |
133956 KB |
Output is correct |
5 |
Correct |
85 ms |
133972 KB |
Output is correct |
6 |
Correct |
83 ms |
133808 KB |
Output is correct |
7 |
Correct |
89 ms |
133936 KB |
Output is correct |
8 |
Correct |
81 ms |
133880 KB |
Output is correct |
9 |
Correct |
80 ms |
133844 KB |
Output is correct |
10 |
Correct |
81 ms |
133956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
133976 KB |
Output is correct |
2 |
Correct |
73 ms |
133964 KB |
Output is correct |
3 |
Correct |
71 ms |
135060 KB |
Output is correct |
4 |
Correct |
72 ms |
135136 KB |
Output is correct |
5 |
Correct |
82 ms |
136616 KB |
Output is correct |
6 |
Correct |
71 ms |
135068 KB |
Output is correct |
7 |
Correct |
72 ms |
135152 KB |
Output is correct |
8 |
Correct |
82 ms |
136908 KB |
Output is correct |
9 |
Correct |
85 ms |
136688 KB |
Output is correct |
10 |
Correct |
73 ms |
135108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
133956 KB |
Output is correct |
2 |
Correct |
88 ms |
136912 KB |
Output is correct |
3 |
Correct |
93 ms |
136988 KB |
Output is correct |
4 |
Correct |
95 ms |
136912 KB |
Output is correct |
5 |
Correct |
84 ms |
136944 KB |
Output is correct |
6 |
Correct |
87 ms |
137096 KB |
Output is correct |
7 |
Correct |
87 ms |
137104 KB |
Output is correct |
8 |
Correct |
86 ms |
136900 KB |
Output is correct |
9 |
Correct |
86 ms |
137028 KB |
Output is correct |
10 |
Correct |
89 ms |
137068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
844 KB |
Output is correct |
27 |
Correct |
1 ms |
844 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
844 KB |
Output is correct |
30 |
Correct |
1 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
133928 KB |
Output is correct |
2 |
Correct |
80 ms |
133884 KB |
Output is correct |
3 |
Correct |
81 ms |
133916 KB |
Output is correct |
4 |
Correct |
80 ms |
133956 KB |
Output is correct |
5 |
Correct |
85 ms |
133972 KB |
Output is correct |
6 |
Correct |
83 ms |
133808 KB |
Output is correct |
7 |
Correct |
89 ms |
133936 KB |
Output is correct |
8 |
Correct |
81 ms |
133880 KB |
Output is correct |
9 |
Correct |
80 ms |
133844 KB |
Output is correct |
10 |
Correct |
81 ms |
133956 KB |
Output is correct |
11 |
Correct |
71 ms |
133976 KB |
Output is correct |
12 |
Correct |
73 ms |
133964 KB |
Output is correct |
13 |
Correct |
71 ms |
135060 KB |
Output is correct |
14 |
Correct |
72 ms |
135136 KB |
Output is correct |
15 |
Correct |
82 ms |
136616 KB |
Output is correct |
16 |
Correct |
71 ms |
135068 KB |
Output is correct |
17 |
Correct |
72 ms |
135152 KB |
Output is correct |
18 |
Correct |
82 ms |
136908 KB |
Output is correct |
19 |
Correct |
85 ms |
136688 KB |
Output is correct |
20 |
Correct |
73 ms |
135108 KB |
Output is correct |
21 |
Correct |
86 ms |
133956 KB |
Output is correct |
22 |
Correct |
88 ms |
136912 KB |
Output is correct |
23 |
Correct |
93 ms |
136988 KB |
Output is correct |
24 |
Correct |
95 ms |
136912 KB |
Output is correct |
25 |
Correct |
84 ms |
136944 KB |
Output is correct |
26 |
Correct |
87 ms |
137096 KB |
Output is correct |
27 |
Correct |
87 ms |
137104 KB |
Output is correct |
28 |
Correct |
86 ms |
136900 KB |
Output is correct |
29 |
Correct |
86 ms |
137028 KB |
Output is correct |
30 |
Correct |
89 ms |
137068 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
320 KB |
Output is correct |
35 |
Correct |
0 ms |
332 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
0 ms |
332 KB |
Output is correct |
41 |
Correct |
0 ms |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
460 KB |
Output is correct |
44 |
Correct |
0 ms |
460 KB |
Output is correct |
45 |
Correct |
1 ms |
460 KB |
Output is correct |
46 |
Correct |
0 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
460 KB |
Output is correct |
49 |
Correct |
1 ms |
460 KB |
Output is correct |
50 |
Correct |
0 ms |
460 KB |
Output is correct |
51 |
Correct |
1 ms |
844 KB |
Output is correct |
52 |
Correct |
1 ms |
852 KB |
Output is correct |
53 |
Correct |
1 ms |
844 KB |
Output is correct |
54 |
Correct |
1 ms |
844 KB |
Output is correct |
55 |
Correct |
1 ms |
844 KB |
Output is correct |
56 |
Correct |
1 ms |
844 KB |
Output is correct |
57 |
Correct |
1 ms |
844 KB |
Output is correct |
58 |
Correct |
1 ms |
844 KB |
Output is correct |
59 |
Correct |
1 ms |
844 KB |
Output is correct |
60 |
Correct |
1 ms |
844 KB |
Output is correct |
61 |
Correct |
162 ms |
145040 KB |
Output is correct |
62 |
Correct |
190 ms |
145288 KB |
Output is correct |
63 |
Correct |
101 ms |
143636 KB |
Output is correct |
64 |
Correct |
157 ms |
145096 KB |
Output is correct |
65 |
Correct |
138 ms |
145204 KB |
Output is correct |
66 |
Correct |
141 ms |
145080 KB |
Output is correct |
67 |
Correct |
133 ms |
145096 KB |
Output is correct |
68 |
Correct |
91 ms |
140100 KB |
Output is correct |
69 |
Correct |
93 ms |
138836 KB |
Output is correct |
70 |
Correct |
93 ms |
136972 KB |
Output is correct |