#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr ll inf = 1e18;
struct Answer{
ll maximum;
ll rangeCnt;
string optimalIndices;
};
int n, k;
vector<ll> a, b, bpr, result;
vector<int> opt, opt2;
constexpr int NODES = 1e7;
int nodeCnt, L[NODES], R[NODES], scnt[NODES];
ll ssum[NODES];
int supd(int p, ll v, int a, int b, int i){
int node = nodeCnt++;
L[node] = L[i];
R[node] = R[i];
if(a == b){
ssum[node] = v;
scnt[node] = 1;
return node;
}
int m = (a+b)/2;
if(p <= m) L[node] = supd(p, v, a, m, L[i]);
else R[node] = supd(p, v, m+1, b, R[i]);
scnt[node] = scnt[L[node]] + scnt[R[node]];
ssum[node] = ssum[L[node]] + ssum[R[node]];
return node;
}
// (sum, k-biggest)
pair<ll, ll> sqry(int goal, int add, int sub, int siz){
if(siz == 1){
assert(scnt[sub] == 0);
return {ssum[add], ssum[add]};
}
int cr = scnt[R[add]] - scnt[R[sub]];
if(cr < goal){
auto [lsum, lkbig] = sqry(goal-cr, L[add], L[sub], siz/2);
return {lsum + ssum[R[add]] - ssum[R[sub]], lkbig};
}
else{
return sqry(goal, R[add], R[sub], siz/2);
}
}
vector<int> roots;
int cap;
pair<ll, ll> qry(int l, int r){
auto [sum, kbig] = sqry(k, roots[r+1], roots[l], cap);
sum -= bpr[r+1] - bpr[l];
return {sum, kbig};
}
void init(){
roots.resize(n+1);
for(cap=1; cap<n; cap*=2);
roots[0] = 1;
for(int i = cap-1; i >= 1; i--) L[i] = 2*i, R[i] = 2*i+1;
nodeCnt = 2*cap;
vector<pair<ll, int>> aind;
for(int i = 0; i < n; i++) aind.emplace_back(a[i], i);
sort(aind.begin(), aind.end());
vector<int> ind(n);
for(int i = 0; i < n; i++) ind[aind[i].second] = i;
for(int i = 0; i < n; i++) roots[i+1] = supd(ind[i], a[i], 0, cap-1, roots[i]);
bpr.resize(n+1);
for(int i = 0; i < n; i++) bpr[i+1] = bpr[i] + b[i];
}
ll ans = -inf;
// minimum optimal index
void dc(int l, int r, int optL, int optR){
if(l > r) return;
int m = (l+r)/2;
ll res = -inf; int ind = optR;
for(int i = max(m+k-1, optL); i <= optR; i++){
ll val = qry(m, i).first;
if(val > res){
res = val;
ind = i;
}
}
ans = max(ans, res);
opt[m] = ind;
result[m] = res;
dc(l, m-1, optL, ind);
dc(m+1, r, ind, optR);
}
Answer solve(){
init();
ans = -inf;
opt.assign(n, -1);
opt2.assign(n, -1);
result.assign(n, -inf);
dc(0, n-1, 0, n-1);
Answer answer;
answer.maximum = ans;
// ans is calculated now.
// optimal indices
{
vector<vector<ll>> add(n), rem(n+1);
int nxt = 0;
for(int i = 0; i < n; i++){
if(result[i] != ans) continue;
nxt = max(nxt, i+1);
while(nxt < n && result[nxt] != ans) nxt++;
int r = n-1;
if(nxt != n) r = opt[nxt];
for(int j = opt[i]; j <= r; j++){
auto [val, kth] = qry(i, j);
if(val == ans){
add[i].push_back(kth);
rem[j+1].push_back(kth);
}
}
}
string optimalIndices(n, '0');
multiset<ll> active;
for(int i = 0; i < n; i++){
for(ll x : add[i]) active.insert(x);
for(ll x : rem[i]) active.erase(active.find(x));
if(active.empty()) continue;
if(a[i] >= *active.begin()) optimalIndices[i] = '1';
}
answer.optimalIndices = optimalIndices;
}
return answer;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
a.resize(n), b.resize(n);
for(int i = 0; i < n; i++) cin >> b[i];
for(int i = 0; i < n; i++) cin >> a[i];
Answer ans = solve();
cout << ans.maximum << "\n";
cout << ans.optimalIndices << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 ms |
6744 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
3 ms |
9208 KB |
Output is correct |
24 |
Correct |
7 ms |
9212 KB |
Output is correct |
25 |
Correct |
8 ms |
9420 KB |
Output is correct |
26 |
Correct |
8 ms |
9356 KB |
Output is correct |
27 |
Correct |
5 ms |
9468 KB |
Output is correct |
28 |
Correct |
6 ms |
9212 KB |
Output is correct |
29 |
Correct |
5 ms |
9212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
355 ms |
137868 KB |
Output is correct |
3 |
Correct |
298 ms |
130008 KB |
Output is correct |
4 |
Correct |
446 ms |
130164 KB |
Output is correct |
5 |
Correct |
721 ms |
135856 KB |
Output is correct |
6 |
Correct |
708 ms |
135544 KB |
Output is correct |
7 |
Correct |
360 ms |
140580 KB |
Output is correct |
8 |
Correct |
343 ms |
130056 KB |
Output is correct |
9 |
Correct |
318 ms |
130164 KB |
Output is correct |
10 |
Correct |
344 ms |
135144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
355 ms |
137868 KB |
Output is correct |
3 |
Correct |
298 ms |
130008 KB |
Output is correct |
4 |
Correct |
446 ms |
130164 KB |
Output is correct |
5 |
Correct |
721 ms |
135856 KB |
Output is correct |
6 |
Correct |
708 ms |
135544 KB |
Output is correct |
7 |
Correct |
360 ms |
140580 KB |
Output is correct |
8 |
Correct |
343 ms |
130056 KB |
Output is correct |
9 |
Correct |
318 ms |
130164 KB |
Output is correct |
10 |
Correct |
344 ms |
135144 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
338 ms |
137904 KB |
Output is correct |
13 |
Correct |
313 ms |
129932 KB |
Output is correct |
14 |
Correct |
509 ms |
129924 KB |
Output is correct |
15 |
Correct |
764 ms |
136076 KB |
Output is correct |
16 |
Correct |
706 ms |
135600 KB |
Output is correct |
17 |
Correct |
367 ms |
140660 KB |
Output is correct |
18 |
Correct |
326 ms |
129912 KB |
Output is correct |
19 |
Correct |
313 ms |
129908 KB |
Output is correct |
20 |
Correct |
373 ms |
135240 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6636 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
305 ms |
129964 KB |
Output is correct |
32 |
Correct |
748 ms |
134068 KB |
Output is correct |
33 |
Correct |
789 ms |
136876 KB |
Output is correct |
34 |
Correct |
667 ms |
133496 KB |
Output is correct |
35 |
Correct |
694 ms |
132236 KB |
Output is correct |
36 |
Correct |
329 ms |
129972 KB |
Output is correct |
37 |
Correct |
352 ms |
136632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6744 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
3 ms |
9208 KB |
Output is correct |
26 |
Correct |
7 ms |
9212 KB |
Output is correct |
27 |
Correct |
8 ms |
9420 KB |
Output is correct |
28 |
Correct |
8 ms |
9356 KB |
Output is correct |
29 |
Correct |
5 ms |
9468 KB |
Output is correct |
30 |
Correct |
6 ms |
9212 KB |
Output is correct |
31 |
Correct |
5 ms |
9212 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
355 ms |
137868 KB |
Output is correct |
34 |
Correct |
298 ms |
130008 KB |
Output is correct |
35 |
Correct |
446 ms |
130164 KB |
Output is correct |
36 |
Correct |
721 ms |
135856 KB |
Output is correct |
37 |
Correct |
708 ms |
135544 KB |
Output is correct |
38 |
Correct |
360 ms |
140580 KB |
Output is correct |
39 |
Correct |
343 ms |
130056 KB |
Output is correct |
40 |
Correct |
318 ms |
130164 KB |
Output is correct |
41 |
Correct |
344 ms |
135144 KB |
Output is correct |
42 |
Correct |
1 ms |
6488 KB |
Output is correct |
43 |
Correct |
338 ms |
137904 KB |
Output is correct |
44 |
Correct |
313 ms |
129932 KB |
Output is correct |
45 |
Correct |
509 ms |
129924 KB |
Output is correct |
46 |
Correct |
764 ms |
136076 KB |
Output is correct |
47 |
Correct |
706 ms |
135600 KB |
Output is correct |
48 |
Correct |
367 ms |
140660 KB |
Output is correct |
49 |
Correct |
326 ms |
129912 KB |
Output is correct |
50 |
Correct |
313 ms |
129908 KB |
Output is correct |
51 |
Correct |
373 ms |
135240 KB |
Output is correct |
52 |
Correct |
1 ms |
6492 KB |
Output is correct |
53 |
Correct |
1 ms |
6492 KB |
Output is correct |
54 |
Correct |
1 ms |
6492 KB |
Output is correct |
55 |
Correct |
1 ms |
6492 KB |
Output is correct |
56 |
Correct |
1 ms |
6636 KB |
Output is correct |
57 |
Correct |
1 ms |
6492 KB |
Output is correct |
58 |
Correct |
1 ms |
6492 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6492 KB |
Output is correct |
61 |
Correct |
1 ms |
6492 KB |
Output is correct |
62 |
Correct |
305 ms |
129964 KB |
Output is correct |
63 |
Correct |
748 ms |
134068 KB |
Output is correct |
64 |
Correct |
789 ms |
136876 KB |
Output is correct |
65 |
Correct |
667 ms |
133496 KB |
Output is correct |
66 |
Correct |
694 ms |
132236 KB |
Output is correct |
67 |
Correct |
329 ms |
129972 KB |
Output is correct |
68 |
Correct |
352 ms |
136632 KB |
Output is correct |
69 |
Correct |
1 ms |
6492 KB |
Output is correct |
70 |
Correct |
350 ms |
137908 KB |
Output is correct |
71 |
Correct |
307 ms |
129932 KB |
Output is correct |
72 |
Correct |
482 ms |
129976 KB |
Output is correct |
73 |
Correct |
699 ms |
135800 KB |
Output is correct |
74 |
Correct |
700 ms |
135600 KB |
Output is correct |
75 |
Correct |
388 ms |
140660 KB |
Output is correct |
76 |
Correct |
326 ms |
130164 KB |
Output is correct |
77 |
Correct |
315 ms |
130108 KB |
Output is correct |
78 |
Correct |
351 ms |
135288 KB |
Output is correct |
79 |
Correct |
1 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
6492 KB |
Output is correct |
81 |
Correct |
1 ms |
6492 KB |
Output is correct |
82 |
Correct |
1 ms |
6492 KB |
Output is correct |
83 |
Correct |
1 ms |
6492 KB |
Output is correct |
84 |
Correct |
1 ms |
6492 KB |
Output is correct |
85 |
Correct |
1 ms |
6492 KB |
Output is correct |
86 |
Correct |
1 ms |
6492 KB |
Output is correct |
87 |
Correct |
1 ms |
6492 KB |
Output is correct |
88 |
Correct |
1 ms |
6492 KB |
Output is correct |
89 |
Correct |
294 ms |
129912 KB |
Output is correct |
90 |
Correct |
725 ms |
134068 KB |
Output is correct |
91 |
Correct |
861 ms |
136876 KB |
Output is correct |
92 |
Correct |
770 ms |
133680 KB |
Output is correct |
93 |
Correct |
798 ms |
131980 KB |
Output is correct |
94 |
Correct |
344 ms |
130116 KB |
Output is correct |
95 |
Correct |
346 ms |
136624 KB |
Output is correct |
96 |
Correct |
3 ms |
9212 KB |
Output is correct |
97 |
Correct |
8 ms |
9212 KB |
Output is correct |
98 |
Correct |
8 ms |
9212 KB |
Output is correct |
99 |
Correct |
11 ms |
9432 KB |
Output is correct |
100 |
Correct |
5 ms |
9500 KB |
Output is correct |
101 |
Correct |
6 ms |
9212 KB |
Output is correct |
102 |
Correct |
6 ms |
9248 KB |
Output is correct |
103 |
Correct |
432 ms |
129912 KB |
Output is correct |
104 |
Correct |
869 ms |
131512 KB |
Output is correct |
105 |
Correct |
752 ms |
133296 KB |
Output is correct |
106 |
Correct |
968 ms |
134836 KB |
Output is correct |
107 |
Correct |
191 ms |
130168 KB |
Output is correct |
108 |
Correct |
555 ms |
133452 KB |
Output is correct |
109 |
Correct |
343 ms |
142196 KB |
Output is correct |
110 |
Correct |
380 ms |
137080 KB |
Output is correct |
111 |
Correct |
188 ms |
130164 KB |
Output is correct |
112 |
Correct |
486 ms |
136056 KB |
Output is correct |