#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;
};
vector<pair<int, int>> segs;
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 = max(0, optl - 30); i <= min(mid, optr + 30); i++) {
long long v = Calc(i, mid);
if (v > best) {
best = v;
opt = i;
if (best > res) {
segs.clear();
res = best;
segs.emplace_back(opt, mid);
} else if (best == res) {
segs.emplace_back(opt, mid);
}
} else if (v == best) {
if (best > res) {
segs.clear();
res = best;
segs.emplace_back(i, mid);
} else if (best == res) {
segs.emplace_back(i, 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> seq(n, 0);
vector<vector<int>> qs(n + 1);
for (auto& p : segs) {
int l = p.first;
int r = p.second;
int el = Walk(root[r], l == 0 ? 0 : root[l - 1], 0, MAX, k);
qs[l].push_back(el);
qs[r + 1].push_back(~el);
}
multiset<int> st;
for (int i = 0; i < n; i++) {
for (int j : qs[i]) {
if (j >= 0) {
st.insert(j);
} else {
st.erase(st.find(~j));
}
}
if (!st.empty() && s[i] >= *st.begin()) {
seq[i] = 1;
}
}
for (int i = 0; i < n; i++) {
cout << seq[i];
}
cout << '\n';
return 0;
}
Compilation message
trade.cpp: In function 'int main()':
trade.cpp:94:7: warning: unused variable 'from' [-Wunused-variable]
94 | int from = -1, to = -1;
| ^~~~
trade.cpp:94:18: warning: unused variable 'to' [-Wunused-variable]
94 | int from = -1, to = -1;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
4 ms |
600 KB |
Output is correct |
18 |
Correct |
3 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
3 ms |
604 KB |
Output is correct |
23 |
Correct |
5 ms |
8144 KB |
Output is correct |
24 |
Correct |
126 ms |
6360 KB |
Output is correct |
25 |
Correct |
94 ms |
6360 KB |
Output is correct |
26 |
Correct |
110 ms |
6376 KB |
Output is correct |
27 |
Correct |
75 ms |
10688 KB |
Output is correct |
28 |
Correct |
76 ms |
6088 KB |
Output is correct |
29 |
Correct |
41 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
3483 ms |
174100 KB |
Output is correct |
3 |
Correct |
3413 ms |
167732 KB |
Output is correct |
4 |
Correct |
4610 ms |
168344 KB |
Output is correct |
5 |
Correct |
5939 ms |
174328 KB |
Output is correct |
6 |
Correct |
6041 ms |
171872 KB |
Output is correct |
7 |
Correct |
4002 ms |
179100 KB |
Output is correct |
8 |
Correct |
3122 ms |
166192 KB |
Output is correct |
9 |
Correct |
3413 ms |
167720 KB |
Output is correct |
10 |
Correct |
3359 ms |
174048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
3483 ms |
174100 KB |
Output is correct |
3 |
Correct |
3413 ms |
167732 KB |
Output is correct |
4 |
Correct |
4610 ms |
168344 KB |
Output is correct |
5 |
Correct |
5939 ms |
174328 KB |
Output is correct |
6 |
Correct |
6041 ms |
171872 KB |
Output is correct |
7 |
Correct |
4002 ms |
179100 KB |
Output is correct |
8 |
Correct |
3122 ms |
166192 KB |
Output is correct |
9 |
Correct |
3413 ms |
167720 KB |
Output is correct |
10 |
Correct |
3359 ms |
174048 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
3481 ms |
176684 KB |
Output is correct |
13 |
Correct |
3384 ms |
167736 KB |
Output is correct |
14 |
Correct |
4613 ms |
166792 KB |
Output is correct |
15 |
Correct |
5913 ms |
172928 KB |
Output is correct |
16 |
Correct |
6018 ms |
173520 KB |
Output is correct |
17 |
Correct |
4006 ms |
179152 KB |
Output is correct |
18 |
Correct |
3139 ms |
166188 KB |
Output is correct |
19 |
Correct |
3453 ms |
167764 KB |
Output is correct |
20 |
Correct |
3367 ms |
173936 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
31 |
Correct |
3502 ms |
166332 KB |
Output is correct |
32 |
Correct |
6074 ms |
170960 KB |
Output is correct |
33 |
Correct |
5265 ms |
175272 KB |
Output is correct |
34 |
Correct |
6162 ms |
169936 KB |
Output is correct |
35 |
Correct |
5956 ms |
168360 KB |
Output is correct |
36 |
Partially correct |
6313 ms |
167724 KB |
Partially correct |
37 |
Correct |
6692 ms |
214656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
4 ms |
600 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
5 ms |
8144 KB |
Output is correct |
26 |
Correct |
126 ms |
6360 KB |
Output is correct |
27 |
Correct |
94 ms |
6360 KB |
Output is correct |
28 |
Correct |
110 ms |
6376 KB |
Output is correct |
29 |
Correct |
75 ms |
10688 KB |
Output is correct |
30 |
Correct |
76 ms |
6088 KB |
Output is correct |
31 |
Correct |
41 ms |
6236 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
3483 ms |
174100 KB |
Output is correct |
34 |
Correct |
3413 ms |
167732 KB |
Output is correct |
35 |
Correct |
4610 ms |
168344 KB |
Output is correct |
36 |
Correct |
5939 ms |
174328 KB |
Output is correct |
37 |
Correct |
6041 ms |
171872 KB |
Output is correct |
38 |
Correct |
4002 ms |
179100 KB |
Output is correct |
39 |
Correct |
3122 ms |
166192 KB |
Output is correct |
40 |
Correct |
3413 ms |
167720 KB |
Output is correct |
41 |
Correct |
3359 ms |
174048 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
3481 ms |
176684 KB |
Output is correct |
44 |
Correct |
3384 ms |
167736 KB |
Output is correct |
45 |
Correct |
4613 ms |
166792 KB |
Output is correct |
46 |
Correct |
5913 ms |
172928 KB |
Output is correct |
47 |
Correct |
6018 ms |
173520 KB |
Output is correct |
48 |
Correct |
4006 ms |
179152 KB |
Output is correct |
49 |
Correct |
3139 ms |
166188 KB |
Output is correct |
50 |
Correct |
3453 ms |
167764 KB |
Output is correct |
51 |
Correct |
3367 ms |
173936 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
0 ms |
604 KB |
Output is correct |
56 |
Correct |
3 ms |
604 KB |
Output is correct |
57 |
Correct |
2 ms |
604 KB |
Output is correct |
58 |
Correct |
2 ms |
604 KB |
Output is correct |
59 |
Correct |
2 ms |
604 KB |
Output is correct |
60 |
Correct |
2 ms |
604 KB |
Output is correct |
61 |
Correct |
3 ms |
604 KB |
Output is correct |
62 |
Correct |
3502 ms |
166332 KB |
Output is correct |
63 |
Correct |
6074 ms |
170960 KB |
Output is correct |
64 |
Correct |
5265 ms |
175272 KB |
Output is correct |
65 |
Correct |
6162 ms |
169936 KB |
Output is correct |
66 |
Correct |
5956 ms |
168360 KB |
Output is correct |
67 |
Partially correct |
6313 ms |
167724 KB |
Partially correct |
68 |
Correct |
6692 ms |
214656 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
3440 ms |
176536 KB |
Output is correct |
71 |
Correct |
3419 ms |
166200 KB |
Output is correct |
72 |
Correct |
4608 ms |
166868 KB |
Output is correct |
73 |
Correct |
5868 ms |
174284 KB |
Output is correct |
74 |
Correct |
6006 ms |
171996 KB |
Output is correct |
75 |
Correct |
3971 ms |
179148 KB |
Output is correct |
76 |
Correct |
3120 ms |
167852 KB |
Output is correct |
77 |
Correct |
3370 ms |
167808 KB |
Output is correct |
78 |
Correct |
3407 ms |
174032 KB |
Output is correct |
79 |
Correct |
0 ms |
344 KB |
Output is correct |
80 |
Correct |
0 ms |
344 KB |
Output is correct |
81 |
Correct |
2 ms |
604 KB |
Output is correct |
82 |
Correct |
0 ms |
604 KB |
Output is correct |
83 |
Correct |
3 ms |
604 KB |
Output is correct |
84 |
Correct |
2 ms |
604 KB |
Output is correct |
85 |
Correct |
2 ms |
604 KB |
Output is correct |
86 |
Correct |
2 ms |
604 KB |
Output is correct |
87 |
Correct |
2 ms |
604 KB |
Output is correct |
88 |
Correct |
3 ms |
604 KB |
Output is correct |
89 |
Correct |
3558 ms |
166276 KB |
Output is correct |
90 |
Correct |
6019 ms |
170772 KB |
Output is correct |
91 |
Correct |
5385 ms |
175404 KB |
Output is correct |
92 |
Correct |
6270 ms |
171484 KB |
Output is correct |
93 |
Correct |
5984 ms |
169948 KB |
Output is correct |
94 |
Partially correct |
6249 ms |
167764 KB |
Partially correct |
95 |
Correct |
6623 ms |
216152 KB |
Output is correct |
96 |
Correct |
6 ms |
8144 KB |
Output is correct |
97 |
Correct |
126 ms |
6356 KB |
Output is correct |
98 |
Correct |
95 ms |
6224 KB |
Output is correct |
99 |
Correct |
109 ms |
6224 KB |
Output is correct |
100 |
Correct |
76 ms |
10692 KB |
Output is correct |
101 |
Correct |
76 ms |
6108 KB |
Output is correct |
102 |
Correct |
41 ms |
6224 KB |
Output is correct |
103 |
Correct |
3126 ms |
164692 KB |
Output is correct |
104 |
Correct |
6289 ms |
166428 KB |
Output is correct |
105 |
Correct |
6086 ms |
168656 KB |
Output is correct |
106 |
Correct |
5529 ms |
170432 KB |
Output is correct |
107 |
Correct |
302 ms |
292152 KB |
Output is correct |
108 |
Correct |
2639 ms |
169936 KB |
Output is correct |
109 |
Correct |
3696 ms |
377844 KB |
Output is correct |
110 |
Correct |
2816 ms |
176380 KB |
Output is correct |
111 |
Correct |
1655 ms |
169764 KB |
Output is correct |
112 |
Correct |
4970 ms |
250812 KB |
Output is correct |