#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define tp tuple<ll, ll, ll>
const ll inf = 1e18;
ll ans = -inf;
ll n, k;
vll b, s;
vll pre;
void dc(ll l, ll r, ll optl, ll optr) {
if (l > r)
return;
if (r - l + 1 < k)
return;
ll mid = (l + r) / 2;
ll need_start = mid + (k - 1);
if (optr < need_start) {
dc(l, mid - 1, optl, min(optr, mid - 1));
dc(mid + 1, r, max(optl, mid + 1), optr);
return;
}
ll st = max({optl, need_start});
ll ed = optr;
priority_queue<ll, vector<ll>, greater<ll>> pq;
ll sum_k_best = 0;
for (ll i = mid; i < st; ++i) {
pq.push(s[i]);
sum_k_best += s[i];
if ((int)pq.size() > (int)k) {
sum_k_best -= pq.top();
pq.pop();
}
}
ll ans_mid = -inf, opt = st - 1;
for (ll i = st; i <= ed; ++i) {
pq.push(s[i]);
sum_k_best += s[i];
if ((int)pq.size() > (int)k) {
sum_k_best -= pq.top();
pq.pop();
}
ll len = i - mid + 1;
if (len >= k) {
ll sum = pre[i] - (mid == 0 ? 0 : pre[mid - 1]);
ll cur = sum_k_best - sum;
if (cur > ans_mid) {
ans_mid = cur;
opt = i;
}
}
}
if (ans_mid > ans)
ans = ans_mid;
dc(l, mid - 1, optl, max(opt, mid - 1));
dc(mid + 1, r, max(opt, mid + 1), optr);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> k;
b.resize(n);
s.resize(n);
pre.resize(n);
for (ll i = 0; i < n; ++i) {
cin >> b[i];
pre[i] = (i == 0 ? 0 : pre[i - 1]) + b[i];
}
for (ll i = 0; i < n; ++i)
cin >> s[i];
dc(0, n - 1, 0, n - 1);
cout << ans << endl;
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |