#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pli = pair<ll, int>;
#define pb push_back
#define ff first
#define ss second
const ll infm = -1e18;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
const int A = 1e9;
int n, k; cin>>n>>k;
vector<int> a(n + 1);
vector<ll> p(n + 1);
mt19937 rng((int) time(0));
for (int i = 1; i <= n; i++){
cin>>a[i];
p[i] = p[i - 1] + a[i];
// a[i] = rng() % A + 1;
}
vector<int> b(n + 1);
for (int i = 1; i <= n; i++){
cin>>b[i];
// b[i] = rng() % A + 1;
}
auto get = [&](int l, int r){
vector<int> all;
for (int i = l; i <= r; i++){
all.pb(b[i]);
}
sort(all.begin(), all.end(), greater<int>());
ll out = 0;
for (int i = 0; i < k; i++){
out += all[i];
}
return out;
};
auto f = [&](int l, int r){
if ((r - l + 1) < k) return infm;
return get(l, r) - (p[r] - p[l - 1]);
};
ll out = infm;
function<void(int, int, int, int)> solve = [&](int l, int r, int l1, int r1){
if (l > r) return;
int m = (l + r) / 2;
pli opt = {infm, 0};
for (int i = max(m, l1); i <= r1; i++){
opt = max(opt, {f(m, i), i});
}
out = max(out, opt.ff);
solve(l, m - 1, l1, opt.ss);
solve(m + 1, r, opt.ss, r1);
};
solve(1, n, 1, n);
cout<<out<<"\n";
}
# | 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... |