#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
struct Node {
ll pref;
int ind_pref;
ll inv_pref;
int inv_ind_pref;
ll suf;
int ind_suf;
ll inv_suf;
int inv_ind_suf;
ll psoms;
int indl_psoms, indr_psoms;
ll inv_psoms;
int inv_indl_psoms, inv_indr_psoms;
ll sum;
int lazy;
};
const int LIM=1<<20, INF=1e9+7;
ll T[LIM], N=1, n, k;
Node tr[2*LIM], pusty;
void inv(int v) {
swap(tr[v].pref, tr[v].inv_pref);
swap(tr[v].ind_pref, tr[v].inv_ind_pref);
swap(tr[v].suf, tr[v].inv_suf);
swap(tr[v].ind_suf, tr[v].inv_ind_suf);
swap(tr[v].psoms, tr[v].inv_psoms);
swap(tr[v].indl_psoms, tr[v].inv_indl_psoms);
swap(tr[v].indr_psoms, tr[v].inv_indr_psoms);
tr[v].sum*=-1;
tr[v].lazy^=1;
}
void spl(int v) {
inv(2*v);
inv(2*v+1);
tr[v].lazy=0;
}
void lacz(int v) {
tr[v].pref=tr[2*v].pref; tr[v].ind_pref=tr[2*v].ind_pref;
if(tr[2*v].sum+tr[2*v+1].pref>tr[v].pref) {
tr[v].pref=tr[2*v].sum+tr[2*v+1].pref;
tr[v].ind_pref=tr[2*v+1].ind_pref;
}
tr[v].suf=tr[2*v+1].suf; tr[v].ind_suf=tr[2*v+1].ind_suf;
if(tr[2*v+1].sum+tr[2*v].suf>tr[v].suf) {
tr[v].suf=tr[2*v+1].sum+tr[2*v].suf;
tr[v].ind_suf=tr[2*v].ind_suf;
}
tr[v].psoms=tr[2*v].psoms;
tr[v].indl_psoms=tr[2*v].indl_psoms; tr[v].indr_psoms=tr[2*v].indr_psoms;
if(tr[2*v+1].psoms>tr[v].psoms) {
tr[v].psoms=tr[2*v+1].psoms;
tr[v].indl_psoms=tr[2*v+1].indl_psoms; tr[v].indr_psoms=tr[2*v+1].indr_psoms;
}
if(tr[2*v].suf+tr[2*v+1].pref>tr[v].psoms) {
tr[v].psoms=tr[2*v].suf+tr[2*v+1].pref;
tr[v].indl_psoms=tr[2*v].ind_suf; tr[v].indr_psoms=tr[2*v+1].ind_pref;
}
tr[v].inv_pref=tr[2*v].inv_pref; tr[v].inv_ind_pref=tr[2*v].inv_ind_pref;
if(-tr[2*v].sum+tr[2*v+1].inv_pref>tr[v].inv_pref) {
tr[v].inv_pref=-tr[2*v].sum+tr[2*v+1].inv_pref;
tr[v].inv_ind_pref=tr[2*v+1].inv_ind_pref;
}
tr[v].inv_suf=tr[2*v+1].inv_suf; tr[v].inv_ind_suf=tr[2*v+1].inv_ind_suf;
if(-tr[2*v+1].sum+tr[2*v].inv_suf>tr[v].inv_suf) {
tr[v].inv_suf=-tr[2*v+1].sum+tr[2*v].inv_suf;
tr[v].inv_ind_suf=tr[2*v].inv_ind_suf;
}
tr[v].inv_psoms=tr[2*v].inv_psoms;
tr[v].inv_indl_psoms=tr[2*v].inv_indl_psoms; tr[v].inv_indr_psoms=tr[2*v].inv_indr_psoms;
if(tr[2*v+1].inv_psoms>tr[v].inv_psoms) {
tr[v].inv_psoms=tr[2*v+1].inv_psoms;
tr[v].inv_indl_psoms=tr[2*v+1].inv_indl_psoms; tr[v].inv_indr_psoms=tr[2*v+1].inv_indr_psoms;
}
if(tr[2*v].inv_suf+tr[2*v+1].inv_pref>tr[v].inv_psoms) {
tr[v].inv_psoms=tr[2*v].inv_suf+tr[2*v+1].inv_pref;
tr[v].inv_indl_psoms=tr[2*v].inv_ind_suf; tr[v].inv_indr_psoms=tr[2*v+1].inv_ind_pref;
}
tr[v].sum=tr[2*v].sum+tr[2*v+1].sum;
}
void upd(int v, int l, int r, int a, int b) {
if(b<l || a>r) return;
if(a<=l && r<=b) {
inv(v);
return;
}
if(tr[v].lazy) spl(v);
int mid=(l+r)/2;
upd(2*v, l, mid, a, b);
upd(2*v+1, mid+1, r, a, b);
lacz(v);
}
ll dynamiczny_psoms(int x) {
rep(i, 2*N) tr[i]=pusty;
rep(i, n) tr[N+i]={T[i], i, -T[i], i, T[i], i, -T[i], i, T[i], i, i, -T[i], i, i, T[i], 0};
for(int i=N-1; i; --i) lacz(i);
ll ans=0;
while(x--) {
if(tr[1].psoms<=0) break;
ans+=tr[1].psoms;
upd(1, 0, N-1, tr[1].indl_psoms, tr[1].indr_psoms);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
k=min(n, k);
while(N<n) N*=2;
rep(i, n) cin >> T[i];
cout << dynamiczny_psoms(k) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
120268 KB |
Output is correct |
2 |
Correct |
96 ms |
120368 KB |
Output is correct |
3 |
Correct |
95 ms |
120396 KB |
Output is correct |
4 |
Correct |
94 ms |
120428 KB |
Output is correct |
5 |
Correct |
94 ms |
120312 KB |
Output is correct |
6 |
Correct |
91 ms |
120204 KB |
Output is correct |
7 |
Correct |
98 ms |
120224 KB |
Output is correct |
8 |
Correct |
107 ms |
120452 KB |
Output is correct |
9 |
Correct |
92 ms |
120324 KB |
Output is correct |
10 |
Correct |
91 ms |
120352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
118648 KB |
Output is correct |
2 |
Correct |
98 ms |
118608 KB |
Output is correct |
3 |
Correct |
82 ms |
118532 KB |
Output is correct |
4 |
Correct |
80 ms |
118608 KB |
Output is correct |
5 |
Correct |
92 ms |
120220 KB |
Output is correct |
6 |
Correct |
102 ms |
118596 KB |
Output is correct |
7 |
Correct |
87 ms |
118788 KB |
Output is correct |
8 |
Correct |
97 ms |
120328 KB |
Output is correct |
9 |
Correct |
98 ms |
120252 KB |
Output is correct |
10 |
Correct |
101 ms |
118608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
120524 KB |
Output is correct |
2 |
Correct |
93 ms |
120408 KB |
Output is correct |
3 |
Correct |
96 ms |
120524 KB |
Output is correct |
4 |
Correct |
109 ms |
120492 KB |
Output is correct |
5 |
Correct |
99 ms |
120564 KB |
Output is correct |
6 |
Correct |
102 ms |
120492 KB |
Output is correct |
7 |
Correct |
109 ms |
120688 KB |
Output is correct |
8 |
Correct |
105 ms |
120564 KB |
Output is correct |
9 |
Correct |
98 ms |
120524 KB |
Output is correct |
10 |
Correct |
97 ms |
120616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
456 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
456 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
2 ms |
720 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
120268 KB |
Output is correct |
2 |
Correct |
96 ms |
120368 KB |
Output is correct |
3 |
Correct |
95 ms |
120396 KB |
Output is correct |
4 |
Correct |
94 ms |
120428 KB |
Output is correct |
5 |
Correct |
94 ms |
120312 KB |
Output is correct |
6 |
Correct |
91 ms |
120204 KB |
Output is correct |
7 |
Correct |
98 ms |
120224 KB |
Output is correct |
8 |
Correct |
107 ms |
120452 KB |
Output is correct |
9 |
Correct |
92 ms |
120324 KB |
Output is correct |
10 |
Correct |
91 ms |
120352 KB |
Output is correct |
11 |
Correct |
87 ms |
118648 KB |
Output is correct |
12 |
Correct |
98 ms |
118608 KB |
Output is correct |
13 |
Correct |
82 ms |
118532 KB |
Output is correct |
14 |
Correct |
80 ms |
118608 KB |
Output is correct |
15 |
Correct |
92 ms |
120220 KB |
Output is correct |
16 |
Correct |
102 ms |
118596 KB |
Output is correct |
17 |
Correct |
87 ms |
118788 KB |
Output is correct |
18 |
Correct |
97 ms |
120328 KB |
Output is correct |
19 |
Correct |
98 ms |
120252 KB |
Output is correct |
20 |
Correct |
101 ms |
118608 KB |
Output is correct |
21 |
Correct |
106 ms |
120524 KB |
Output is correct |
22 |
Correct |
93 ms |
120408 KB |
Output is correct |
23 |
Correct |
96 ms |
120524 KB |
Output is correct |
24 |
Correct |
109 ms |
120492 KB |
Output is correct |
25 |
Correct |
99 ms |
120564 KB |
Output is correct |
26 |
Correct |
102 ms |
120492 KB |
Output is correct |
27 |
Correct |
109 ms |
120688 KB |
Output is correct |
28 |
Correct |
105 ms |
120564 KB |
Output is correct |
29 |
Correct |
98 ms |
120524 KB |
Output is correct |
30 |
Correct |
97 ms |
120616 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
0 ms |
324 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
324 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
324 KB |
Output is correct |
48 |
Correct |
1 ms |
456 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
724 KB |
Output is correct |
52 |
Correct |
1 ms |
724 KB |
Output is correct |
53 |
Correct |
1 ms |
852 KB |
Output is correct |
54 |
Correct |
1 ms |
724 KB |
Output is correct |
55 |
Correct |
1 ms |
724 KB |
Output is correct |
56 |
Correct |
1 ms |
724 KB |
Output is correct |
57 |
Correct |
2 ms |
720 KB |
Output is correct |
58 |
Correct |
1 ms |
724 KB |
Output is correct |
59 |
Correct |
1 ms |
724 KB |
Output is correct |
60 |
Correct |
1 ms |
724 KB |
Output is correct |
61 |
Correct |
161 ms |
120516 KB |
Output is correct |
62 |
Correct |
179 ms |
120644 KB |
Output is correct |
63 |
Correct |
116 ms |
120416 KB |
Output is correct |
64 |
Correct |
151 ms |
120524 KB |
Output is correct |
65 |
Correct |
136 ms |
120596 KB |
Output is correct |
66 |
Correct |
136 ms |
120588 KB |
Output is correct |
67 |
Correct |
137 ms |
120496 KB |
Output is correct |
68 |
Correct |
93 ms |
120556 KB |
Output is correct |
69 |
Correct |
94 ms |
120464 KB |
Output is correct |
70 |
Correct |
94 ms |
120380 KB |
Output is correct |