#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pli = pair<ll,int>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
const ll inf = 1e18;
const int MXN = 3e5+5;
struct node{
pli l, r; // v, pos
pair<ll,pii> m;
ll a;
}t[MXN<<2], nt[MXN<<2];
bool lz[MXN];
int A[MXN];
node z = {{-inf,0},{-inf,0},{-inf,{0,0}},0};
node operator+(const node &i, const node &j){
node k;
k.l = max(i.l,{i.a+j.l.first,j.l.second});
k.r = max(j.r,{i.r.first+j.a,i.r.second});
k.m = max(i.m,j.m);
k.m = max(k.m,{i.r.first+j.l.first,{i.r.second,j.l.second}});
k.a = i.a+j.a;
return k;
}
void push(int x,int s,int e){
if(!lz[x]) return;
swap(t[x],nt[x]);
if(s!=e){
lz[x*2] ^= lz[x];
lz[x*2+1] ^= lz[x];
}
lz[x] = 0;
}
void upd(int x,int s,int e,int l,int r){
push(x,s,e);
if(e<l || r<s) return;
if(l<=s && e<=r) {
lz[x]^=1;
return push(x,s,e);
}
int mid = s+e>>1;
upd(x*2,s,mid,l,r), upd(x*2+1,mid+1,e,l,r);
t[x] = t[x*2]+t[x*2+1];
nt[x] = nt[x*2]+nt[x*2+1];
}
void init(int x,int s,int e){
if(s==e) {
ll v = A[s];
t[x] = {{v,s},{v,s},{v,{s,s}},v};
nt[x] = {{-v,s},{-v,s},{-v,{s,s}},-v};
return;
}
int mid = s+e>>1;
init(x*2,s,mid), init(x*2+1,mid+1,e);
t[x] = t[x*2]+t[x*2+1];
nt[x] = nt[x*2]+nt[x*2+1];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k; cin >> n >> k;
for(int i = 1;i<=n;i++) cin >> A[i];
init(1,1,n);
ll ans = 0;
for(int i = 0;i<k;i++){
auto [l,r] = t[1].m.second;
ll v = t[1].m.first;
if(v<=0) break;
ans += v;
upd(1,1,n,l,r);
}
cout << ans;
}
Compilation message
feast.cpp: In function 'void upd(int, int, int, int, int)':
feast.cpp:42:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int mid = s+e>>1;
| ~^~
feast.cpp: In function 'void init(int, int, int)':
feast.cpp:54:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int mid = s+e>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
136056 KB |
Output is correct |
2 |
Correct |
59 ms |
136020 KB |
Output is correct |
3 |
Correct |
59 ms |
136224 KB |
Output is correct |
4 |
Correct |
79 ms |
136204 KB |
Output is correct |
5 |
Correct |
62 ms |
136212 KB |
Output is correct |
6 |
Correct |
59 ms |
136076 KB |
Output is correct |
7 |
Correct |
56 ms |
136100 KB |
Output is correct |
8 |
Correct |
57 ms |
136016 KB |
Output is correct |
9 |
Correct |
59 ms |
136208 KB |
Output is correct |
10 |
Correct |
57 ms |
136080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
134484 KB |
Output is correct |
2 |
Correct |
51 ms |
134488 KB |
Output is correct |
3 |
Correct |
48 ms |
134280 KB |
Output is correct |
4 |
Correct |
63 ms |
134492 KB |
Output is correct |
5 |
Correct |
59 ms |
135988 KB |
Output is correct |
6 |
Correct |
50 ms |
134432 KB |
Output is correct |
7 |
Correct |
62 ms |
134536 KB |
Output is correct |
8 |
Correct |
63 ms |
136276 KB |
Output is correct |
9 |
Correct |
57 ms |
136020 KB |
Output is correct |
10 |
Correct |
51 ms |
134484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
136276 KB |
Output is correct |
2 |
Correct |
70 ms |
136212 KB |
Output is correct |
3 |
Correct |
73 ms |
136176 KB |
Output is correct |
4 |
Correct |
72 ms |
136388 KB |
Output is correct |
5 |
Correct |
61 ms |
136420 KB |
Output is correct |
6 |
Correct |
63 ms |
136440 KB |
Output is correct |
7 |
Correct |
61 ms |
136272 KB |
Output is correct |
8 |
Correct |
67 ms |
136424 KB |
Output is correct |
9 |
Correct |
62 ms |
136432 KB |
Output is correct |
10 |
Correct |
61 ms |
136448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
133520 KB |
Output is correct |
2 |
Correct |
22 ms |
133060 KB |
Output is correct |
3 |
Correct |
22 ms |
133212 KB |
Output is correct |
4 |
Correct |
25 ms |
133112 KB |
Output is correct |
5 |
Correct |
20 ms |
133212 KB |
Output is correct |
6 |
Correct |
22 ms |
133456 KB |
Output is correct |
7 |
Correct |
23 ms |
133204 KB |
Output is correct |
8 |
Correct |
20 ms |
133208 KB |
Output is correct |
9 |
Correct |
21 ms |
133212 KB |
Output is correct |
10 |
Correct |
21 ms |
133212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
133520 KB |
Output is correct |
2 |
Correct |
22 ms |
133060 KB |
Output is correct |
3 |
Correct |
22 ms |
133212 KB |
Output is correct |
4 |
Correct |
25 ms |
133112 KB |
Output is correct |
5 |
Correct |
20 ms |
133212 KB |
Output is correct |
6 |
Correct |
22 ms |
133456 KB |
Output is correct |
7 |
Correct |
23 ms |
133204 KB |
Output is correct |
8 |
Correct |
20 ms |
133208 KB |
Output is correct |
9 |
Correct |
21 ms |
133212 KB |
Output is correct |
10 |
Correct |
21 ms |
133212 KB |
Output is correct |
11 |
Correct |
22 ms |
133108 KB |
Output is correct |
12 |
Correct |
22 ms |
133120 KB |
Output is correct |
13 |
Correct |
22 ms |
133156 KB |
Output is correct |
14 |
Correct |
20 ms |
133132 KB |
Output is correct |
15 |
Correct |
22 ms |
133212 KB |
Output is correct |
16 |
Correct |
20 ms |
133212 KB |
Output is correct |
17 |
Correct |
21 ms |
133200 KB |
Output is correct |
18 |
Correct |
21 ms |
133212 KB |
Output is correct |
19 |
Correct |
21 ms |
133212 KB |
Output is correct |
20 |
Correct |
21 ms |
133068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
133520 KB |
Output is correct |
2 |
Correct |
22 ms |
133060 KB |
Output is correct |
3 |
Correct |
22 ms |
133212 KB |
Output is correct |
4 |
Correct |
25 ms |
133112 KB |
Output is correct |
5 |
Correct |
20 ms |
133212 KB |
Output is correct |
6 |
Correct |
22 ms |
133456 KB |
Output is correct |
7 |
Correct |
23 ms |
133204 KB |
Output is correct |
8 |
Correct |
20 ms |
133208 KB |
Output is correct |
9 |
Correct |
21 ms |
133212 KB |
Output is correct |
10 |
Correct |
21 ms |
133212 KB |
Output is correct |
11 |
Correct |
22 ms |
133108 KB |
Output is correct |
12 |
Correct |
22 ms |
133120 KB |
Output is correct |
13 |
Correct |
22 ms |
133156 KB |
Output is correct |
14 |
Correct |
20 ms |
133132 KB |
Output is correct |
15 |
Correct |
22 ms |
133212 KB |
Output is correct |
16 |
Correct |
20 ms |
133212 KB |
Output is correct |
17 |
Correct |
21 ms |
133200 KB |
Output is correct |
18 |
Correct |
21 ms |
133212 KB |
Output is correct |
19 |
Correct |
21 ms |
133212 KB |
Output is correct |
20 |
Correct |
21 ms |
133068 KB |
Output is correct |
21 |
Correct |
21 ms |
133212 KB |
Output is correct |
22 |
Correct |
21 ms |
133132 KB |
Output is correct |
23 |
Correct |
21 ms |
133200 KB |
Output is correct |
24 |
Correct |
21 ms |
133144 KB |
Output is correct |
25 |
Correct |
22 ms |
133284 KB |
Output is correct |
26 |
Correct |
23 ms |
133200 KB |
Output is correct |
27 |
Correct |
24 ms |
133208 KB |
Output is correct |
28 |
Correct |
22 ms |
133260 KB |
Output is correct |
29 |
Correct |
22 ms |
133132 KB |
Output is correct |
30 |
Correct |
20 ms |
133212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
136056 KB |
Output is correct |
2 |
Correct |
59 ms |
136020 KB |
Output is correct |
3 |
Correct |
59 ms |
136224 KB |
Output is correct |
4 |
Correct |
79 ms |
136204 KB |
Output is correct |
5 |
Correct |
62 ms |
136212 KB |
Output is correct |
6 |
Correct |
59 ms |
136076 KB |
Output is correct |
7 |
Correct |
56 ms |
136100 KB |
Output is correct |
8 |
Correct |
57 ms |
136016 KB |
Output is correct |
9 |
Correct |
59 ms |
136208 KB |
Output is correct |
10 |
Correct |
57 ms |
136080 KB |
Output is correct |
11 |
Correct |
51 ms |
134484 KB |
Output is correct |
12 |
Correct |
51 ms |
134488 KB |
Output is correct |
13 |
Correct |
48 ms |
134280 KB |
Output is correct |
14 |
Correct |
63 ms |
134492 KB |
Output is correct |
15 |
Correct |
59 ms |
135988 KB |
Output is correct |
16 |
Correct |
50 ms |
134432 KB |
Output is correct |
17 |
Correct |
62 ms |
134536 KB |
Output is correct |
18 |
Correct |
63 ms |
136276 KB |
Output is correct |
19 |
Correct |
57 ms |
136020 KB |
Output is correct |
20 |
Correct |
51 ms |
134484 KB |
Output is correct |
21 |
Correct |
62 ms |
136276 KB |
Output is correct |
22 |
Correct |
70 ms |
136212 KB |
Output is correct |
23 |
Correct |
73 ms |
136176 KB |
Output is correct |
24 |
Correct |
72 ms |
136388 KB |
Output is correct |
25 |
Correct |
61 ms |
136420 KB |
Output is correct |
26 |
Correct |
63 ms |
136440 KB |
Output is correct |
27 |
Correct |
61 ms |
136272 KB |
Output is correct |
28 |
Correct |
67 ms |
136424 KB |
Output is correct |
29 |
Correct |
62 ms |
136432 KB |
Output is correct |
30 |
Correct |
61 ms |
136448 KB |
Output is correct |
31 |
Correct |
22 ms |
133520 KB |
Output is correct |
32 |
Correct |
22 ms |
133060 KB |
Output is correct |
33 |
Correct |
22 ms |
133212 KB |
Output is correct |
34 |
Correct |
25 ms |
133112 KB |
Output is correct |
35 |
Correct |
20 ms |
133212 KB |
Output is correct |
36 |
Correct |
22 ms |
133456 KB |
Output is correct |
37 |
Correct |
23 ms |
133204 KB |
Output is correct |
38 |
Correct |
20 ms |
133208 KB |
Output is correct |
39 |
Correct |
21 ms |
133212 KB |
Output is correct |
40 |
Correct |
21 ms |
133212 KB |
Output is correct |
41 |
Correct |
22 ms |
133108 KB |
Output is correct |
42 |
Correct |
22 ms |
133120 KB |
Output is correct |
43 |
Correct |
22 ms |
133156 KB |
Output is correct |
44 |
Correct |
20 ms |
133132 KB |
Output is correct |
45 |
Correct |
22 ms |
133212 KB |
Output is correct |
46 |
Correct |
20 ms |
133212 KB |
Output is correct |
47 |
Correct |
21 ms |
133200 KB |
Output is correct |
48 |
Correct |
21 ms |
133212 KB |
Output is correct |
49 |
Correct |
21 ms |
133212 KB |
Output is correct |
50 |
Correct |
21 ms |
133068 KB |
Output is correct |
51 |
Correct |
21 ms |
133212 KB |
Output is correct |
52 |
Correct |
21 ms |
133132 KB |
Output is correct |
53 |
Correct |
21 ms |
133200 KB |
Output is correct |
54 |
Correct |
21 ms |
133144 KB |
Output is correct |
55 |
Correct |
22 ms |
133284 KB |
Output is correct |
56 |
Correct |
23 ms |
133200 KB |
Output is correct |
57 |
Correct |
24 ms |
133208 KB |
Output is correct |
58 |
Correct |
22 ms |
133260 KB |
Output is correct |
59 |
Correct |
22 ms |
133132 KB |
Output is correct |
60 |
Correct |
20 ms |
133212 KB |
Output is correct |
61 |
Incorrect |
80 ms |
136384 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |