第六回日本最強プログラマー学生選手権-予選-(AtCoder Regular Contest 201)
D
Match, Mod, Minimize
AtCoder
⌀ 2389
原题
← CE →
翻译中…

Problem Statement

翻译中…

You are given sequences of non-negative integers $A=(A_1,A_2,\dots ,A_N),B=(B_1,B_2,\dots ,B_N)$ of length $N$ and a positive integer $M$.

翻译中…

When the elements of $A$ can be rearranged freely, find the minimum possible value of $\max_{1\le i\le N} ((A_i+B_i) \bmod M)$.

翻译中…

$T$ test cases are given, so find the answer for each.

翻译中…

Constraints

翻译中…
  • $1 \le T \le 10^5$
  • $1 \le N \le 3 \times 10^5$
  • $1 \le M \le 10^9$
  • $0 \le A_i,B_i \lt M$
  • The sum of $N$ over all test cases is at most $3 \times 10^5$.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

翻译中…

Each test case is given in the following format:

翻译中…

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

翻译中…

Output

翻译中…

Output $T$ lines.

翻译中…

The $j$-th line should contain the minimum possible value of $\max_{1\le i\le N} ((A_i+B_i) \bmod M)$ for the $j$-th test case.

翻译中…

样例

Input #1
3
3 6
3 1 4
2 0 1
1 1000000000
999999999
999999999
10 201
144 150 176 154 110 187 38 136 111 46
96 109 73 63 85 1 156 7 13 171
Output #1
3
999999998
111
For the first test case, if we rearrange $A$ to $4,3,1$, then $(A_i+B_i) \bmod M$ becomes $0,3,2$ respectively, and the $\max$ of these is $3$.
题目信息

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。