#include <bits/stdc++.h>
using namespace std;
const int N = 250000;
const int MAX = (int) 1.00001e9;
int root[N * 60], ch[N * 60][2], cnt[N * 60], tsz;
long long sum[N * 60];
void Modify(int& x, int y, int l, int r, int i) {
x = ++tsz;
ch[x][0] = ch[y][0];
ch[x][1] = ch[y][1];
sum[x] = sum[y] + i;
cnt[x] = cnt[y] + 1;
if (l == r) {
return;
}
int mid = (l + r) >> 1;
if (i <= mid) {
Modify(ch[x][0], ch[y][0], l, mid, i);
} else {
Modify(ch[x][1], ch[y][1], mid + 1, r, i);
}
}
int Walk(int x, int y, int l, int r, int k) {
if (l == r) {
return l;
}
int mid = (l + r) >> 1;
if (cnt[ch[x][1]] - cnt[ch[y][1]] >= k) {
return Walk(ch[x][1], ch[y][1], mid + 1, r, k);
} else {
return Walk(ch[x][0], ch[y][0], l, mid, k - (cnt[ch[x][1]] - cnt[ch[y][1]]));
}
}
int QueryCnt(int x, int y, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return cnt[x] - cnt[y];
}
int mid = (l + r) >> 1;
if (rr <= mid) {
return QueryCnt(ch[x][0], ch[y][0], l, mid, ll, rr);
} else if (ll > mid) {
return QueryCnt(ch[x][1], ch[y][1], mid + 1, r, ll, rr);
} else {
return QueryCnt(ch[x][0], ch[y][0], l, mid, ll, rr) + QueryCnt(ch[x][1], ch[y][1], mid + 1, r, ll, rr);
}
}
long long QuerySum(int x, int y, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return sum[x] - sum[y];
}
int mid = (l + r) >> 1;
if (rr <= mid) {
return QuerySum(ch[x][0], ch[y][0], l, mid, ll, rr);
} else if (ll > mid) {
return QuerySum(ch[x][1], ch[y][1], mid + 1, r, ll, rr);
} else {
return QuerySum(ch[x][0], ch[y][0], l, mid, ll, rr) + QuerySum(ch[x][1], ch[y][1], mid + 1, r, ll, rr);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
}
vector<int> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
vector<long long> pref(n);
for (int i = 0; i < n; i++) {
pref[i] = (i == 0 ? 0LL : pref[i - 1]) + c[i];
}
for (int i = 0; i < n; i++) {
if (i > 0) {
root[i] = root[i - 1];
}
Modify(root[i], root[i], 0, MAX, s[i]);
}
const long long inf = (long long) -1e18;
long long res = inf;
int from = -1, to = -1;
auto Calc = [&](int l, int r) {
if (r - l + 1 < k) {
return inf;
}
long long ret = -(pref[r] - (l == 0 ? 0LL : pref[l - 1]));
int el = Walk(root[r], l == 0 ? 0 : root[l - 1], 0, MAX, k);
int c = QueryCnt(root[r], l == 0 ? 0 : root[l - 1], 0, MAX, el + 1, MAX);
ret += QuerySum(root[r], l == 0 ? 0 : root[l - 1], 0, MAX, el + 1, MAX);
ret += el * 1LL * (k - c);
return ret;
};
function<void(int, int, int, int)> Solve = [&](int l, int r, int optl, int optr) {
if (l > r) {
return;
}
int mid = (l + r) >> 1;
int opt = min(optl, mid);
long long best = Calc(opt, mid);
for (int i = optl; i <= min(mid, optr); i++) {
long long v = Calc(i, mid);
if (v > best) {
best = v;
opt = i;
}
}
if (best > res) {
res = best;
from = opt;
to = mid;
}
Solve(l, mid - 1, optl, opt);
Solve(mid + 1, r, opt, optr);
};
Solve(0, n - 1, 0, n - 1);
cout << res << '\n';
vector<int> order;
for (int i = from; i <= to; i++) {
order.push_back(i);
}
sort(order.begin(), order.end(), [&](int i, int j) {
return s[i] > s[j];
});
vector<int> seq(n, 0);
for (int i = 0; i < min(k, (int) order.size()); i++) {
seq[order[i]] = 1;
}
for (int i = 0; i < n; i++) {
cout << seq[i];
}
cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Partially correct |
0 ms |
348 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Partially correct |
0 ms |
348 KB |
Partially correct |
3 |
Partially correct |
0 ms |
348 KB |
Partially correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Partially correct |
1 ms |
604 KB |
Partially correct |
7 |
Partially correct |
1 ms |
372 KB |
Partially correct |
8 |
Partially correct |
1 ms |
604 KB |
Partially correct |
9 |
Partially correct |
1 ms |
604 KB |
Partially correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Partially correct |
1 ms |
604 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Partially correct |
0 ms |
348 KB |
Partially correct |
3 |
Partially correct |
0 ms |
348 KB |
Partially correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Partially correct |
1 ms |
604 KB |
Partially correct |
7 |
Partially correct |
1 ms |
372 KB |
Partially correct |
8 |
Partially correct |
1 ms |
604 KB |
Partially correct |
9 |
Partially correct |
1 ms |
604 KB |
Partially correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Partially correct |
1 ms |
604 KB |
Partially correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Partially correct |
0 ms |
348 KB |
Partially correct |
14 |
Partially correct |
0 ms |
348 KB |
Partially correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Partially correct |
1 ms |
604 KB |
Partially correct |
18 |
Partially correct |
1 ms |
604 KB |
Partially correct |
19 |
Partially correct |
1 ms |
600 KB |
Partially correct |
20 |
Partially correct |
1 ms |
604 KB |
Partially correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Partially correct |
1 ms |
604 KB |
Partially correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Partially correct |
37 ms |
6120 KB |
Partially correct |
25 |
Partially correct |
27 ms |
6116 KB |
Partially correct |
26 |
Partially correct |
30 ms |
5980 KB |
Partially correct |
27 |
Partially correct |
19 ms |
5980 KB |
Partially correct |
28 |
Correct |
22 ms |
5980 KB |
Output is correct |
29 |
Correct |
11 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
0 ms |
348 KB |
Partially correct |
2 |
Partially correct |
1097 ms |
159568 KB |
Partially correct |
3 |
Correct |
1056 ms |
161856 KB |
Output is correct |
4 |
Correct |
1772 ms |
160336 KB |
Output is correct |
5 |
Partially correct |
1893 ms |
160548 KB |
Partially correct |
6 |
Partially correct |
1619 ms |
160332 KB |
Partially correct |
7 |
Partially correct |
1966 ms |
161456 KB |
Partially correct |
8 |
Correct |
962 ms |
160336 KB |
Output is correct |
9 |
Correct |
1044 ms |
161872 KB |
Output is correct |
10 |
Partially correct |
1043 ms |
160340 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
0 ms |
348 KB |
Partially correct |
2 |
Partially correct |
1097 ms |
159568 KB |
Partially correct |
3 |
Correct |
1056 ms |
161856 KB |
Output is correct |
4 |
Correct |
1772 ms |
160336 KB |
Output is correct |
5 |
Partially correct |
1893 ms |
160548 KB |
Partially correct |
6 |
Partially correct |
1619 ms |
160332 KB |
Partially correct |
7 |
Partially correct |
1966 ms |
161456 KB |
Partially correct |
8 |
Correct |
962 ms |
160336 KB |
Output is correct |
9 |
Correct |
1044 ms |
161872 KB |
Output is correct |
10 |
Partially correct |
1043 ms |
160340 KB |
Partially correct |
11 |
Partially correct |
0 ms |
348 KB |
Partially correct |
12 |
Partially correct |
1097 ms |
161888 KB |
Partially correct |
13 |
Correct |
1071 ms |
161860 KB |
Output is correct |
14 |
Correct |
1856 ms |
161856 KB |
Output is correct |
15 |
Partially correct |
1903 ms |
161876 KB |
Partially correct |
16 |
Partially correct |
1664 ms |
161856 KB |
Partially correct |
17 |
Partially correct |
2011 ms |
162996 KB |
Partially correct |
18 |
Correct |
980 ms |
161876 KB |
Output is correct |
19 |
Correct |
1038 ms |
161872 KB |
Output is correct |
20 |
Partially correct |
1048 ms |
161856 KB |
Partially correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Partially correct |
0 ms |
344 KB |
Partially correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Partially correct |
1 ms |
348 KB |
Partially correct |
26 |
Partially correct |
1 ms |
604 KB |
Partially correct |
27 |
Partially correct |
1 ms |
604 KB |
Partially correct |
28 |
Partially correct |
1 ms |
604 KB |
Partially correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Partially correct |
1 ms |
604 KB |
Partially correct |
31 |
Correct |
1028 ms |
161848 KB |
Output is correct |
32 |
Partially correct |
2021 ms |
162132 KB |
Partially correct |
33 |
Partially correct |
1920 ms |
162108 KB |
Partially correct |
34 |
Partially correct |
1620 ms |
161876 KB |
Partially correct |
35 |
Partially correct |
1462 ms |
161856 KB |
Partially correct |
36 |
Partially correct |
1696 ms |
163004 KB |
Partially correct |
37 |
Partially correct |
1388 ms |
161852 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Partially correct |
0 ms |
348 KB |
Partially correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Partially correct |
0 ms |
348 KB |
Partially correct |
5 |
Partially correct |
0 ms |
348 KB |
Partially correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Partially correct |
1 ms |
604 KB |
Partially correct |
9 |
Partially correct |
1 ms |
372 KB |
Partially correct |
10 |
Partially correct |
1 ms |
604 KB |
Partially correct |
11 |
Partially correct |
1 ms |
604 KB |
Partially correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Partially correct |
1 ms |
604 KB |
Partially correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Partially correct |
0 ms |
348 KB |
Partially correct |
16 |
Partially correct |
0 ms |
348 KB |
Partially correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Partially correct |
1 ms |
604 KB |
Partially correct |
20 |
Partially correct |
1 ms |
604 KB |
Partially correct |
21 |
Partially correct |
1 ms |
600 KB |
Partially correct |
22 |
Partially correct |
1 ms |
604 KB |
Partially correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Partially correct |
1 ms |
604 KB |
Partially correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Partially correct |
37 ms |
6120 KB |
Partially correct |
27 |
Partially correct |
27 ms |
6116 KB |
Partially correct |
28 |
Partially correct |
30 ms |
5980 KB |
Partially correct |
29 |
Partially correct |
19 ms |
5980 KB |
Partially correct |
30 |
Correct |
22 ms |
5980 KB |
Output is correct |
31 |
Correct |
11 ms |
5980 KB |
Output is correct |
32 |
Partially correct |
0 ms |
348 KB |
Partially correct |
33 |
Partially correct |
1097 ms |
159568 KB |
Partially correct |
34 |
Correct |
1056 ms |
161856 KB |
Output is correct |
35 |
Correct |
1772 ms |
160336 KB |
Output is correct |
36 |
Partially correct |
1893 ms |
160548 KB |
Partially correct |
37 |
Partially correct |
1619 ms |
160332 KB |
Partially correct |
38 |
Partially correct |
1966 ms |
161456 KB |
Partially correct |
39 |
Correct |
962 ms |
160336 KB |
Output is correct |
40 |
Correct |
1044 ms |
161872 KB |
Output is correct |
41 |
Partially correct |
1043 ms |
160340 KB |
Partially correct |
42 |
Partially correct |
0 ms |
348 KB |
Partially correct |
43 |
Partially correct |
1097 ms |
161888 KB |
Partially correct |
44 |
Correct |
1071 ms |
161860 KB |
Output is correct |
45 |
Correct |
1856 ms |
161856 KB |
Output is correct |
46 |
Partially correct |
1903 ms |
161876 KB |
Partially correct |
47 |
Partially correct |
1664 ms |
161856 KB |
Partially correct |
48 |
Partially correct |
2011 ms |
162996 KB |
Partially correct |
49 |
Correct |
980 ms |
161876 KB |
Output is correct |
50 |
Correct |
1038 ms |
161872 KB |
Output is correct |
51 |
Partially correct |
1048 ms |
161856 KB |
Partially correct |
52 |
Correct |
0 ms |
344 KB |
Output is correct |
53 |
Partially correct |
0 ms |
344 KB |
Partially correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
0 ms |
604 KB |
Output is correct |
56 |
Partially correct |
1 ms |
348 KB |
Partially correct |
57 |
Partially correct |
1 ms |
604 KB |
Partially correct |
58 |
Partially correct |
1 ms |
604 KB |
Partially correct |
59 |
Partially correct |
1 ms |
604 KB |
Partially correct |
60 |
Correct |
1 ms |
604 KB |
Output is correct |
61 |
Partially correct |
1 ms |
604 KB |
Partially correct |
62 |
Correct |
1028 ms |
161848 KB |
Output is correct |
63 |
Partially correct |
2021 ms |
162132 KB |
Partially correct |
64 |
Partially correct |
1920 ms |
162108 KB |
Partially correct |
65 |
Partially correct |
1620 ms |
161876 KB |
Partially correct |
66 |
Partially correct |
1462 ms |
161856 KB |
Partially correct |
67 |
Partially correct |
1696 ms |
163004 KB |
Partially correct |
68 |
Partially correct |
1388 ms |
161852 KB |
Partially correct |
69 |
Partially correct |
0 ms |
348 KB |
Partially correct |
70 |
Partially correct |
1063 ms |
161904 KB |
Partially correct |
71 |
Correct |
1030 ms |
161876 KB |
Output is correct |
72 |
Correct |
1740 ms |
161856 KB |
Output is correct |
73 |
Partially correct |
1876 ms |
162072 KB |
Partially correct |
74 |
Partially correct |
1677 ms |
160272 KB |
Partially correct |
75 |
Partially correct |
1988 ms |
163004 KB |
Partially correct |
76 |
Correct |
959 ms |
161872 KB |
Output is correct |
77 |
Correct |
1066 ms |
161876 KB |
Output is correct |
78 |
Partially correct |
1045 ms |
161848 KB |
Partially correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Partially correct |
0 ms |
348 KB |
Partially correct |
81 |
Correct |
1 ms |
604 KB |
Output is correct |
82 |
Correct |
0 ms |
604 KB |
Output is correct |
83 |
Partially correct |
1 ms |
604 KB |
Partially correct |
84 |
Partially correct |
1 ms |
604 KB |
Partially correct |
85 |
Partially correct |
1 ms |
604 KB |
Partially correct |
86 |
Partially correct |
1 ms |
600 KB |
Partially correct |
87 |
Correct |
1 ms |
604 KB |
Output is correct |
88 |
Partially correct |
1 ms |
604 KB |
Partially correct |
89 |
Correct |
1016 ms |
160344 KB |
Output is correct |
90 |
Partially correct |
2021 ms |
162104 KB |
Partially correct |
91 |
Partially correct |
1937 ms |
160552 KB |
Partially correct |
92 |
Partially correct |
1661 ms |
160336 KB |
Partially correct |
93 |
Partially correct |
1541 ms |
160384 KB |
Partially correct |
94 |
Partially correct |
1705 ms |
163000 KB |
Partially correct |
95 |
Partially correct |
1424 ms |
161916 KB |
Partially correct |
96 |
Correct |
3 ms |
5980 KB |
Output is correct |
97 |
Partially correct |
30 ms |
6120 KB |
Partially correct |
98 |
Partially correct |
26 ms |
6128 KB |
Partially correct |
99 |
Partially correct |
30 ms |
6112 KB |
Partially correct |
100 |
Partially correct |
18 ms |
5976 KB |
Partially correct |
101 |
Correct |
22 ms |
5980 KB |
Output is correct |
102 |
Correct |
11 ms |
5980 KB |
Output is correct |
103 |
Correct |
1097 ms |
161024 KB |
Output is correct |
104 |
Partially correct |
1663 ms |
161872 KB |
Partially correct |
105 |
Partially correct |
1647 ms |
161876 KB |
Partially correct |
106 |
Partially correct |
1704 ms |
160684 KB |
Partially correct |
107 |
Partially correct |
155 ms |
163368 KB |
Partially correct |
108 |
Partially correct |
901 ms |
162808 KB |
Partially correct |
109 |
Partially correct |
1032 ms |
161332 KB |
Partially correct |
110 |
Partially correct |
1219 ms |
161508 KB |
Partially correct |
111 |
Correct |
587 ms |
162640 KB |
Output is correct |
112 |
Partially correct |
1354 ms |
162328 KB |
Partially correct |