#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 250003;
int n, k;
int g[N];
int cl, cr;
bool ok[N];
long long s2;
int c[N], s[N];
long long dp[N];
long long pref[N];
multiset<int> se, se2;
vector<int> to_add[N], to_rem[N];
void balance() {
if ((int) se2.size() > k) {
int mn = *se2.begin();
s2 -= mn;
se.insert(mn);
se2.erase(se2.begin());
}
if ((int) se2.size() < k && !se.empty()) {
int mx = *se.rbegin();
s2 += mx;
se2.insert(mx);
se.erase(se.find(mx));
}
}
void add(int ind) {
se2.insert(s[ind]);
s2 += s[ind];
balance();
}
void rem(int ind) {
if (se2.count(s[ind])) {
s2 -= s[ind];
se2.erase(se2.find(s[ind]));
} else {
se.erase(se.find(s[ind]));
}
balance();
}
void edit(int l, int r) {
while (cl > l) add(--cl);
while (cr < r) add(++cr);
while (cl < l) rem(cl++);
while (cr > r) rem(cr--);
}
long long solve(int l, int r, int optl, int optr) {
if (l > r) {
return -LLONG_MAX;
}
int mid = (l + r) >> 1;
long long ret = -LLONG_MAX;
int opt = -1;
for (int i = min(mid, optr); i >= optl; --i) {
edit(i, mid);
if ((int) se2.size() == k) {
long long val = pref[mid] - pref[i - 1] + s2;
if (val > ret) {
ret = val;
opt = i;
}
}
}
dp[mid] = ret;
ret = max(ret, solve(l, mid - 1, optl, opt));
ret = max(ret, solve(mid + 1, r, opt, optr));
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
pref[i] = pref[i - 1] - c[i];
}
for (int i = 1; i <= n; ++i) {
cin >> s[i];
}
for (int i = 0; i < k; ++i) {
dp[i] = -LLONG_MAX;
}
cl = 1, cr = 0;
long long ans = solve(k, n, 1, n);
for (int i = 1; i <= n; ++i) {
g[i] = (dp[i] == ans);
g[i] += g[i - 1];
}
for (int i = n, j = n; i >= 1; --i) {
if (dp[i] != ans) {
continue;
}
if (j > i) j = i;
//debug(i);
int f = 0;
while (j > 0) {
edit(j, i);
int nf = g[i] - g[j - 1] - (k > 1);
long long val = pref[i] - pref[j - 1] + s2;
if (val == ans && (int) se2.size() == k) {
int mn = *se2.begin();
to_add[j].push_back(mn);
to_rem[i].push_back(mn);
f |= 2;
}
if (f && nf) {
break;
}
--j;
}
}
multiset<int> ss;
for (int i = 1; i <= n; ++i) {
for (int j : to_add[i]) {
ss.insert(j);
}
if (!ss.empty() && *ss.begin() <= s[i]) {
ok[i] = true;
}
for (int j : to_rem[i]) {
ss.erase(ss.find(j));
}
}
cout << ans << '\n';
for (int i = 1; i <= n; ++i) {
cout << ok[i];
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12120 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12020 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12120 KB |
Output is correct |
10 |
Correct |
8 ms |
12120 KB |
Output is correct |
11 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12020 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12120 KB |
Output is correct |
10 |
Correct |
8 ms |
12120 KB |
Output is correct |
11 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
12 |
Correct |
5 ms |
12124 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
6 ms |
12124 KB |
Output is correct |
15 |
Correct |
6 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12376 KB |
Output is correct |
17 |
Correct |
5 ms |
12124 KB |
Output is correct |
18 |
Correct |
5 ms |
12124 KB |
Output is correct |
19 |
Correct |
6 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
21 |
Correct |
6 ms |
12124 KB |
Output is correct |
22 |
Partially correct |
6 ms |
12120 KB |
Partially correct |
23 |
Correct |
11 ms |
12632 KB |
Output is correct |
24 |
Correct |
22 ms |
12476 KB |
Output is correct |
25 |
Correct |
34 ms |
12604 KB |
Output is correct |
26 |
Correct |
43 ms |
12488 KB |
Output is correct |
27 |
Correct |
27 ms |
12896 KB |
Output is correct |
28 |
Correct |
16 ms |
12384 KB |
Output is correct |
29 |
Correct |
28 ms |
12580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
491 ms |
27352 KB |
Output is correct |
3 |
Correct |
827 ms |
29740 KB |
Output is correct |
4 |
Correct |
896 ms |
31004 KB |
Output is correct |
5 |
Correct |
946 ms |
26840 KB |
Output is correct |
6 |
Correct |
845 ms |
25172 KB |
Output is correct |
7 |
Correct |
2183 ms |
41556 KB |
Output is correct |
8 |
Correct |
790 ms |
27968 KB |
Output is correct |
9 |
Correct |
784 ms |
25168 KB |
Output is correct |
10 |
Correct |
700 ms |
25428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12120 KB |
Output is correct |
2 |
Correct |
491 ms |
27352 KB |
Output is correct |
3 |
Correct |
827 ms |
29740 KB |
Output is correct |
4 |
Correct |
896 ms |
31004 KB |
Output is correct |
5 |
Correct |
946 ms |
26840 KB |
Output is correct |
6 |
Correct |
845 ms |
25172 KB |
Output is correct |
7 |
Correct |
2183 ms |
41556 KB |
Output is correct |
8 |
Correct |
790 ms |
27968 KB |
Output is correct |
9 |
Correct |
784 ms |
25168 KB |
Output is correct |
10 |
Correct |
700 ms |
25428 KB |
Output is correct |
11 |
Correct |
6 ms |
12124 KB |
Output is correct |
12 |
Correct |
500 ms |
27112 KB |
Output is correct |
13 |
Correct |
763 ms |
29576 KB |
Output is correct |
14 |
Correct |
903 ms |
30952 KB |
Output is correct |
15 |
Correct |
947 ms |
26788 KB |
Output is correct |
16 |
Correct |
849 ms |
25304 KB |
Output is correct |
17 |
Correct |
2068 ms |
41560 KB |
Output is correct |
18 |
Correct |
852 ms |
27984 KB |
Output is correct |
19 |
Correct |
769 ms |
25024 KB |
Output is correct |
20 |
Correct |
700 ms |
25232 KB |
Output is correct |
21 |
Correct |
6 ms |
12124 KB |
Output is correct |
22 |
Correct |
5 ms |
12124 KB |
Output is correct |
23 |
Correct |
5 ms |
12124 KB |
Output is correct |
24 |
Correct |
7 ms |
12124 KB |
Output is correct |
25 |
Correct |
7 ms |
12124 KB |
Output is correct |
26 |
Correct |
5 ms |
12124 KB |
Output is correct |
27 |
Correct |
5 ms |
12124 KB |
Output is correct |
28 |
Correct |
5 ms |
12124 KB |
Output is correct |
29 |
Correct |
6 ms |
12124 KB |
Output is correct |
30 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
31 |
Correct |
1392 ms |
30292 KB |
Output is correct |
32 |
Correct |
933 ms |
25308 KB |
Output is correct |
33 |
Correct |
1790 ms |
32448 KB |
Output is correct |
34 |
Correct |
1664 ms |
25168 KB |
Output is correct |
35 |
Correct |
1554 ms |
25316 KB |
Output is correct |
36 |
Correct |
2385 ms |
30956 KB |
Output is correct |
37 |
Correct |
992 ms |
25580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12120 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12020 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
5 ms |
12120 KB |
Output is correct |
12 |
Correct |
8 ms |
12120 KB |
Output is correct |
13 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
14 |
Correct |
5 ms |
12124 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
6 ms |
12124 KB |
Output is correct |
17 |
Correct |
6 ms |
12124 KB |
Output is correct |
18 |
Correct |
5 ms |
12376 KB |
Output is correct |
19 |
Correct |
5 ms |
12124 KB |
Output is correct |
20 |
Correct |
5 ms |
12124 KB |
Output is correct |
21 |
Correct |
6 ms |
12124 KB |
Output is correct |
22 |
Correct |
5 ms |
12124 KB |
Output is correct |
23 |
Correct |
6 ms |
12124 KB |
Output is correct |
24 |
Partially correct |
6 ms |
12120 KB |
Partially correct |
25 |
Correct |
11 ms |
12632 KB |
Output is correct |
26 |
Correct |
22 ms |
12476 KB |
Output is correct |
27 |
Correct |
34 ms |
12604 KB |
Output is correct |
28 |
Correct |
43 ms |
12488 KB |
Output is correct |
29 |
Correct |
27 ms |
12896 KB |
Output is correct |
30 |
Correct |
16 ms |
12384 KB |
Output is correct |
31 |
Correct |
28 ms |
12580 KB |
Output is correct |
32 |
Correct |
5 ms |
12120 KB |
Output is correct |
33 |
Correct |
491 ms |
27352 KB |
Output is correct |
34 |
Correct |
827 ms |
29740 KB |
Output is correct |
35 |
Correct |
896 ms |
31004 KB |
Output is correct |
36 |
Correct |
946 ms |
26840 KB |
Output is correct |
37 |
Correct |
845 ms |
25172 KB |
Output is correct |
38 |
Correct |
2183 ms |
41556 KB |
Output is correct |
39 |
Correct |
790 ms |
27968 KB |
Output is correct |
40 |
Correct |
784 ms |
25168 KB |
Output is correct |
41 |
Correct |
700 ms |
25428 KB |
Output is correct |
42 |
Correct |
6 ms |
12124 KB |
Output is correct |
43 |
Correct |
500 ms |
27112 KB |
Output is correct |
44 |
Correct |
763 ms |
29576 KB |
Output is correct |
45 |
Correct |
903 ms |
30952 KB |
Output is correct |
46 |
Correct |
947 ms |
26788 KB |
Output is correct |
47 |
Correct |
849 ms |
25304 KB |
Output is correct |
48 |
Correct |
2068 ms |
41560 KB |
Output is correct |
49 |
Correct |
852 ms |
27984 KB |
Output is correct |
50 |
Correct |
769 ms |
25024 KB |
Output is correct |
51 |
Correct |
700 ms |
25232 KB |
Output is correct |
52 |
Correct |
6 ms |
12124 KB |
Output is correct |
53 |
Correct |
5 ms |
12124 KB |
Output is correct |
54 |
Correct |
5 ms |
12124 KB |
Output is correct |
55 |
Correct |
7 ms |
12124 KB |
Output is correct |
56 |
Correct |
7 ms |
12124 KB |
Output is correct |
57 |
Correct |
5 ms |
12124 KB |
Output is correct |
58 |
Correct |
5 ms |
12124 KB |
Output is correct |
59 |
Correct |
5 ms |
12124 KB |
Output is correct |
60 |
Correct |
6 ms |
12124 KB |
Output is correct |
61 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
62 |
Correct |
1392 ms |
30292 KB |
Output is correct |
63 |
Correct |
933 ms |
25308 KB |
Output is correct |
64 |
Correct |
1790 ms |
32448 KB |
Output is correct |
65 |
Correct |
1664 ms |
25168 KB |
Output is correct |
66 |
Correct |
1554 ms |
25316 KB |
Output is correct |
67 |
Correct |
2385 ms |
30956 KB |
Output is correct |
68 |
Correct |
992 ms |
25580 KB |
Output is correct |
69 |
Correct |
5 ms |
12124 KB |
Output is correct |
70 |
Correct |
487 ms |
27220 KB |
Output is correct |
71 |
Correct |
803 ms |
29776 KB |
Output is correct |
72 |
Correct |
908 ms |
30856 KB |
Output is correct |
73 |
Correct |
933 ms |
26704 KB |
Output is correct |
74 |
Correct |
836 ms |
25348 KB |
Output is correct |
75 |
Correct |
2106 ms |
41548 KB |
Output is correct |
76 |
Correct |
829 ms |
27984 KB |
Output is correct |
77 |
Correct |
748 ms |
25204 KB |
Output is correct |
78 |
Correct |
702 ms |
25284 KB |
Output is correct |
79 |
Correct |
5 ms |
12124 KB |
Output is correct |
80 |
Correct |
5 ms |
12028 KB |
Output is correct |
81 |
Correct |
6 ms |
12124 KB |
Output is correct |
82 |
Correct |
6 ms |
12124 KB |
Output is correct |
83 |
Correct |
6 ms |
12124 KB |
Output is correct |
84 |
Correct |
5 ms |
12124 KB |
Output is correct |
85 |
Correct |
5 ms |
12124 KB |
Output is correct |
86 |
Correct |
7 ms |
12124 KB |
Output is correct |
87 |
Correct |
5 ms |
12124 KB |
Output is correct |
88 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
89 |
Correct |
1373 ms |
30320 KB |
Output is correct |
90 |
Correct |
949 ms |
25172 KB |
Output is correct |
91 |
Correct |
1690 ms |
32460 KB |
Output is correct |
92 |
Correct |
1641 ms |
25168 KB |
Output is correct |
93 |
Correct |
1538 ms |
25240 KB |
Output is correct |
94 |
Correct |
2363 ms |
30948 KB |
Output is correct |
95 |
Correct |
999 ms |
25612 KB |
Output is correct |
96 |
Correct |
10 ms |
12636 KB |
Output is correct |
97 |
Correct |
23 ms |
12380 KB |
Output is correct |
98 |
Correct |
34 ms |
12592 KB |
Output is correct |
99 |
Correct |
29 ms |
12632 KB |
Output is correct |
100 |
Correct |
23 ms |
12840 KB |
Output is correct |
101 |
Correct |
15 ms |
12376 KB |
Output is correct |
102 |
Correct |
28 ms |
12572 KB |
Output is correct |
103 |
Correct |
3396 ms |
30996 KB |
Output is correct |
104 |
Correct |
1771 ms |
25260 KB |
Output is correct |
105 |
Correct |
1895 ms |
25172 KB |
Output is correct |
106 |
Correct |
2659 ms |
28144 KB |
Output is correct |
107 |
Correct |
2521 ms |
31256 KB |
Output is correct |
108 |
Correct |
3789 ms |
32084 KB |
Output is correct |
109 |
Correct |
2079 ms |
40392 KB |
Output is correct |
110 |
Correct |
2917 ms |
37840 KB |
Output is correct |
111 |
Correct |
1047 ms |
27984 KB |
Output is correct |
112 |
Partially correct |
2295 ms |
29636 KB |
Partially correct |