#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 250003;
int n, k;
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];
}
cl = 1, cr = 0;
long long ans = solve(k, n, 1, n);
for (int i = n, j = n; i >= 1; --i) {
if (dp[i] != ans) {
continue;
}
//debug(i, j);
//debug(i, j);
if (j > i) j = i;
int f = 0;
while (j > 0) {
edit(j, i);
if (dp[j] == ans && (j < i || k == 1)) {
f |= 2;
}
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 |= 1;
}
if (f == 3) {
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 |
12204 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
6 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12208 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
4 ms |
12016 KB |
Output is correct |
6 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
7 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
8 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
9 |
Correct |
7 ms |
12208 KB |
Output is correct |
10 |
Correct |
8 ms |
12124 KB |
Output is correct |
11 |
Partially correct |
8 ms |
12124 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
6 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12208 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
4 ms |
12016 KB |
Output is correct |
6 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
7 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
8 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
9 |
Correct |
7 ms |
12208 KB |
Output is correct |
10 |
Correct |
8 ms |
12124 KB |
Output is correct |
11 |
Partially correct |
8 ms |
12124 KB |
Partially correct |
12 |
Correct |
7 ms |
12120 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12124 KB |
Output is correct |
15 |
Correct |
5 ms |
12120 KB |
Output is correct |
16 |
Correct |
8 ms |
12204 KB |
Output is correct |
17 |
Partially correct |
6 ms |
12120 KB |
Partially correct |
18 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
19 |
Partially correct |
8 ms |
12124 KB |
Partially correct |
20 |
Correct |
7 ms |
12124 KB |
Output is correct |
21 |
Correct |
6 ms |
12168 KB |
Output is correct |
22 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
23 |
Correct |
12 ms |
12560 KB |
Output is correct |
24 |
Partially correct |
21 ms |
12632 KB |
Partially correct |
25 |
Partially correct |
33 ms |
12564 KB |
Partially correct |
26 |
Partially correct |
26 ms |
12636 KB |
Partially correct |
27 |
Partially correct |
24 ms |
12636 KB |
Partially correct |
28 |
Correct |
17 ms |
12632 KB |
Output is correct |
29 |
Correct |
30 ms |
12608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
552 ms |
27364 KB |
Output is correct |
3 |
Correct |
846 ms |
31824 KB |
Output is correct |
4 |
Correct |
972 ms |
33040 KB |
Output is correct |
5 |
Correct |
976 ms |
28860 KB |
Output is correct |
6 |
Correct |
868 ms |
26840 KB |
Output is correct |
7 |
Correct |
2164 ms |
42616 KB |
Output is correct |
8 |
Correct |
838 ms |
30188 KB |
Output is correct |
9 |
Correct |
776 ms |
25500 KB |
Output is correct |
10 |
Correct |
758 ms |
26452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
552 ms |
27364 KB |
Output is correct |
3 |
Correct |
846 ms |
31824 KB |
Output is correct |
4 |
Correct |
972 ms |
33040 KB |
Output is correct |
5 |
Correct |
976 ms |
28860 KB |
Output is correct |
6 |
Correct |
868 ms |
26840 KB |
Output is correct |
7 |
Correct |
2164 ms |
42616 KB |
Output is correct |
8 |
Correct |
838 ms |
30188 KB |
Output is correct |
9 |
Correct |
776 ms |
25500 KB |
Output is correct |
10 |
Correct |
758 ms |
26452 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
488 ms |
27308 KB |
Output is correct |
13 |
Correct |
793 ms |
31824 KB |
Output is correct |
14 |
Correct |
876 ms |
33108 KB |
Output is correct |
15 |
Correct |
946 ms |
28752 KB |
Output is correct |
16 |
Correct |
860 ms |
26964 KB |
Output is correct |
17 |
Correct |
2109 ms |
42524 KB |
Output is correct |
18 |
Correct |
810 ms |
29904 KB |
Output is correct |
19 |
Correct |
744 ms |
25488 KB |
Output is correct |
20 |
Correct |
694 ms |
26448 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 |
12084 KB |
Output is correct |
24 |
Correct |
5 ms |
12120 KB |
Output is correct |
25 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
26 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
27 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
28 |
Correct |
5 ms |
12124 KB |
Output is correct |
29 |
Correct |
6 ms |
12124 KB |
Output is correct |
30 |
Partially correct |
5 ms |
12180 KB |
Partially correct |
31 |
Correct |
1376 ms |
32596 KB |
Output is correct |
32 |
Partially correct |
963 ms |
32796 KB |
Partially correct |
33 |
Partially correct |
1801 ms |
32440 KB |
Partially correct |
34 |
Partially correct |
1743 ms |
32252 KB |
Partially correct |
35 |
Partially correct |
1570 ms |
32116 KB |
Partially correct |
36 |
Correct |
2414 ms |
31528 KB |
Output is correct |
37 |
Partially correct |
1006 ms |
30800 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12204 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 ms |
12124 KB |
Output is correct |
4 |
Correct |
6 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12208 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
4 ms |
12016 KB |
Output is correct |
8 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
9 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
10 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
11 |
Correct |
7 ms |
12208 KB |
Output is correct |
12 |
Correct |
8 ms |
12124 KB |
Output is correct |
13 |
Partially correct |
8 ms |
12124 KB |
Partially correct |
14 |
Correct |
7 ms |
12120 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
16 |
Correct |
5 ms |
12124 KB |
Output is correct |
17 |
Correct |
5 ms |
12120 KB |
Output is correct |
18 |
Correct |
8 ms |
12204 KB |
Output is correct |
19 |
Partially correct |
6 ms |
12120 KB |
Partially correct |
20 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
21 |
Partially correct |
8 ms |
12124 KB |
Partially correct |
22 |
Correct |
7 ms |
12124 KB |
Output is correct |
23 |
Correct |
6 ms |
12168 KB |
Output is correct |
24 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
25 |
Correct |
12 ms |
12560 KB |
Output is correct |
26 |
Partially correct |
21 ms |
12632 KB |
Partially correct |
27 |
Partially correct |
33 ms |
12564 KB |
Partially correct |
28 |
Partially correct |
26 ms |
12636 KB |
Partially correct |
29 |
Partially correct |
24 ms |
12636 KB |
Partially correct |
30 |
Correct |
17 ms |
12632 KB |
Output is correct |
31 |
Correct |
30 ms |
12608 KB |
Output is correct |
32 |
Correct |
6 ms |
12124 KB |
Output is correct |
33 |
Correct |
552 ms |
27364 KB |
Output is correct |
34 |
Correct |
846 ms |
31824 KB |
Output is correct |
35 |
Correct |
972 ms |
33040 KB |
Output is correct |
36 |
Correct |
976 ms |
28860 KB |
Output is correct |
37 |
Correct |
868 ms |
26840 KB |
Output is correct |
38 |
Correct |
2164 ms |
42616 KB |
Output is correct |
39 |
Correct |
838 ms |
30188 KB |
Output is correct |
40 |
Correct |
776 ms |
25500 KB |
Output is correct |
41 |
Correct |
758 ms |
26452 KB |
Output is correct |
42 |
Correct |
5 ms |
12124 KB |
Output is correct |
43 |
Correct |
488 ms |
27308 KB |
Output is correct |
44 |
Correct |
793 ms |
31824 KB |
Output is correct |
45 |
Correct |
876 ms |
33108 KB |
Output is correct |
46 |
Correct |
946 ms |
28752 KB |
Output is correct |
47 |
Correct |
860 ms |
26964 KB |
Output is correct |
48 |
Correct |
2109 ms |
42524 KB |
Output is correct |
49 |
Correct |
810 ms |
29904 KB |
Output is correct |
50 |
Correct |
744 ms |
25488 KB |
Output is correct |
51 |
Correct |
694 ms |
26448 KB |
Output is correct |
52 |
Correct |
6 ms |
12124 KB |
Output is correct |
53 |
Correct |
5 ms |
12124 KB |
Output is correct |
54 |
Correct |
6 ms |
12084 KB |
Output is correct |
55 |
Correct |
5 ms |
12120 KB |
Output is correct |
56 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
57 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
58 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
59 |
Correct |
5 ms |
12124 KB |
Output is correct |
60 |
Correct |
6 ms |
12124 KB |
Output is correct |
61 |
Partially correct |
5 ms |
12180 KB |
Partially correct |
62 |
Correct |
1376 ms |
32596 KB |
Output is correct |
63 |
Partially correct |
963 ms |
32796 KB |
Partially correct |
64 |
Partially correct |
1801 ms |
32440 KB |
Partially correct |
65 |
Partially correct |
1743 ms |
32252 KB |
Partially correct |
66 |
Partially correct |
1570 ms |
32116 KB |
Partially correct |
67 |
Correct |
2414 ms |
31528 KB |
Output is correct |
68 |
Partially correct |
1006 ms |
30800 KB |
Partially correct |
69 |
Correct |
7 ms |
12120 KB |
Output is correct |
70 |
Correct |
494 ms |
27476 KB |
Output is correct |
71 |
Correct |
798 ms |
31944 KB |
Output is correct |
72 |
Correct |
886 ms |
32952 KB |
Output is correct |
73 |
Correct |
956 ms |
28848 KB |
Output is correct |
74 |
Correct |
836 ms |
26964 KB |
Output is correct |
75 |
Correct |
2194 ms |
42852 KB |
Output is correct |
76 |
Correct |
834 ms |
30132 KB |
Output is correct |
77 |
Correct |
776 ms |
25684 KB |
Output is correct |
78 |
Correct |
697 ms |
26448 KB |
Output is correct |
79 |
Correct |
5 ms |
12120 KB |
Output is correct |
80 |
Correct |
6 ms |
12124 KB |
Output is correct |
81 |
Correct |
5 ms |
12124 KB |
Output is correct |
82 |
Correct |
6 ms |
12016 KB |
Output is correct |
83 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
84 |
Partially correct |
6 ms |
12120 KB |
Partially correct |
85 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
86 |
Correct |
6 ms |
12048 KB |
Output is correct |
87 |
Correct |
6 ms |
12124 KB |
Output is correct |
88 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
89 |
Correct |
1368 ms |
32636 KB |
Output is correct |
90 |
Partially correct |
909 ms |
32800 KB |
Partially correct |
91 |
Partially correct |
1657 ms |
32592 KB |
Partially correct |
92 |
Partially correct |
1640 ms |
32336 KB |
Partially correct |
93 |
Partially correct |
1544 ms |
32292 KB |
Partially correct |
94 |
Correct |
2490 ms |
31568 KB |
Output is correct |
95 |
Partially correct |
1000 ms |
30788 KB |
Partially correct |
96 |
Correct |
11 ms |
12632 KB |
Output is correct |
97 |
Partially correct |
24 ms |
12544 KB |
Partially correct |
98 |
Partially correct |
33 ms |
12624 KB |
Partially correct |
99 |
Partially correct |
27 ms |
12628 KB |
Partially correct |
100 |
Partially correct |
27 ms |
12636 KB |
Partially correct |
101 |
Correct |
15 ms |
12636 KB |
Output is correct |
102 |
Correct |
28 ms |
12636 KB |
Output is correct |
103 |
Correct |
3437 ms |
33876 KB |
Output is correct |
104 |
Partially correct |
1803 ms |
32324 KB |
Partially correct |
105 |
Partially correct |
1818 ms |
32336 KB |
Partially correct |
106 |
Partially correct |
2579 ms |
32304 KB |
Partially correct |
107 |
Partially correct |
2600 ms |
30688 KB |
Partially correct |
108 |
Partially correct |
3671 ms |
31452 KB |
Partially correct |
109 |
Partially correct |
1964 ms |
33232 KB |
Partially correct |
110 |
Correct |
2936 ms |
37836 KB |
Output is correct |
111 |
Correct |
1035 ms |
29520 KB |
Output is correct |
112 |
Partially correct |
2337 ms |
33108 KB |
Partially correct |