#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <functional>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, int>> Heap;
template <typename C>
pair<int, int> gtop(C& q, C& dq) {
while (!dq.empty() && dq.top() == q.top()) {
dq.pop();
q.pop();
}
if (q.empty())
return make_pair(-1, -1);
return q.top();
};
pair<int, int> neg(const pair<int, int>& pr) { return make_pair(-pr.first, pr.second); }
void solve() {
int n, k, l;
scanf("%d%d%d", &n, &k, &l);
vector<pair<int, int>> a(n), b(n), tot(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i].first);
a[i].second = i;
}
for (int i = 0; i < n; ++i) {
scanf("%d", &b[i]);
b[i].second = i;
}
vector<int> vis(n);
for (int i = 0; i < n; ++i)
tot[i] = make_pair(a[i].first + b[i].first, i);
if (k == l) {
ll ans = 0;
nth_element(tot.begin(), tot.begin() + k, tot.end(), greater<pair<int, int>>());
for (int i = 0; i < k; ++i)
ans += tot[i].first;
printf("%lld\n", ans);
return;
}
Heap qa(a.begin(), a.end()), dqa,
qb(b.begin(), b.end()), dqb,
qtot(tot.begin(), tot.end()), dqtot,
qab, dqab, qba, dqba;
Heap qda, dqda, qdb, dqdb;
ll ans = 0;
int ac = 0, bc = 0, ad = 0, bd = 0;
for (int rep = 0; rep < k * 2; ++rep) {
pair<int, int> opt(-1, -1);
if (ad < k - l && ac < k) // 0 0 => 1 0
opt = max(opt, make_pair(gtop(qa, dqa).first, 1));
if (bd < k - l && bc < k) // 0 0 => 0 1
opt = max(opt, make_pair(gtop(qb, dqb).first, 2));
if (ad && bc < k) // [1 0] [0 0] => [0 0] [1 1]
opt = max(opt, make_pair(gtop(qtot, dqtot).first + gtop(qda, dqda).first, 3));
if (bd && ac < k) // [0 1] [0 0] => [0 0] [1 1]
opt = max(opt, make_pair(gtop(qtot, dqtot).first + gtop(qdb, dqdb).first, 4));
if (bc < k) // [1 0] => [1 1]
opt = max(opt, make_pair(gtop(qab, dqab).first, 5));
if (ac < k) // [0 1] => [1 1]
opt = max(opt, make_pair(gtop(qba, dqba).first, 6));
ans += opt.first;
int pos, qos;
switch (opt.second) {
case 1:
pos = gtop(qa, dqa).second; qa.pop();
dqb.push(b[pos]);
dqtot.push(tot[pos]);
qab.push(b[pos]);
qda.push(neg(a[pos]));
++ac; ++ad;
break;
case 2:
pos = gtop(qb, dqb).second; qb.pop();
dqa.push(a[pos]);
dqtot.push(tot[pos]);
qba.push(a[pos]);
qdb.push(neg(b[pos]));
++bc; ++bc;
break;
case 3:
pos = gtop(qtot, dqtot).second; qtot.pop();
qos = gtop(qda, dqda).second; qda.pop();
dqab.push(b[qos]);
qb.push(b[qos]);
qtot.push(tot[qos]);
dqa.push(a[pos]);
dqb.push(b[pos]);
--ad; ++bc;
break;
case 4:
pos = gtop(qtot, dqtot).second; qtot.pop();
qos = gtop(qdb, dqdb).second; qdb.pop();
dqba.push(a[qos]);
qa.push(a[qos]);
qtot.push(tot[qos]);
dqa.push(a[pos]);
dqb.push(b[pos]);
--bd; ++ac;
break;
case 5:
pos = gtop(qab, dqab).second; qab.pop();
dqda.push(neg(a[pos]));
++bc;
break;
case 6:
pos = gtop(qba, dqba).second; qba.pop();
dqdb.push(neg(b[pos]));
++ac;
break;
}
}
printf("%lld\n", ans);
}
int main() {
#ifdef LBT
freopen("sequence2.in", "r", stdin);
#endif
int t;
scanf("%d", &t);
while (t--)
solve();
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 32.4 us | 24 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #2 | 49.5 us | 28 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #3 | 38.43 us | 28 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #4 | 56.68 us | 28 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #5 | 69.9 us | 28 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #6 | 78.36 us | 28 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #7 | 95.7 us | 28 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #8 | 28.38 us | 28 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #9 | 355.88 us | 36 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #10 | 366.5 us | 40 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #11 | 1.288 ms | 84 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #12 | 3.18 ms | 180 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #13 | 4.284 ms | 212 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #14 | 4.329 ms | 204 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #15 | 4.326 ms | 204 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #16 | 4.236 ms | 208 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #17 | 22.203 ms | 936 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #18 | 37.949 ms | 3 MB + 992 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #19 | 76.424 ms | 15 MB + 528 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #20 | 78.717 ms | 15 MB + 284 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #21 | 100.712 ms | 15 MB + 184 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #22 | 185.096 ms | 14 MB + 600 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #23 | 216.743 ms | 16 MB + 996 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #24 | 289.6 ms | 17 MB + 880 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #25 | 301.183 ms | 16 MB + 392 KB | Wrong Answer | Score: 0 | 显示更多 |