#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 |
1 ms |
348 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
348 KB |
Partially correct |
2 |
Partially correct |
1 ms |
348 KB |
Partially correct |
3 |
Partially correct |
0 ms |
464 KB |
Partially correct |
4 |
Partially correct |
1 ms |
348 KB |
Partially correct |
5 |
Partially correct |
1 ms |
348 KB |
Partially correct |
6 |
Partially correct |
1 ms |
348 KB |
Partially correct |
7 |
Partially correct |
1 ms |
464 KB |
Partially correct |
8 |
Partially correct |
0 ms |
348 KB |
Partially correct |
9 |
Partially correct |
1 ms |
344 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 |
348 KB |
Partially correct |
2 |
Partially correct |
1 ms |
348 KB |
Partially correct |
3 |
Partially correct |
0 ms |
464 KB |
Partially correct |
4 |
Partially correct |
1 ms |
348 KB |
Partially correct |
5 |
Partially correct |
1 ms |
348 KB |
Partially correct |
6 |
Partially correct |
1 ms |
348 KB |
Partially correct |
7 |
Partially correct |
1 ms |
464 KB |
Partially correct |
8 |
Partially correct |
0 ms |
348 KB |
Partially correct |
9 |
Partially correct |
1 ms |
344 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 |
1 ms |
348 KB |
Partially correct |
14 |
Partially correct |
0 ms |
348 KB |
Partially correct |
15 |
Partially correct |
1 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 |
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 |
348 KB |
Partially correct |
22 |
Partially correct |
1 ms |
348 KB |
Partially correct |
23 |
Partially correct |
4 ms |
860 KB |
Partially correct |
24 |
Partially correct |
13 ms |
588 KB |
Partially correct |
25 |
Partially correct |
29 ms |
604 KB |
Partially correct |
26 |
Partially correct |
21 ms |
604 KB |
Partially correct |
27 |
Partially correct |
17 ms |
600 KB |
Partially correct |
28 |
Partially correct |
9 ms |
604 KB |
Partially correct |
29 |
Partially correct |
32 ms |
604 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
344 KB |
Partially correct |
2 |
Partially correct |
528 ms |
11116 KB |
Partially correct |
3 |
Partially correct |
705 ms |
12884 KB |
Partially correct |
4 |
Partially correct |
869 ms |
12880 KB |
Partially correct |
5 |
Partially correct |
897 ms |
12628 KB |
Partially correct |
6 |
Partially correct |
779 ms |
12468 KB |
Partially correct |
7 |
Partially correct |
2391 ms |
18004 KB |
Partially correct |
8 |
Partially correct |
812 ms |
12780 KB |
Partially correct |
9 |
Partially correct |
766 ms |
11604 KB |
Partially correct |
10 |
Partially correct |
702 ms |
12272 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
344 KB |
Partially correct |
2 |
Partially correct |
528 ms |
11116 KB |
Partially correct |
3 |
Partially correct |
705 ms |
12884 KB |
Partially correct |
4 |
Partially correct |
869 ms |
12880 KB |
Partially correct |
5 |
Partially correct |
897 ms |
12628 KB |
Partially correct |
6 |
Partially correct |
779 ms |
12468 KB |
Partially correct |
7 |
Partially correct |
2391 ms |
18004 KB |
Partially correct |
8 |
Partially correct |
812 ms |
12780 KB |
Partially correct |
9 |
Partially correct |
766 ms |
11604 KB |
Partially correct |
10 |
Partially correct |
702 ms |
12272 KB |
Partially correct |
11 |
Partially correct |
1 ms |
348 KB |
Partially correct |
12 |
Partially correct |
480 ms |
11204 KB |
Partially correct |
13 |
Partially correct |
741 ms |
13036 KB |
Partially correct |
14 |
Partially correct |
916 ms |
12872 KB |
Partially correct |
15 |
Partially correct |
945 ms |
12704 KB |
Partially correct |
16 |
Partially correct |
824 ms |
12516 KB |
Partially correct |
17 |
Partially correct |
2384 ms |
18012 KB |
Partially correct |
18 |
Partially correct |
878 ms |
12792 KB |
Partially correct |
19 |
Partially correct |
700 ms |
11688 KB |
Partially correct |
20 |
Partially correct |
714 ms |
12116 KB |
Partially correct |
21 |
Partially correct |
0 ms |
348 KB |
Partially correct |
22 |
Partially correct |
1 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 |
348 KB |
Partially correct |
26 |
Partially correct |
1 ms |
348 KB |
Partially correct |
27 |
Partially correct |
1 ms |
348 KB |
Partially correct |
28 |
Partially correct |
1 ms |
600 KB |
Partially correct |
29 |
Partially correct |
1 ms |
472 KB |
Partially correct |
30 |
Partially correct |
1 ms |
348 KB |
Partially correct |
31 |
Partially correct |
1409 ms |
12880 KB |
Partially correct |
32 |
Partially correct |
945 ms |
12008 KB |
Partially correct |
33 |
Partially correct |
1805 ms |
11608 KB |
Partially correct |
34 |
Partially correct |
1672 ms |
11760 KB |
Partially correct |
35 |
Partially correct |
1482 ms |
11344 KB |
Partially correct |
36 |
Partially correct |
2308 ms |
12524 KB |
Partially correct |
37 |
Partially correct |
946 ms |
10900 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
0 ms |
348 KB |
Partially correct |
2 |
Partially correct |
1 ms |
348 KB |
Partially correct |
3 |
Partially correct |
1 ms |
348 KB |
Partially correct |
4 |
Partially correct |
1 ms |
348 KB |
Partially correct |
5 |
Partially correct |
0 ms |
464 KB |
Partially correct |
6 |
Partially correct |
1 ms |
348 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 |
464 KB |
Partially correct |
10 |
Partially correct |
0 ms |
348 KB |
Partially correct |
11 |
Partially correct |
1 ms |
344 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 |
1 ms |
348 KB |
Partially correct |
16 |
Partially correct |
0 ms |
348 KB |
Partially correct |
17 |
Partially correct |
1 ms |
348 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 |
348 KB |
Partially correct |
22 |
Partially correct |
1 ms |
348 KB |
Partially correct |
23 |
Partially correct |
1 ms |
348 KB |
Partially correct |
24 |
Partially correct |
1 ms |
348 KB |
Partially correct |
25 |
Partially correct |
4 ms |
860 KB |
Partially correct |
26 |
Partially correct |
13 ms |
588 KB |
Partially correct |
27 |
Partially correct |
29 ms |
604 KB |
Partially correct |
28 |
Partially correct |
21 ms |
604 KB |
Partially correct |
29 |
Partially correct |
17 ms |
600 KB |
Partially correct |
30 |
Partially correct |
9 ms |
604 KB |
Partially correct |
31 |
Partially correct |
32 ms |
604 KB |
Partially correct |
32 |
Partially correct |
1 ms |
344 KB |
Partially correct |
33 |
Partially correct |
528 ms |
11116 KB |
Partially correct |
34 |
Partially correct |
705 ms |
12884 KB |
Partially correct |
35 |
Partially correct |
869 ms |
12880 KB |
Partially correct |
36 |
Partially correct |
897 ms |
12628 KB |
Partially correct |
37 |
Partially correct |
779 ms |
12468 KB |
Partially correct |
38 |
Partially correct |
2391 ms |
18004 KB |
Partially correct |
39 |
Partially correct |
812 ms |
12780 KB |
Partially correct |
40 |
Partially correct |
766 ms |
11604 KB |
Partially correct |
41 |
Partially correct |
702 ms |
12272 KB |
Partially correct |
42 |
Partially correct |
1 ms |
348 KB |
Partially correct |
43 |
Partially correct |
480 ms |
11204 KB |
Partially correct |
44 |
Partially correct |
741 ms |
13036 KB |
Partially correct |
45 |
Partially correct |
916 ms |
12872 KB |
Partially correct |
46 |
Partially correct |
945 ms |
12704 KB |
Partially correct |
47 |
Partially correct |
824 ms |
12516 KB |
Partially correct |
48 |
Partially correct |
2384 ms |
18012 KB |
Partially correct |
49 |
Partially correct |
878 ms |
12792 KB |
Partially correct |
50 |
Partially correct |
700 ms |
11688 KB |
Partially correct |
51 |
Partially correct |
714 ms |
12116 KB |
Partially correct |
52 |
Partially correct |
0 ms |
348 KB |
Partially correct |
53 |
Partially correct |
1 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 |
348 KB |
Partially correct |
57 |
Partially correct |
1 ms |
348 KB |
Partially correct |
58 |
Partially correct |
1 ms |
348 KB |
Partially correct |
59 |
Partially correct |
1 ms |
600 KB |
Partially correct |
60 |
Partially correct |
1 ms |
472 KB |
Partially correct |
61 |
Partially correct |
1 ms |
348 KB |
Partially correct |
62 |
Partially correct |
1409 ms |
12880 KB |
Partially correct |
63 |
Partially correct |
945 ms |
12008 KB |
Partially correct |
64 |
Partially correct |
1805 ms |
11608 KB |
Partially correct |
65 |
Partially correct |
1672 ms |
11760 KB |
Partially correct |
66 |
Partially correct |
1482 ms |
11344 KB |
Partially correct |
67 |
Partially correct |
2308 ms |
12524 KB |
Partially correct |
68 |
Partially correct |
946 ms |
10900 KB |
Partially correct |
69 |
Partially correct |
1 ms |
348 KB |
Partially correct |
70 |
Partially correct |
453 ms |
10088 KB |
Partially correct |
71 |
Partially correct |
696 ms |
10068 KB |
Partially correct |
72 |
Partially correct |
858 ms |
10220 KB |
Partially correct |
73 |
Partially correct |
901 ms |
10064 KB |
Partially correct |
74 |
Partially correct |
771 ms |
10068 KB |
Partially correct |
75 |
Partially correct |
2147 ms |
15952 KB |
Partially correct |
76 |
Partially correct |
776 ms |
10216 KB |
Partially correct |
77 |
Partially correct |
674 ms |
10072 KB |
Partially correct |
78 |
Partially correct |
688 ms |
10064 KB |
Partially correct |
79 |
Partially correct |
0 ms |
348 KB |
Partially correct |
80 |
Partially correct |
0 ms |
348 KB |
Partially correct |
81 |
Partially correct |
1 ms |
344 KB |
Partially correct |
82 |
Partially correct |
0 ms |
344 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 |
348 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 |
1317 ms |
10052 KB |
Partially correct |
90 |
Partially correct |
864 ms |
10064 KB |
Partially correct |
91 |
Partially correct |
1633 ms |
10064 KB |
Partially correct |
92 |
Partially correct |
1586 ms |
10068 KB |
Partially correct |
93 |
Partially correct |
1480 ms |
10224 KB |
Partially correct |
94 |
Partially correct |
2280 ms |
11320 KB |
Partially correct |
95 |
Partially correct |
931 ms |
10220 KB |
Partially correct |
96 |
Partially correct |
4 ms |
600 KB |
Partially correct |
97 |
Partially correct |
13 ms |
600 KB |
Partially correct |
98 |
Partially correct |
25 ms |
604 KB |
Partially correct |
99 |
Partially correct |
21 ms |
604 KB |
Partially correct |
100 |
Partially correct |
16 ms |
604 KB |
Partially correct |
101 |
Partially correct |
9 ms |
600 KB |
Partially correct |
102 |
Partially correct |
22 ms |
604 KB |
Partially correct |
103 |
Partially correct |
3289 ms |
13152 KB |
Partially correct |
104 |
Partially correct |
1678 ms |
10108 KB |
Partially correct |
105 |
Partially correct |
1727 ms |
10128 KB |
Partially correct |
106 |
Partially correct |
2445 ms |
11604 KB |
Partially correct |
107 |
Partially correct |
2427 ms |
15956 KB |
Partially correct |
108 |
Partially correct |
3576 ms |
14416 KB |
Partially correct |
109 |
Partially correct |
1905 ms |
12624 KB |
Partially correct |
110 |
Partially correct |
2730 ms |
12176 KB |
Partially correct |
111 |
Partially correct |
1042 ms |
13140 KB |
Partially correct |
112 |
Partially correct |
2033 ms |
12368 KB |
Partially correct |