#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
#define int long long
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
const int maxn = 3e5 + 5;
struct Node{
int sum , pre , suf , res;
int ppre , psuf , pl , pr;
Node(){};
Node(int sum , int pre , int suf , int res,int ppre , int psuf , int pl , int pr)
:sum(sum),pre(pre),suf(suf),res(res),ppre(ppre),psuf(psuf),pl(pl),pr(pr){};
Node operator + (const Node other){
if(ppre == -1)return other;
if(other.ppre == -1)return (*this);
Node ans;
if(res < other.res)ans = other;
else ans = (*this);
ans.sum = sum + other.sum;
if(pre > sum + other.pre){
ans.pre = pre;
ans.ppre = ppre;
}else{
ans.pre = sum + other.pre;
ans.ppre = other.ppre;
}
if(other.suf > other.sum + suf){
ans.suf = other.suf;
ans.psuf = other.psuf;
}else{
ans.suf = other.sum + suf;
ans.psuf = psuf;
}
if(ans.res < suf + other.pre){
ans.res = suf + other.pre;
ans.pl = psuf;
ans.pr = other.ppre;
}
return ans;
}
}s[maxn * 4] , rs[maxn * 4];
int n , k;
bool lz[maxn * 4];
int a[maxn];
void Build(int x , int l , int r){
if(l == r){
s[x] = Node(a[l],a[l],a[l],a[l],l,l,l,l);
rs[x] = Node(-a[l],-a[l],-a[l],-a[l],l,l,l,l);
return;
}
int mid = l + r >> 1;
Build(x*2,l,mid);
Build(x*2+1,mid+1,r);
s[x] = s[x * 2] + s[x * 2 + 1];
rs[x] = rs[x * 2] + rs[x * 2 + 1];
}
void Push(int x , bool key){
if(lz[x])swap(s[x],rs[x]);
if(key){
lz[x * 2] ^= lz[x];
lz[x * 2 + 1] ^= lz[x];
}
lz[x] = 0;
}
void update(int x , int l , int r , int pos ,int val){
Push(x,l!=r);
if(l == r){
a[l] = val;
s[x] = Node(a[l],a[l],a[l],a[l],l,l,l,l);
rs[x] = Node(-a[l],-a[l],-a[l],-a[l],l,l,l,l);
lz[x] = 0;
return;
}
int mid = l + r >> 1;
if(pos <= mid)update(x*2,l,mid,pos,val);
else update(x*2+1,mid+1,r,pos,val);
s[x] = s[x * 2] + s[x * 2 + 1];
rs[x] = rs[x * 2] + rs[x * 2 + 1];
}
void Rev(int x , int l , int r , int L , int R){
Push(x,l!=r);
if(L > r || l > R)return;
if(L <= l && r <= R){
lz[x] ^= 1;
Push(x,l!=r);
return;
}
int mid = l + r >> 1;
Rev(x*2,l,mid,L,R);Rev(x*2+1,mid+1,r,L,R);
s[x] = s[x * 2] + s[x * 2 + 1];
rs[x] = rs[x * 2] + rs[x * 2 + 1];
}
Node Query(int x , int l , int r , int L , int R){
Push(x,l!=r);
if(L > r || l > R)return Node(-1,-1,-1,-1,-1,-1,-1,-1);
if(L <= l && r <= R)return s[x];
int mid = l + r >> 1;
return Query(x*2,l,mid,L,R)+Query(x*2+1,mid+1,r,L,R);
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
cin >> n >> k;
for(int i = 1 ; i <= n ; ++i){
cin >> a[i];
}
Build(1,1,n);
ll res = 0;
while(k--){
if(s[1].res <= 0)break;
res += s[1].res;
Rev(1,1,n,s[1].pl,s[1].pr);
}
cout << res;
}
Compilation message
feast.cpp: In function 'void Build(long long int, long long int, long long int)':
feast.cpp:60:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
feast.cpp: In function 'void update(long long int, long long int, long long int, long long int, long long int)':
feast.cpp:83:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
feast.cpp: In function 'void Rev(long long int, long long int, long long int, long long int, long long int)':
feast.cpp:98:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
feast.cpp: In function 'Node Query(long long int, long long int, long long int, long long int, long long int)':
feast.cpp:107:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
feast.cpp: In function 'int32_t main()':
feast.cpp:116:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".INP", "r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:117:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".OUT", "w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
136824 KB |
Output is correct |
2 |
Correct |
103 ms |
136824 KB |
Output is correct |
3 |
Correct |
108 ms |
136952 KB |
Output is correct |
4 |
Correct |
104 ms |
136824 KB |
Output is correct |
5 |
Correct |
107 ms |
136824 KB |
Output is correct |
6 |
Correct |
108 ms |
136936 KB |
Output is correct |
7 |
Correct |
107 ms |
136824 KB |
Output is correct |
8 |
Correct |
108 ms |
136824 KB |
Output is correct |
9 |
Correct |
107 ms |
136824 KB |
Output is correct |
10 |
Correct |
108 ms |
136824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
135032 KB |
Output is correct |
2 |
Correct |
85 ms |
135160 KB |
Output is correct |
3 |
Correct |
93 ms |
135176 KB |
Output is correct |
4 |
Correct |
90 ms |
135160 KB |
Output is correct |
5 |
Correct |
107 ms |
136824 KB |
Output is correct |
6 |
Correct |
85 ms |
135032 KB |
Output is correct |
7 |
Correct |
84 ms |
135160 KB |
Output is correct |
8 |
Correct |
103 ms |
136824 KB |
Output is correct |
9 |
Correct |
108 ms |
136696 KB |
Output is correct |
10 |
Correct |
94 ms |
135160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
137080 KB |
Output is correct |
2 |
Correct |
121 ms |
136984 KB |
Output is correct |
3 |
Correct |
116 ms |
137100 KB |
Output is correct |
4 |
Correct |
112 ms |
137080 KB |
Output is correct |
5 |
Correct |
122 ms |
137080 KB |
Output is correct |
6 |
Correct |
127 ms |
137080 KB |
Output is correct |
7 |
Correct |
112 ms |
137080 KB |
Output is correct |
8 |
Correct |
116 ms |
137084 KB |
Output is correct |
9 |
Correct |
116 ms |
137192 KB |
Output is correct |
10 |
Correct |
111 ms |
137080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
896 KB |
Output is correct |
22 |
Correct |
2 ms |
896 KB |
Output is correct |
23 |
Correct |
1 ms |
896 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
2 ms |
896 KB |
Output is correct |
26 |
Correct |
2 ms |
896 KB |
Output is correct |
27 |
Correct |
1 ms |
1024 KB |
Output is correct |
28 |
Correct |
1 ms |
896 KB |
Output is correct |
29 |
Correct |
1 ms |
896 KB |
Output is correct |
30 |
Correct |
1 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
136824 KB |
Output is correct |
2 |
Correct |
103 ms |
136824 KB |
Output is correct |
3 |
Correct |
108 ms |
136952 KB |
Output is correct |
4 |
Correct |
104 ms |
136824 KB |
Output is correct |
5 |
Correct |
107 ms |
136824 KB |
Output is correct |
6 |
Correct |
108 ms |
136936 KB |
Output is correct |
7 |
Correct |
107 ms |
136824 KB |
Output is correct |
8 |
Correct |
108 ms |
136824 KB |
Output is correct |
9 |
Correct |
107 ms |
136824 KB |
Output is correct |
10 |
Correct |
108 ms |
136824 KB |
Output is correct |
11 |
Correct |
86 ms |
135032 KB |
Output is correct |
12 |
Correct |
85 ms |
135160 KB |
Output is correct |
13 |
Correct |
93 ms |
135176 KB |
Output is correct |
14 |
Correct |
90 ms |
135160 KB |
Output is correct |
15 |
Correct |
107 ms |
136824 KB |
Output is correct |
16 |
Correct |
85 ms |
135032 KB |
Output is correct |
17 |
Correct |
84 ms |
135160 KB |
Output is correct |
18 |
Correct |
103 ms |
136824 KB |
Output is correct |
19 |
Correct |
108 ms |
136696 KB |
Output is correct |
20 |
Correct |
94 ms |
135160 KB |
Output is correct |
21 |
Correct |
123 ms |
137080 KB |
Output is correct |
22 |
Correct |
121 ms |
136984 KB |
Output is correct |
23 |
Correct |
116 ms |
137100 KB |
Output is correct |
24 |
Correct |
112 ms |
137080 KB |
Output is correct |
25 |
Correct |
122 ms |
137080 KB |
Output is correct |
26 |
Correct |
127 ms |
137080 KB |
Output is correct |
27 |
Correct |
112 ms |
137080 KB |
Output is correct |
28 |
Correct |
116 ms |
137084 KB |
Output is correct |
29 |
Correct |
116 ms |
137192 KB |
Output is correct |
30 |
Correct |
111 ms |
137080 KB |
Output is correct |
31 |
Correct |
0 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
0 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
0 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
0 ms |
384 KB |
Output is correct |
40 |
Correct |
0 ms |
384 KB |
Output is correct |
41 |
Correct |
0 ms |
512 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
512 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
1 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
1 ms |
512 KB |
Output is correct |
51 |
Correct |
1 ms |
896 KB |
Output is correct |
52 |
Correct |
2 ms |
896 KB |
Output is correct |
53 |
Correct |
1 ms |
896 KB |
Output is correct |
54 |
Correct |
1 ms |
896 KB |
Output is correct |
55 |
Correct |
2 ms |
896 KB |
Output is correct |
56 |
Correct |
2 ms |
896 KB |
Output is correct |
57 |
Correct |
1 ms |
1024 KB |
Output is correct |
58 |
Correct |
1 ms |
896 KB |
Output is correct |
59 |
Correct |
1 ms |
896 KB |
Output is correct |
60 |
Correct |
1 ms |
896 KB |
Output is correct |
61 |
Correct |
200 ms |
137976 KB |
Output is correct |
62 |
Correct |
226 ms |
138176 KB |
Output is correct |
63 |
Correct |
120 ms |
138104 KB |
Output is correct |
64 |
Correct |
201 ms |
138104 KB |
Output is correct |
65 |
Correct |
166 ms |
138104 KB |
Output is correct |
66 |
Correct |
171 ms |
138232 KB |
Output is correct |
67 |
Correct |
161 ms |
137976 KB |
Output is correct |
68 |
Correct |
115 ms |
137976 KB |
Output is correct |
69 |
Correct |
111 ms |
137724 KB |
Output is correct |
70 |
Correct |
110 ms |
137080 KB |
Output is correct |