#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
const ll inf = 1e18;
const int MXN = 3e5+5;
struct node{
pii l, r; // v, pos
pair<ll,pii> m;
ll a;
}t[MXN<<2], nt[MXN<<2];
int A[MXN], lz[MXN];
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:38:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = s+e>>1;
| ~^~
feast.cpp: In function 'void init(int, int, int)':
feast.cpp:50:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid = s+e>>1;
| ~^~
feast.cpp: In function 'int main()':
feast.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | auto [l,r] = t[1].m.second;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
152656 KB |
Output is correct |
2 |
Correct |
63 ms |
152868 KB |
Output is correct |
3 |
Correct |
59 ms |
152660 KB |
Output is correct |
4 |
Correct |
60 ms |
152912 KB |
Output is correct |
5 |
Correct |
58 ms |
152656 KB |
Output is correct |
6 |
Correct |
56 ms |
152836 KB |
Output is correct |
7 |
Correct |
57 ms |
152924 KB |
Output is correct |
8 |
Correct |
58 ms |
152924 KB |
Output is correct |
9 |
Correct |
57 ms |
152884 KB |
Output is correct |
10 |
Correct |
70 ms |
152900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
152920 KB |
Output is correct |
2 |
Correct |
53 ms |
152864 KB |
Output is correct |
3 |
Correct |
50 ms |
152924 KB |
Output is correct |
4 |
Correct |
49 ms |
152924 KB |
Output is correct |
5 |
Correct |
57 ms |
152668 KB |
Output is correct |
6 |
Correct |
49 ms |
152656 KB |
Output is correct |
7 |
Correct |
61 ms |
152828 KB |
Output is correct |
8 |
Correct |
60 ms |
152668 KB |
Output is correct |
9 |
Correct |
56 ms |
152924 KB |
Output is correct |
10 |
Correct |
51 ms |
152912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
152924 KB |
Output is correct |
2 |
Correct |
70 ms |
152912 KB |
Output is correct |
3 |
Correct |
64 ms |
152904 KB |
Output is correct |
4 |
Correct |
61 ms |
152912 KB |
Output is correct |
5 |
Correct |
65 ms |
152916 KB |
Output is correct |
6 |
Correct |
65 ms |
152912 KB |
Output is correct |
7 |
Correct |
64 ms |
152912 KB |
Output is correct |
8 |
Correct |
62 ms |
152924 KB |
Output is correct |
9 |
Correct |
64 ms |
152904 KB |
Output is correct |
10 |
Correct |
108 ms |
152916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
153120 KB |
Output is correct |
2 |
Correct |
22 ms |
152656 KB |
Output is correct |
3 |
Correct |
22 ms |
152656 KB |
Output is correct |
4 |
Correct |
23 ms |
152668 KB |
Output is correct |
5 |
Correct |
22 ms |
152668 KB |
Output is correct |
6 |
Correct |
22 ms |
152756 KB |
Output is correct |
7 |
Correct |
23 ms |
152880 KB |
Output is correct |
8 |
Correct |
22 ms |
152924 KB |
Output is correct |
9 |
Correct |
22 ms |
152808 KB |
Output is correct |
10 |
Correct |
22 ms |
152768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
153120 KB |
Output is correct |
2 |
Correct |
22 ms |
152656 KB |
Output is correct |
3 |
Correct |
22 ms |
152656 KB |
Output is correct |
4 |
Correct |
23 ms |
152668 KB |
Output is correct |
5 |
Correct |
22 ms |
152668 KB |
Output is correct |
6 |
Correct |
22 ms |
152756 KB |
Output is correct |
7 |
Correct |
23 ms |
152880 KB |
Output is correct |
8 |
Correct |
22 ms |
152924 KB |
Output is correct |
9 |
Correct |
22 ms |
152808 KB |
Output is correct |
10 |
Correct |
22 ms |
152768 KB |
Output is correct |
11 |
Correct |
22 ms |
152852 KB |
Output is correct |
12 |
Correct |
23 ms |
152808 KB |
Output is correct |
13 |
Correct |
22 ms |
152924 KB |
Output is correct |
14 |
Correct |
22 ms |
152836 KB |
Output is correct |
15 |
Correct |
24 ms |
152768 KB |
Output is correct |
16 |
Correct |
22 ms |
152660 KB |
Output is correct |
17 |
Correct |
23 ms |
152660 KB |
Output is correct |
18 |
Correct |
22 ms |
152876 KB |
Output is correct |
19 |
Correct |
22 ms |
152856 KB |
Output is correct |
20 |
Correct |
22 ms |
152768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
153120 KB |
Output is correct |
2 |
Correct |
22 ms |
152656 KB |
Output is correct |
3 |
Correct |
22 ms |
152656 KB |
Output is correct |
4 |
Correct |
23 ms |
152668 KB |
Output is correct |
5 |
Correct |
22 ms |
152668 KB |
Output is correct |
6 |
Correct |
22 ms |
152756 KB |
Output is correct |
7 |
Correct |
23 ms |
152880 KB |
Output is correct |
8 |
Correct |
22 ms |
152924 KB |
Output is correct |
9 |
Correct |
22 ms |
152808 KB |
Output is correct |
10 |
Correct |
22 ms |
152768 KB |
Output is correct |
11 |
Correct |
22 ms |
152852 KB |
Output is correct |
12 |
Correct |
23 ms |
152808 KB |
Output is correct |
13 |
Correct |
22 ms |
152924 KB |
Output is correct |
14 |
Correct |
22 ms |
152836 KB |
Output is correct |
15 |
Correct |
24 ms |
152768 KB |
Output is correct |
16 |
Correct |
22 ms |
152660 KB |
Output is correct |
17 |
Correct |
23 ms |
152660 KB |
Output is correct |
18 |
Correct |
22 ms |
152876 KB |
Output is correct |
19 |
Correct |
22 ms |
152856 KB |
Output is correct |
20 |
Correct |
22 ms |
152768 KB |
Output is correct |
21 |
Correct |
22 ms |
152856 KB |
Output is correct |
22 |
Correct |
22 ms |
152924 KB |
Output is correct |
23 |
Correct |
23 ms |
152804 KB |
Output is correct |
24 |
Correct |
23 ms |
152768 KB |
Output is correct |
25 |
Correct |
24 ms |
152852 KB |
Output is correct |
26 |
Correct |
22 ms |
152924 KB |
Output is correct |
27 |
Correct |
22 ms |
152880 KB |
Output is correct |
28 |
Correct |
22 ms |
152924 KB |
Output is correct |
29 |
Correct |
22 ms |
152816 KB |
Output is correct |
30 |
Correct |
22 ms |
152844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
152656 KB |
Output is correct |
2 |
Correct |
63 ms |
152868 KB |
Output is correct |
3 |
Correct |
59 ms |
152660 KB |
Output is correct |
4 |
Correct |
60 ms |
152912 KB |
Output is correct |
5 |
Correct |
58 ms |
152656 KB |
Output is correct |
6 |
Correct |
56 ms |
152836 KB |
Output is correct |
7 |
Correct |
57 ms |
152924 KB |
Output is correct |
8 |
Correct |
58 ms |
152924 KB |
Output is correct |
9 |
Correct |
57 ms |
152884 KB |
Output is correct |
10 |
Correct |
70 ms |
152900 KB |
Output is correct |
11 |
Correct |
59 ms |
152920 KB |
Output is correct |
12 |
Correct |
53 ms |
152864 KB |
Output is correct |
13 |
Correct |
50 ms |
152924 KB |
Output is correct |
14 |
Correct |
49 ms |
152924 KB |
Output is correct |
15 |
Correct |
57 ms |
152668 KB |
Output is correct |
16 |
Correct |
49 ms |
152656 KB |
Output is correct |
17 |
Correct |
61 ms |
152828 KB |
Output is correct |
18 |
Correct |
60 ms |
152668 KB |
Output is correct |
19 |
Correct |
56 ms |
152924 KB |
Output is correct |
20 |
Correct |
51 ms |
152912 KB |
Output is correct |
21 |
Correct |
62 ms |
152924 KB |
Output is correct |
22 |
Correct |
70 ms |
152912 KB |
Output is correct |
23 |
Correct |
64 ms |
152904 KB |
Output is correct |
24 |
Correct |
61 ms |
152912 KB |
Output is correct |
25 |
Correct |
65 ms |
152916 KB |
Output is correct |
26 |
Correct |
65 ms |
152912 KB |
Output is correct |
27 |
Correct |
64 ms |
152912 KB |
Output is correct |
28 |
Correct |
62 ms |
152924 KB |
Output is correct |
29 |
Correct |
64 ms |
152904 KB |
Output is correct |
30 |
Correct |
108 ms |
152916 KB |
Output is correct |
31 |
Correct |
22 ms |
153120 KB |
Output is correct |
32 |
Correct |
22 ms |
152656 KB |
Output is correct |
33 |
Correct |
22 ms |
152656 KB |
Output is correct |
34 |
Correct |
23 ms |
152668 KB |
Output is correct |
35 |
Correct |
22 ms |
152668 KB |
Output is correct |
36 |
Correct |
22 ms |
152756 KB |
Output is correct |
37 |
Correct |
23 ms |
152880 KB |
Output is correct |
38 |
Correct |
22 ms |
152924 KB |
Output is correct |
39 |
Correct |
22 ms |
152808 KB |
Output is correct |
40 |
Correct |
22 ms |
152768 KB |
Output is correct |
41 |
Correct |
22 ms |
152852 KB |
Output is correct |
42 |
Correct |
23 ms |
152808 KB |
Output is correct |
43 |
Correct |
22 ms |
152924 KB |
Output is correct |
44 |
Correct |
22 ms |
152836 KB |
Output is correct |
45 |
Correct |
24 ms |
152768 KB |
Output is correct |
46 |
Correct |
22 ms |
152660 KB |
Output is correct |
47 |
Correct |
23 ms |
152660 KB |
Output is correct |
48 |
Correct |
22 ms |
152876 KB |
Output is correct |
49 |
Correct |
22 ms |
152856 KB |
Output is correct |
50 |
Correct |
22 ms |
152768 KB |
Output is correct |
51 |
Correct |
22 ms |
152856 KB |
Output is correct |
52 |
Correct |
22 ms |
152924 KB |
Output is correct |
53 |
Correct |
23 ms |
152804 KB |
Output is correct |
54 |
Correct |
23 ms |
152768 KB |
Output is correct |
55 |
Correct |
24 ms |
152852 KB |
Output is correct |
56 |
Correct |
22 ms |
152924 KB |
Output is correct |
57 |
Correct |
22 ms |
152880 KB |
Output is correct |
58 |
Correct |
22 ms |
152924 KB |
Output is correct |
59 |
Correct |
22 ms |
152816 KB |
Output is correct |
60 |
Correct |
22 ms |
152844 KB |
Output is correct |
61 |
Incorrect |
126 ms |
153076 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |