#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;
long long s2;
int c[N], s[N];
long long pref[N];
multiset<int> se, se2;
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;
}
}
}
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;
cout << solve(k, n, 1, n) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
0 ms |
348 KB |
Partially correct |
2 |
Partially correct |
0 ms |
348 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
344 KB |
Partially correct |
2 |
Partially correct |
0 ms |
604 KB |
Partially correct |
3 |
Partially correct |
0 ms |
348 KB |
Partially correct |
4 |
Partially correct |
1 ms |
348 KB |
Partially correct |
5 |
Partially correct |
0 ms |
348 KB |
Partially correct |
6 |
Partially correct |
1 ms |
344 KB |
Partially correct |
7 |
Partially correct |
1 ms |
348 KB |
Partially correct |
8 |
Partially correct |
1 ms |
348 KB |
Partially correct |
9 |
Partially correct |
1 ms |
348 KB |
Partially correct |
10 |
Partially correct |
1 ms |
348 KB |
Partially correct |
11 |
Partially correct |
1 ms |
348 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
344 KB |
Partially correct |
2 |
Partially correct |
0 ms |
604 KB |
Partially correct |
3 |
Partially correct |
0 ms |
348 KB |
Partially correct |
4 |
Partially correct |
1 ms |
348 KB |
Partially correct |
5 |
Partially correct |
0 ms |
348 KB |
Partially correct |
6 |
Partially correct |
1 ms |
344 KB |
Partially correct |
7 |
Partially correct |
1 ms |
348 KB |
Partially correct |
8 |
Partially correct |
1 ms |
348 KB |
Partially correct |
9 |
Partially correct |
1 ms |
348 KB |
Partially correct |
10 |
Partially correct |
1 ms |
348 KB |
Partially correct |
11 |
Partially correct |
1 ms |
348 KB |
Partially correct |
12 |
Partially correct |
0 ms |
348 KB |
Partially correct |
13 |
Partially correct |
0 ms |
348 KB |
Partially correct |
14 |
Partially correct |
1 ms |
348 KB |
Partially correct |
15 |
Partially correct |
1 ms |
348 KB |
Partially correct |
16 |
Partially correct |
1 ms |
468 KB |
Partially correct |
17 |
Partially correct |
1 ms |
600 KB |
Partially correct |
18 |
Partially correct |
1 ms |
348 KB |
Partially correct |
19 |
Partially correct |
1 ms |
348 KB |
Partially correct |
20 |
Partially correct |
1 ms |
348 KB |
Partially correct |
21 |
Partially correct |
1 ms |
344 KB |
Partially correct |
22 |
Partially correct |
1 ms |
348 KB |
Partially correct |
23 |
Partially correct |
4 ms |
936 KB |
Partially correct |
24 |
Partially correct |
13 ms |
604 KB |
Partially correct |
25 |
Partially correct |
26 ms |
600 KB |
Partially correct |
26 |
Partially correct |
22 ms |
600 KB |
Partially correct |
27 |
Partially correct |
18 ms |
600 KB |
Partially correct |
28 |
Partially correct |
10 ms |
600 KB |
Partially correct |
29 |
Partially correct |
22 ms |
604 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
344 KB |
Partially correct |
2 |
Partially correct |
466 ms |
11072 KB |
Partially correct |
3 |
Partially correct |
692 ms |
13140 KB |
Partially correct |
4 |
Partially correct |
837 ms |
13140 KB |
Partially correct |
5 |
Partially correct |
858 ms |
13164 KB |
Partially correct |
6 |
Partially correct |
767 ms |
12628 KB |
Partially correct |
7 |
Partially correct |
2121 ms |
18136 KB |
Partially correct |
8 |
Partially correct |
739 ms |
13140 KB |
Partially correct |
9 |
Partially correct |
669 ms |
11684 KB |
Partially correct |
10 |
Partially correct |
664 ms |
12316 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
344 KB |
Partially correct |
2 |
Partially correct |
466 ms |
11072 KB |
Partially correct |
3 |
Partially correct |
692 ms |
13140 KB |
Partially correct |
4 |
Partially correct |
837 ms |
13140 KB |
Partially correct |
5 |
Partially correct |
858 ms |
13164 KB |
Partially correct |
6 |
Partially correct |
767 ms |
12628 KB |
Partially correct |
7 |
Partially correct |
2121 ms |
18136 KB |
Partially correct |
8 |
Partially correct |
739 ms |
13140 KB |
Partially correct |
9 |
Partially correct |
669 ms |
11684 KB |
Partially correct |
10 |
Partially correct |
664 ms |
12316 KB |
Partially correct |
11 |
Partially correct |
0 ms |
348 KB |
Partially correct |
12 |
Partially correct |
475 ms |
11144 KB |
Partially correct |
13 |
Partially correct |
675 ms |
13136 KB |
Partially correct |
14 |
Partially correct |
827 ms |
13136 KB |
Partially correct |
15 |
Partially correct |
851 ms |
13136 KB |
Partially correct |
16 |
Partially correct |
761 ms |
12628 KB |
Partially correct |
17 |
Partially correct |
1978 ms |
18260 KB |
Partially correct |
18 |
Partially correct |
763 ms |
13136 KB |
Partially correct |
19 |
Partially correct |
693 ms |
11692 KB |
Partially correct |
20 |
Partially correct |
673 ms |
12316 KB |
Partially correct |
21 |
Partially correct |
0 ms |
348 KB |
Partially correct |
22 |
Partially correct |
0 ms |
348 KB |
Partially correct |
23 |
Partially correct |
1 ms |
348 KB |
Partially correct |
24 |
Partially correct |
0 ms |
348 KB |
Partially correct |
25 |
Partially correct |
1 ms |
344 KB |
Partially correct |
26 |
Partially correct |
2 ms |
344 KB |
Partially correct |
27 |
Partially correct |
1 ms |
348 KB |
Partially correct |
28 |
Partially correct |
1 ms |
348 KB |
Partially correct |
29 |
Partially correct |
1 ms |
344 KB |
Partially correct |
30 |
Partially correct |
1 ms |
348 KB |
Partially correct |
31 |
Partially correct |
1328 ms |
13220 KB |
Partially correct |
32 |
Partially correct |
853 ms |
12984 KB |
Partially correct |
33 |
Partially correct |
1584 ms |
12624 KB |
Partially correct |
34 |
Partially correct |
1546 ms |
12628 KB |
Partially correct |
35 |
Partially correct |
1449 ms |
12492 KB |
Partially correct |
36 |
Partially correct |
2577 ms |
17404 KB |
Partially correct |
37 |
Partially correct |
1014 ms |
11216 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
0 ms |
348 KB |
Partially correct |
2 |
Partially correct |
0 ms |
348 KB |
Partially correct |
3 |
Partially correct |
1 ms |
344 KB |
Partially correct |
4 |
Partially correct |
0 ms |
604 KB |
Partially correct |
5 |
Partially correct |
0 ms |
348 KB |
Partially correct |
6 |
Partially correct |
1 ms |
348 KB |
Partially correct |
7 |
Partially correct |
0 ms |
348 KB |
Partially correct |
8 |
Partially correct |
1 ms |
344 KB |
Partially correct |
9 |
Partially correct |
1 ms |
348 KB |
Partially correct |
10 |
Partially correct |
1 ms |
348 KB |
Partially correct |
11 |
Partially correct |
1 ms |
348 KB |
Partially correct |
12 |
Partially correct |
1 ms |
348 KB |
Partially correct |
13 |
Partially correct |
1 ms |
348 KB |
Partially correct |
14 |
Partially correct |
0 ms |
348 KB |
Partially correct |
15 |
Partially correct |
0 ms |
348 KB |
Partially correct |
16 |
Partially correct |
1 ms |
348 KB |
Partially correct |
17 |
Partially correct |
1 ms |
348 KB |
Partially correct |
18 |
Partially correct |
1 ms |
468 KB |
Partially correct |
19 |
Partially correct |
1 ms |
600 KB |
Partially correct |
20 |
Partially correct |
1 ms |
348 KB |
Partially correct |
21 |
Partially correct |
1 ms |
348 KB |
Partially correct |
22 |
Partially correct |
1 ms |
348 KB |
Partially correct |
23 |
Partially correct |
1 ms |
344 KB |
Partially correct |
24 |
Partially correct |
1 ms |
348 KB |
Partially correct |
25 |
Partially correct |
4 ms |
936 KB |
Partially correct |
26 |
Partially correct |
13 ms |
604 KB |
Partially correct |
27 |
Partially correct |
26 ms |
600 KB |
Partially correct |
28 |
Partially correct |
22 ms |
600 KB |
Partially correct |
29 |
Partially correct |
18 ms |
600 KB |
Partially correct |
30 |
Partially correct |
10 ms |
600 KB |
Partially correct |
31 |
Partially correct |
22 ms |
604 KB |
Partially correct |
32 |
Partially correct |
1 ms |
344 KB |
Partially correct |
33 |
Partially correct |
466 ms |
11072 KB |
Partially correct |
34 |
Partially correct |
692 ms |
13140 KB |
Partially correct |
35 |
Partially correct |
837 ms |
13140 KB |
Partially correct |
36 |
Partially correct |
858 ms |
13164 KB |
Partially correct |
37 |
Partially correct |
767 ms |
12628 KB |
Partially correct |
38 |
Partially correct |
2121 ms |
18136 KB |
Partially correct |
39 |
Partially correct |
739 ms |
13140 KB |
Partially correct |
40 |
Partially correct |
669 ms |
11684 KB |
Partially correct |
41 |
Partially correct |
664 ms |
12316 KB |
Partially correct |
42 |
Partially correct |
0 ms |
348 KB |
Partially correct |
43 |
Partially correct |
475 ms |
11144 KB |
Partially correct |
44 |
Partially correct |
675 ms |
13136 KB |
Partially correct |
45 |
Partially correct |
827 ms |
13136 KB |
Partially correct |
46 |
Partially correct |
851 ms |
13136 KB |
Partially correct |
47 |
Partially correct |
761 ms |
12628 KB |
Partially correct |
48 |
Partially correct |
1978 ms |
18260 KB |
Partially correct |
49 |
Partially correct |
763 ms |
13136 KB |
Partially correct |
50 |
Partially correct |
693 ms |
11692 KB |
Partially correct |
51 |
Partially correct |
673 ms |
12316 KB |
Partially correct |
52 |
Partially correct |
0 ms |
348 KB |
Partially correct |
53 |
Partially correct |
0 ms |
348 KB |
Partially correct |
54 |
Partially correct |
1 ms |
348 KB |
Partially correct |
55 |
Partially correct |
0 ms |
348 KB |
Partially correct |
56 |
Partially correct |
1 ms |
344 KB |
Partially correct |
57 |
Partially correct |
2 ms |
344 KB |
Partially correct |
58 |
Partially correct |
1 ms |
348 KB |
Partially correct |
59 |
Partially correct |
1 ms |
348 KB |
Partially correct |
60 |
Partially correct |
1 ms |
344 KB |
Partially correct |
61 |
Partially correct |
1 ms |
348 KB |
Partially correct |
62 |
Partially correct |
1328 ms |
13220 KB |
Partially correct |
63 |
Partially correct |
853 ms |
12984 KB |
Partially correct |
64 |
Partially correct |
1584 ms |
12624 KB |
Partially correct |
65 |
Partially correct |
1546 ms |
12628 KB |
Partially correct |
66 |
Partially correct |
1449 ms |
12492 KB |
Partially correct |
67 |
Partially correct |
2577 ms |
17404 KB |
Partially correct |
68 |
Partially correct |
1014 ms |
11216 KB |
Partially correct |
69 |
Partially correct |
1 ms |
348 KB |
Partially correct |
70 |
Partially correct |
514 ms |
11200 KB |
Partially correct |
71 |
Partially correct |
733 ms |
13324 KB |
Partially correct |
72 |
Partially correct |
875 ms |
13156 KB |
Partially correct |
73 |
Partially correct |
861 ms |
13140 KB |
Partially correct |
74 |
Partially correct |
785 ms |
12624 KB |
Partially correct |
75 |
Partially correct |
2263 ms |
18084 KB |
Partially correct |
76 |
Partially correct |
807 ms |
13256 KB |
Partially correct |
77 |
Partially correct |
747 ms |
11680 KB |
Partially correct |
78 |
Partially correct |
714 ms |
12316 KB |
Partially correct |
79 |
Partially correct |
0 ms |
344 KB |
Partially correct |
80 |
Partially correct |
1 ms |
348 KB |
Partially correct |
81 |
Partially correct |
1 ms |
348 KB |
Partially correct |
82 |
Partially correct |
0 ms |
348 KB |
Partially correct |
83 |
Partially correct |
1 ms |
348 KB |
Partially correct |
84 |
Partially correct |
1 ms |
348 KB |
Partially correct |
85 |
Partially correct |
1 ms |
344 KB |
Partially correct |
86 |
Partially correct |
1 ms |
348 KB |
Partially correct |
87 |
Partially correct |
1 ms |
348 KB |
Partially correct |
88 |
Partially correct |
1 ms |
348 KB |
Partially correct |
89 |
Partially correct |
1409 ms |
13184 KB |
Partially correct |
90 |
Partially correct |
969 ms |
13136 KB |
Partially correct |
91 |
Partially correct |
1819 ms |
12840 KB |
Partially correct |
92 |
Partially correct |
1691 ms |
12632 KB |
Partially correct |
93 |
Partially correct |
1613 ms |
12488 KB |
Partially correct |
94 |
Partially correct |
2946 ms |
17492 KB |
Partially correct |
95 |
Partially correct |
1031 ms |
11208 KB |
Partially correct |
96 |
Partially correct |
4 ms |
860 KB |
Partially correct |
97 |
Partially correct |
22 ms |
784 KB |
Partially correct |
98 |
Partially correct |
25 ms |
604 KB |
Partially correct |
99 |
Partially correct |
29 ms |
604 KB |
Partially correct |
100 |
Partially correct |
18 ms |
856 KB |
Partially correct |
101 |
Partially correct |
11 ms |
604 KB |
Partially correct |
102 |
Partially correct |
31 ms |
736 KB |
Partially correct |
103 |
Partially correct |
3564 ms |
17992 KB |
Partially correct |
104 |
Partially correct |
1729 ms |
12420 KB |
Partially correct |
105 |
Partially correct |
1746 ms |
12624 KB |
Partially correct |
106 |
Partially correct |
2477 ms |
13996 KB |
Partially correct |
107 |
Partially correct |
2453 ms |
18392 KB |
Partially correct |
108 |
Partially correct |
3640 ms |
16888 KB |
Partially correct |
109 |
Partially correct |
2325 ms |
16228 KB |
Partially correct |
110 |
Partially correct |
3355 ms |
17524 KB |
Partially correct |
111 |
Partially correct |
1031 ms |
16464 KB |
Partially correct |
112 |
Partially correct |
2241 ms |
14672 KB |
Partially correct |