赛时脑子抽了没想到优先队列,写的线段树但是 WA。求调玄关。码风良好。
#include <bits/stdc++.h>
#define int long long
#define umap unordered_map
using namespace std;
using re = double;
using ll = long long;
using pii = pair<int, int>;
inline int read() {
int res = 0, f = 1, ch = getchar();
while (!isdigit(ch)) (ch == '-') && (f = 0), ch = getchar();
while (isdigit(ch)) res = res * 10 + (ch ^ 48), ch = getchar();
return f ? res : ~res + 1;
};
const int N = 1E6 + 100;
const int Inf = 1E18;
struct Node {
int l, r, cnt, sum;
} st[N << 2];
void Build(int p, int l, int r) {
st[p] = {l, r, 0, 0};
if (l == r) return;
int mid = (l + r) >> 1;
Build(p << 1, l, mid), Build(p << 1 | 1, mid + 1, r);
}
void PushUp(int p) {
st[p].sum = st[p << 1].sum + st[p << 1 | 1].sum;
st[p].cnt = st[p << 1].cnt + st[p << 1 | 1].cnt;
}
void Modify(int p, int t, int d) {
if (st[p].l == st[p].r) return st[p].sum += st[p].l * d, st[p].cnt += d, void();
int mid = (st[p].l + st[p].r) >> 1;
if (t <= mid) Modify(p << 1, t, d);
if (mid < t) Modify(p << 1 | 1, t, d);
PushUp(p);
}
int Query(int p, int k) {
if (st[p].l == st[p].r) return st[p].sum;
if (st[p << 1].cnt >= k) return Query(p << 1, k);
return Query(p << 1 | 1, k - st[p << 1].cnt) + st[p << 1].sum;
}
void Main() {
int n = read(), k = read(), res = Inf;
vector<pii> s(n);
for (auto &[a, b] : s) a = read();
for (auto &[a, b] : s) b = read(), Modify(1, b, 1);
sort(s.begin(), s.end(), [&](pii x, pii y) { return x.first > y.first; });
for (const auto &[a, b] : s) {
if (st[1].cnt >= k) res = min(res, a * Query(1, k));
Modify(1, b, -1);
}
printf("%lld\n", res);
}
signed main() {
Build(1, 1, N - 1);
for (int T = read(); T--; Main());
return 0;
}