#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;
typedef tuple<int,int,int> tii;
typedef tuple<ll,ll,ll> tll;
typedef tuple<int,int,int,int> ti4;
typedef vector<vector<ll>> mat;
const ll mod=1e9+7,inf=1e18;
const int N=3e5+5,M=2e5+5,K=1e7+5;
int n,k;
ll a[N],ans;
struct SEG{
struct node{
tll Lmx,Rmx,Sum,Mx;
tll Lmx2,Rmx2,Sum2,Mx2;
void Swap(){
swap(Lmx,Lmx2); swap(Rmx,Rmx2); swap(Sum,Sum2); swap(Mx,Mx2);
}
node operator +(node n1){
node n;
n.Lmx=max(Lmx,{get<0>(Sum)+get<0>(n1.Lmx),get<1>(Sum),get<2>(n1.Lmx)});
n.Rmx=max(n1.Rmx,{get<0>(Rmx)+get<0>(n1.Sum),get<1>(Rmx),get<2>(n1.Sum)});
n.Sum={get<0>(Sum)+get<0>(n1.Sum),get<1>(Sum),get<2>(n1.Sum)};
n.Mx=max({Mx,n1.Mx,{get<0>(Rmx)+get<0>(n1.Lmx),get<1>(Rmx),get<2>(n1.Lmx)}});
n.Lmx2=max(Lmx2,{get<0>(Sum2)+get<0>(n1.Lmx2),get<1>(Sum2),get<2>(n1.Lmx2)});
n.Rmx2=max(n1.Rmx2,{get<0>(Rmx2)+get<0>(n1.Sum2),get<1>(Rmx2),get<2>(n1.Sum2)});
n.Sum2={get<0>(Sum2)+get<0>(n1.Sum2),get<1>(Sum2),get<2>(n1.Sum2)};
n.Mx2=max({Mx2,n1.Mx2,{get<0>(Rmx2)+get<0>(n1.Lmx2),get<1>(Rmx2),get<2>(n1.Lmx2)}});
return n;
}
}tree[4*N]; bool lazy[4*N];
void pd(int nd,int l,int r){
if(lazy[nd]) tree[nd].Swap();
if(l!=r){
lazy[nd<<1]^=lazy[nd];
lazy[nd<<1|1]^=lazy[nd];
}
lazy[nd]=0;
}
void init(int nd,int l,int r){
if(l==r){
tll t1={a[l],l,l},t2={-a[l],l,l};
tree[nd]={t1,t1,t1,t1,t2,t2,t2,t2};
return;
}
int m=(l+r)>>1;
init(nd<<1,l,m); init(nd<<1|1,m+1,r);
tree[nd]=tree[nd<<1]+tree[nd<<1|1];
}
void upd(int nd,int l,int r,int s,int e){
pd(nd,l,r);
if(r<s||e<l) return;
if(s<=l&&r<=e){
lazy[nd]=1; pd(nd,l,r); return;
}
int m=(l+r)>>1;
upd(nd<<1,l,m,s,e); upd(nd<<1|1,m+1,r,s,e);
tree[nd]=tree[nd<<1]+tree[nd<<1|1];
}
}Solver;
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
Solver.init(1,1,n);
for(int i=1;i<=k;i++){
if(get<0>(Solver.tree[1].Mx)<=0) break;
ans+=get<0>(Solver.tree[1].Mx);
Solver.upd(1,1,n,get<1>(Solver.tree[1].Mx),get<2>(Solver.tree[1].Mx));
}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
228064 KB |
Output is correct |
2 |
Correct |
183 ms |
228052 KB |
Output is correct |
3 |
Correct |
175 ms |
228060 KB |
Output is correct |
4 |
Correct |
176 ms |
228176 KB |
Output is correct |
5 |
Correct |
179 ms |
228092 KB |
Output is correct |
6 |
Correct |
175 ms |
228088 KB |
Output is correct |
7 |
Correct |
176 ms |
228088 KB |
Output is correct |
8 |
Correct |
202 ms |
228100 KB |
Output is correct |
9 |
Correct |
183 ms |
228072 KB |
Output is correct |
10 |
Correct |
186 ms |
228160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
228164 KB |
Output is correct |
2 |
Correct |
160 ms |
228216 KB |
Output is correct |
3 |
Correct |
157 ms |
228088 KB |
Output is correct |
4 |
Correct |
175 ms |
228088 KB |
Output is correct |
5 |
Correct |
181 ms |
228004 KB |
Output is correct |
6 |
Correct |
157 ms |
228088 KB |
Output is correct |
7 |
Correct |
155 ms |
228092 KB |
Output is correct |
8 |
Correct |
172 ms |
228088 KB |
Output is correct |
9 |
Correct |
191 ms |
228040 KB |
Output is correct |
10 |
Correct |
167 ms |
228112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
228192 KB |
Output is correct |
2 |
Correct |
183 ms |
228216 KB |
Output is correct |
3 |
Correct |
180 ms |
228088 KB |
Output is correct |
4 |
Correct |
182 ms |
228088 KB |
Output is correct |
5 |
Correct |
187 ms |
228196 KB |
Output is correct |
6 |
Correct |
179 ms |
228216 KB |
Output is correct |
7 |
Correct |
177 ms |
228088 KB |
Output is correct |
8 |
Correct |
183 ms |
228088 KB |
Output is correct |
9 |
Correct |
183 ms |
228172 KB |
Output is correct |
10 |
Correct |
195 ms |
228252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
225760 KB |
Output is correct |
2 |
Correct |
116 ms |
225784 KB |
Output is correct |
3 |
Correct |
119 ms |
225784 KB |
Output is correct |
4 |
Correct |
116 ms |
225784 KB |
Output is correct |
5 |
Correct |
122 ms |
225708 KB |
Output is correct |
6 |
Correct |
120 ms |
225784 KB |
Output is correct |
7 |
Correct |
121 ms |
225760 KB |
Output is correct |
8 |
Correct |
120 ms |
225784 KB |
Output is correct |
9 |
Correct |
122 ms |
225824 KB |
Output is correct |
10 |
Correct |
129 ms |
225784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
225760 KB |
Output is correct |
2 |
Correct |
116 ms |
225784 KB |
Output is correct |
3 |
Correct |
119 ms |
225784 KB |
Output is correct |
4 |
Correct |
116 ms |
225784 KB |
Output is correct |
5 |
Correct |
122 ms |
225708 KB |
Output is correct |
6 |
Correct |
120 ms |
225784 KB |
Output is correct |
7 |
Correct |
121 ms |
225760 KB |
Output is correct |
8 |
Correct |
120 ms |
225784 KB |
Output is correct |
9 |
Correct |
122 ms |
225824 KB |
Output is correct |
10 |
Correct |
129 ms |
225784 KB |
Output is correct |
11 |
Correct |
118 ms |
225760 KB |
Output is correct |
12 |
Correct |
117 ms |
225784 KB |
Output is correct |
13 |
Correct |
125 ms |
225744 KB |
Output is correct |
14 |
Correct |
122 ms |
225808 KB |
Output is correct |
15 |
Correct |
138 ms |
225784 KB |
Output is correct |
16 |
Correct |
119 ms |
225820 KB |
Output is correct |
17 |
Correct |
125 ms |
225764 KB |
Output is correct |
18 |
Correct |
115 ms |
225784 KB |
Output is correct |
19 |
Correct |
117 ms |
225784 KB |
Output is correct |
20 |
Correct |
117 ms |
225784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
225760 KB |
Output is correct |
2 |
Correct |
116 ms |
225784 KB |
Output is correct |
3 |
Correct |
119 ms |
225784 KB |
Output is correct |
4 |
Correct |
116 ms |
225784 KB |
Output is correct |
5 |
Correct |
122 ms |
225708 KB |
Output is correct |
6 |
Correct |
120 ms |
225784 KB |
Output is correct |
7 |
Correct |
121 ms |
225760 KB |
Output is correct |
8 |
Correct |
120 ms |
225784 KB |
Output is correct |
9 |
Correct |
122 ms |
225824 KB |
Output is correct |
10 |
Correct |
129 ms |
225784 KB |
Output is correct |
11 |
Correct |
118 ms |
225760 KB |
Output is correct |
12 |
Correct |
117 ms |
225784 KB |
Output is correct |
13 |
Correct |
125 ms |
225744 KB |
Output is correct |
14 |
Correct |
122 ms |
225808 KB |
Output is correct |
15 |
Correct |
138 ms |
225784 KB |
Output is correct |
16 |
Correct |
119 ms |
225820 KB |
Output is correct |
17 |
Correct |
125 ms |
225764 KB |
Output is correct |
18 |
Correct |
115 ms |
225784 KB |
Output is correct |
19 |
Correct |
117 ms |
225784 KB |
Output is correct |
20 |
Correct |
117 ms |
225784 KB |
Output is correct |
21 |
Correct |
121 ms |
225760 KB |
Output is correct |
22 |
Correct |
131 ms |
225784 KB |
Output is correct |
23 |
Correct |
123 ms |
225784 KB |
Output is correct |
24 |
Correct |
124 ms |
225928 KB |
Output is correct |
25 |
Correct |
126 ms |
225784 KB |
Output is correct |
26 |
Correct |
121 ms |
225772 KB |
Output is correct |
27 |
Correct |
129 ms |
225772 KB |
Output is correct |
28 |
Correct |
132 ms |
225784 KB |
Output is correct |
29 |
Correct |
169 ms |
225840 KB |
Output is correct |
30 |
Correct |
179 ms |
225760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
228064 KB |
Output is correct |
2 |
Correct |
183 ms |
228052 KB |
Output is correct |
3 |
Correct |
175 ms |
228060 KB |
Output is correct |
4 |
Correct |
176 ms |
228176 KB |
Output is correct |
5 |
Correct |
179 ms |
228092 KB |
Output is correct |
6 |
Correct |
175 ms |
228088 KB |
Output is correct |
7 |
Correct |
176 ms |
228088 KB |
Output is correct |
8 |
Correct |
202 ms |
228100 KB |
Output is correct |
9 |
Correct |
183 ms |
228072 KB |
Output is correct |
10 |
Correct |
186 ms |
228160 KB |
Output is correct |
11 |
Correct |
162 ms |
228164 KB |
Output is correct |
12 |
Correct |
160 ms |
228216 KB |
Output is correct |
13 |
Correct |
157 ms |
228088 KB |
Output is correct |
14 |
Correct |
175 ms |
228088 KB |
Output is correct |
15 |
Correct |
181 ms |
228004 KB |
Output is correct |
16 |
Correct |
157 ms |
228088 KB |
Output is correct |
17 |
Correct |
155 ms |
228092 KB |
Output is correct |
18 |
Correct |
172 ms |
228088 KB |
Output is correct |
19 |
Correct |
191 ms |
228040 KB |
Output is correct |
20 |
Correct |
167 ms |
228112 KB |
Output is correct |
21 |
Correct |
184 ms |
228192 KB |
Output is correct |
22 |
Correct |
183 ms |
228216 KB |
Output is correct |
23 |
Correct |
180 ms |
228088 KB |
Output is correct |
24 |
Correct |
182 ms |
228088 KB |
Output is correct |
25 |
Correct |
187 ms |
228196 KB |
Output is correct |
26 |
Correct |
179 ms |
228216 KB |
Output is correct |
27 |
Correct |
177 ms |
228088 KB |
Output is correct |
28 |
Correct |
183 ms |
228088 KB |
Output is correct |
29 |
Correct |
183 ms |
228172 KB |
Output is correct |
30 |
Correct |
195 ms |
228252 KB |
Output is correct |
31 |
Correct |
113 ms |
225760 KB |
Output is correct |
32 |
Correct |
116 ms |
225784 KB |
Output is correct |
33 |
Correct |
119 ms |
225784 KB |
Output is correct |
34 |
Correct |
116 ms |
225784 KB |
Output is correct |
35 |
Correct |
122 ms |
225708 KB |
Output is correct |
36 |
Correct |
120 ms |
225784 KB |
Output is correct |
37 |
Correct |
121 ms |
225760 KB |
Output is correct |
38 |
Correct |
120 ms |
225784 KB |
Output is correct |
39 |
Correct |
122 ms |
225824 KB |
Output is correct |
40 |
Correct |
129 ms |
225784 KB |
Output is correct |
41 |
Correct |
118 ms |
225760 KB |
Output is correct |
42 |
Correct |
117 ms |
225784 KB |
Output is correct |
43 |
Correct |
125 ms |
225744 KB |
Output is correct |
44 |
Correct |
122 ms |
225808 KB |
Output is correct |
45 |
Correct |
138 ms |
225784 KB |
Output is correct |
46 |
Correct |
119 ms |
225820 KB |
Output is correct |
47 |
Correct |
125 ms |
225764 KB |
Output is correct |
48 |
Correct |
115 ms |
225784 KB |
Output is correct |
49 |
Correct |
117 ms |
225784 KB |
Output is correct |
50 |
Correct |
117 ms |
225784 KB |
Output is correct |
51 |
Correct |
121 ms |
225760 KB |
Output is correct |
52 |
Correct |
131 ms |
225784 KB |
Output is correct |
53 |
Correct |
123 ms |
225784 KB |
Output is correct |
54 |
Correct |
124 ms |
225928 KB |
Output is correct |
55 |
Correct |
126 ms |
225784 KB |
Output is correct |
56 |
Correct |
121 ms |
225772 KB |
Output is correct |
57 |
Correct |
129 ms |
225772 KB |
Output is correct |
58 |
Correct |
132 ms |
225784 KB |
Output is correct |
59 |
Correct |
169 ms |
225840 KB |
Output is correct |
60 |
Correct |
179 ms |
225760 KB |
Output is correct |
61 |
Correct |
354 ms |
229440 KB |
Output is correct |
62 |
Correct |
417 ms |
232180 KB |
Output is correct |
63 |
Correct |
214 ms |
232020 KB |
Output is correct |
64 |
Correct |
357 ms |
232160 KB |
Output is correct |
65 |
Correct |
292 ms |
232160 KB |
Output is correct |
66 |
Correct |
319 ms |
232220 KB |
Output is correct |
67 |
Correct |
261 ms |
232056 KB |
Output is correct |
68 |
Correct |
195 ms |
232056 KB |
Output is correct |
69 |
Correct |
207 ms |
231840 KB |
Output is correct |
70 |
Correct |
184 ms |
231160 KB |
Output is correct |