Problem Description
Xuejiejie is a beautiful and charming sharpshooter.
She often carries n guns, and every gun has an attack power a[i] .
One day, Xuejiejie goes outside and comes across m monsters, and every monster has a defensive power b[j] .
Xuejiejie can use the gun i to kill the monster j , which satisfies b[j]≤a[i] , and then she will get a[i]−b[j] bonus .
Remember that every gun can be used to kill at most one monster, and obviously every monster can be killed at most once.
Xuejiejie wants to gain most of the bonus. It's no need for her to kill all monsters.
She often carries n guns, and every gun has an attack power a[i] .
One day, Xuejiejie goes outside and comes across m monsters, and every monster has a defensive power b[j] .
Xuejiejie can use the gun i to kill the monster j , which satisfies b[j]≤a[i] , and then she will get a[i]−b[j] bonus .
Remember that every gun can be used to kill at most one monster, and obviously every monster can be killed at most once.
Xuejiejie wants to gain most of the bonus. It's no need for her to kill all monsters.
Input
In the first line there is an integer
T
, indicates the number of test cases.
In each case:
The first line contains two integers n , m .
The second line contains n integers, which means every gun's attack power.
The third line contains m integers, which mean every monster's defensive power.
1≤n,m≤100000 , −109≤a[i],b[j]≤109 。
In each case:
The first line contains two integers n , m .
The second line contains n integers, which means every gun's attack power.
The third line contains m integers, which mean every monster's defensive power.
1≤n,m≤100000 , −109≤a[i],b[j]≤109 。
Output
For each test case, output one integer which means the maximum of the bonus Xuejiejie could gain.
Sample Input
1 2 2 2 3 2 2
Sample Output
1
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<set> #include<map> #include<queue> using namespace std; const int maxn = 100005; int n, m, T; __int64 a[maxn], b[maxn], p, ans, sum; queue<__int64> u, w; int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); for (int i = 0; i < m; i++) scanf("%I64d", &b[i]); sort(a, a + n); sort(b, b + m); for (int i = 0, j = 0; i < n; i++) { if (j == m) { if (!u.empty()) u.pop(), u.push(a[i]); } else if (a[i] >= b[j]) { u.push(a[i]); w.push(b[j++]); } else if (!u.empty()) u.pop(), u.push(a[i]); } ans = 0; sum = 0; for (int i = 0; i < u.size(); i++) sum -= b[i]; for (int i = n - 1; i + u.size() >= n; i--) sum += a[i]; for (int i = u.size(); i; i--) { ans = max(ans, sum); sum += b[i - 1]; sum -= a[n - i]; } while (!u.empty()) u.pop(); while (!w.empty()) w.pop(); printf("%I64d\n", ans); } return 0; }