CF BUDDY
← Problems·

1949B · Charming Meals

1500 · binary search, brute force, greedy

Problem: The Czech cuisine features nn appetizers and nn main dishes. The ii-th appetizer has spiciness aia_i, and the ii-th main dish has spiciness bib_i.

A typical Czech meal consists of exactly one appetizer and one main dish. You want to pair up the nn appetizers and nn main dishes into nn meals with each appetizer and each main dish being included in exactly one meal.

Your meals shall surprise the diners, so you want the spiciness levels of the two parts of the same meal to be as different as possible. The charm of a meal is the difference (in absolute value) between the spiciness of the appetizer and the spiciness of the main dish. So, a meal consisting of an appetizer with spiciness xx and a main dish with spiciness yy has charm equal to xy|x-y|.

You want to maximize the minimum charm of the resulting nn meals. What is the largest possible value of the minimum charm that you can achieve?

Input Format: Each test contains multiple test cases. The first line contains an integer tt (1t10001\le t\le 1\,000) — the number of test cases. The descriptions of the tt test cases follow.

The first line of each test case contains a single integer nn (1n50001 \leq n \leq 5\,000) —the number of appetizers and main dishes.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai1090 \leq a_i \leq 10^{9}) — the spicinesses of the nn appetizers.

The third line of each test case contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n (0bi1090 \leq b_i \leq 10^{9}) — the spicinesses of the nn main dishes.

It is guaranteed that the sum of n2n^2 over all test cases does not exceed 2510625\cdot 10^6.

Output Format: For each test case, print the largest possible value of the minimum charm you can achieve.

Note: In the first test case, no matter how you pair up the appetizers with the main dishes, each meal will have an appetizer with spiciness 00 and a main dish with spiciness 10000000001000000000, so the charm of each meal will be 10000000001000000000.

In the second test case, one optimal way to pair up appetizers and main dishes is: (1,5)(1, 5), (2,4)(2, 4), (3,1)(3, 1), (4,2)(4, 2), (5,3)(5, 3). The corresponding meals have charms: 44, 22, 22, 22, 22. The resulting minimum charm is 22.

In the third test case, one way to maximize the minimum charm is to pair up the three appetizers with spiciness 00 with the three main dishes with spiciness 100100, and the three appetizers with spiciness 100100 with the three main dishes with spiciness 00. Doing so, the charm of each meal will be exactly 100100.

Sample Cases

Case 1

Input

4
3
0 0 0
1000000000 1000000000 1000000000
5
1 2 3 4 5
1 2 3 4 5
6
0 0 0 100 100 100
100 100 100 0 0 0
7
14 25 62 74 86 95 12
51 62 71 72 92 20 84

Output

1000000000
2
100
30

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback