#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];
}
dp[0] = -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;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 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 |
6 ms |
12124 KB |
Output is correct |
7 |
Correct |
7 ms |
12120 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
6 ms |
12120 KB |
Output is correct |
11 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
6 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 |
6 ms |
12124 KB |
Output is correct |
7 |
Correct |
7 ms |
12120 KB |
Output is correct |
8 |
Correct |
5 ms |
12124 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
6 ms |
12120 KB |
Output is correct |
11 |
Partially correct |
6 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 |
5 ms |
12124 KB |
Output is correct |
15 |
Correct |
6 ms |
12124 KB |
Output is correct |
16 |
Correct |
8 ms |
12124 KB |
Output is correct |
17 |
Correct |
7 ms |
12124 KB |
Output is correct |
18 |
Correct |
8 ms |
12008 KB |
Output is correct |
19 |
Correct |
6 ms |
12124 KB |
Output is correct |
20 |
Correct |
7 ms |
12120 KB |
Output is correct |
21 |
Correct |
8 ms |
12124 KB |
Output is correct |
22 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
23 |
Correct |
16 ms |
12636 KB |
Output is correct |
24 |
Correct |
19 ms |
12552 KB |
Output is correct |
25 |
Correct |
32 ms |
12636 KB |
Output is correct |
26 |
Correct |
32 ms |
12628 KB |
Output is correct |
27 |
Correct |
30 ms |
12888 KB |
Output is correct |
28 |
Correct |
16 ms |
12476 KB |
Output is correct |
29 |
Correct |
29 ms |
12632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
518 ms |
28088 KB |
Output is correct |
3 |
Correct |
829 ms |
32708 KB |
Output is correct |
4 |
Correct |
965 ms |
34132 KB |
Output is correct |
5 |
Correct |
974 ms |
29776 KB |
Output is correct |
6 |
Correct |
879 ms |
27932 KB |
Output is correct |
7 |
Correct |
2508 ms |
43492 KB |
Output is correct |
8 |
Correct |
870 ms |
31060 KB |
Output is correct |
9 |
Correct |
796 ms |
26640 KB |
Output is correct |
10 |
Correct |
718 ms |
27380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12124 KB |
Output is correct |
2 |
Correct |
518 ms |
28088 KB |
Output is correct |
3 |
Correct |
829 ms |
32708 KB |
Output is correct |
4 |
Correct |
965 ms |
34132 KB |
Output is correct |
5 |
Correct |
974 ms |
29776 KB |
Output is correct |
6 |
Correct |
879 ms |
27932 KB |
Output is correct |
7 |
Correct |
2508 ms |
43492 KB |
Output is correct |
8 |
Correct |
870 ms |
31060 KB |
Output is correct |
9 |
Correct |
796 ms |
26640 KB |
Output is correct |
10 |
Correct |
718 ms |
27380 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
501 ms |
28088 KB |
Output is correct |
13 |
Correct |
907 ms |
32916 KB |
Output is correct |
14 |
Correct |
992 ms |
34128 KB |
Output is correct |
15 |
Correct |
988 ms |
29748 KB |
Output is correct |
16 |
Correct |
839 ms |
27840 KB |
Output is correct |
17 |
Correct |
2183 ms |
43744 KB |
Output is correct |
18 |
Correct |
868 ms |
31056 KB |
Output is correct |
19 |
Correct |
733 ms |
26704 KB |
Output is correct |
20 |
Correct |
682 ms |
27412 KB |
Output is correct |
21 |
Correct |
6 ms |
12120 KB |
Output is correct |
22 |
Correct |
5 ms |
12124 KB |
Output is correct |
23 |
Correct |
5 ms |
12204 KB |
Output is correct |
24 |
Correct |
5 ms |
12124 KB |
Output is correct |
25 |
Correct |
6 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 |
7 ms |
12124 KB |
Output is correct |
29 |
Correct |
8 ms |
12124 KB |
Output is correct |
30 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
31 |
Correct |
1347 ms |
33620 KB |
Output is correct |
32 |
Correct |
932 ms |
28140 KB |
Output is correct |
33 |
Correct |
1671 ms |
35008 KB |
Output is correct |
34 |
Correct |
1638 ms |
27636 KB |
Output is correct |
35 |
Correct |
1532 ms |
27728 KB |
Output is correct |
36 |
Correct |
2410 ms |
32592 KB |
Output is correct |
37 |
Correct |
979 ms |
26452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12188 KB |
Output is correct |
3 |
Correct |
6 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 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 |
6 ms |
12124 KB |
Output is correct |
9 |
Correct |
7 ms |
12120 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
6 ms |
12120 KB |
Output is correct |
13 |
Partially correct |
6 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 |
5 ms |
12124 KB |
Output is correct |
17 |
Correct |
6 ms |
12124 KB |
Output is correct |
18 |
Correct |
8 ms |
12124 KB |
Output is correct |
19 |
Correct |
7 ms |
12124 KB |
Output is correct |
20 |
Correct |
8 ms |
12008 KB |
Output is correct |
21 |
Correct |
6 ms |
12124 KB |
Output is correct |
22 |
Correct |
7 ms |
12120 KB |
Output is correct |
23 |
Correct |
8 ms |
12124 KB |
Output is correct |
24 |
Partially correct |
6 ms |
12124 KB |
Partially correct |
25 |
Correct |
16 ms |
12636 KB |
Output is correct |
26 |
Correct |
19 ms |
12552 KB |
Output is correct |
27 |
Correct |
32 ms |
12636 KB |
Output is correct |
28 |
Correct |
32 ms |
12628 KB |
Output is correct |
29 |
Correct |
30 ms |
12888 KB |
Output is correct |
30 |
Correct |
16 ms |
12476 KB |
Output is correct |
31 |
Correct |
29 ms |
12632 KB |
Output is correct |
32 |
Correct |
6 ms |
12124 KB |
Output is correct |
33 |
Correct |
518 ms |
28088 KB |
Output is correct |
34 |
Correct |
829 ms |
32708 KB |
Output is correct |
35 |
Correct |
965 ms |
34132 KB |
Output is correct |
36 |
Correct |
974 ms |
29776 KB |
Output is correct |
37 |
Correct |
879 ms |
27932 KB |
Output is correct |
38 |
Correct |
2508 ms |
43492 KB |
Output is correct |
39 |
Correct |
870 ms |
31060 KB |
Output is correct |
40 |
Correct |
796 ms |
26640 KB |
Output is correct |
41 |
Correct |
718 ms |
27380 KB |
Output is correct |
42 |
Correct |
5 ms |
12124 KB |
Output is correct |
43 |
Correct |
501 ms |
28088 KB |
Output is correct |
44 |
Correct |
907 ms |
32916 KB |
Output is correct |
45 |
Correct |
992 ms |
34128 KB |
Output is correct |
46 |
Correct |
988 ms |
29748 KB |
Output is correct |
47 |
Correct |
839 ms |
27840 KB |
Output is correct |
48 |
Correct |
2183 ms |
43744 KB |
Output is correct |
49 |
Correct |
868 ms |
31056 KB |
Output is correct |
50 |
Correct |
733 ms |
26704 KB |
Output is correct |
51 |
Correct |
682 ms |
27412 KB |
Output is correct |
52 |
Correct |
6 ms |
12120 KB |
Output is correct |
53 |
Correct |
5 ms |
12124 KB |
Output is correct |
54 |
Correct |
5 ms |
12204 KB |
Output is correct |
55 |
Correct |
5 ms |
12124 KB |
Output is correct |
56 |
Correct |
6 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 |
7 ms |
12124 KB |
Output is correct |
60 |
Correct |
8 ms |
12124 KB |
Output is correct |
61 |
Partially correct |
5 ms |
12124 KB |
Partially correct |
62 |
Correct |
1347 ms |
33620 KB |
Output is correct |
63 |
Correct |
932 ms |
28140 KB |
Output is correct |
64 |
Correct |
1671 ms |
35008 KB |
Output is correct |
65 |
Correct |
1638 ms |
27636 KB |
Output is correct |
66 |
Correct |
1532 ms |
27728 KB |
Output is correct |
67 |
Correct |
2410 ms |
32592 KB |
Output is correct |
68 |
Correct |
979 ms |
26452 KB |
Output is correct |
69 |
Correct |
5 ms |
12120 KB |
Output is correct |
70 |
Correct |
472 ms |
28160 KB |
Output is correct |
71 |
Correct |
800 ms |
32848 KB |
Output is correct |
72 |
Correct |
861 ms |
34128 KB |
Output is correct |
73 |
Correct |
913 ms |
29628 KB |
Output is correct |
74 |
Correct |
816 ms |
27988 KB |
Output is correct |
75 |
Correct |
2137 ms |
43676 KB |
Output is correct |
76 |
Correct |
819 ms |
31072 KB |
Output is correct |
77 |
Correct |
744 ms |
26712 KB |
Output is correct |
78 |
Correct |
729 ms |
27396 KB |
Output is correct |
79 |
Correct |
6 ms |
12124 KB |
Output is correct |
80 |
Correct |
4 ms |
12124 KB |
Output is correct |
81 |
Correct |
5 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 |
6 ms |
12080 KB |
Output is correct |
85 |
Correct |
5 ms |
12120 KB |
Output is correct |
86 |
Correct |
5 ms |
12124 KB |
Output is correct |
87 |
Correct |
6 ms |
12124 KB |
Output is correct |
88 |
Partially correct |
6 ms |
12036 KB |
Partially correct |
89 |
Correct |
1357 ms |
33620 KB |
Output is correct |
90 |
Correct |
927 ms |
28296 KB |
Output is correct |
91 |
Correct |
1775 ms |
35152 KB |
Output is correct |
92 |
Correct |
1763 ms |
27784 KB |
Output is correct |
93 |
Correct |
1762 ms |
27520 KB |
Output is correct |
94 |
Correct |
2986 ms |
32392 KB |
Output is correct |
95 |
Correct |
1089 ms |
26396 KB |
Output is correct |
96 |
Correct |
13 ms |
12636 KB |
Output is correct |
97 |
Correct |
27 ms |
12532 KB |
Output is correct |
98 |
Correct |
34 ms |
12648 KB |
Output is correct |
99 |
Correct |
31 ms |
12636 KB |
Output is correct |
100 |
Correct |
35 ms |
12820 KB |
Output is correct |
101 |
Correct |
16 ms |
12636 KB |
Output is correct |
102 |
Correct |
31 ms |
12624 KB |
Output is correct |
103 |
Correct |
4224 ms |
34812 KB |
Output is correct |
104 |
Correct |
1973 ms |
27700 KB |
Output is correct |
105 |
Correct |
2027 ms |
27624 KB |
Output is correct |
106 |
Correct |
3127 ms |
30260 KB |
Output is correct |
107 |
Correct |
3129 ms |
31728 KB |
Output is correct |
108 |
Correct |
3923 ms |
33540 KB |
Output is correct |
109 |
Correct |
2073 ms |
40448 KB |
Output is correct |
110 |
Correct |
2996 ms |
38996 KB |
Output is correct |
111 |
Correct |
1057 ms |
30552 KB |
Output is correct |
112 |
Partially correct |
2287 ms |
30232 KB |
Partially correct |