P13967 [VKOSHP 2024] Two Arrays

Description

Let the maximum in the array $d$ be denoted as $\max(d)$ and the minimum as $\min(d)$. Two arrays $a$ and $b$ of length $n$ are given. In one operation, you can choose an index $1 \leq i \leq n$ and simultaneously increase the elements $a_i$ and $b_i$ by one: $a_i = a_i + 1$, $b_i = b_i + 1$. It is necessary to use these operations to achieve the simultaneous fulfillment of two conditions: - $\max(a) - \min(a) \leq x$, - $\max(b) - \min(b) \leq y$. Determine the minimum number of operations required to achieve the simultaneous fulfillment of the specified conditions, or find out that it is impossible.

Input Format

Each test consists of several test cases. The first line contains one integer $t$ --- the number of test cases ($1 \leq t \leq 10^5$). The description of the test cases follows. The first line of each test case contains three integers: $n$, $x$, $y$ ($1 \leq n \leq 10^5$, $0 \leq x, y \leq 10^9$). The second line of each test case contains $n$ integers $a_1, a_2, \dots a_n$ --- the elements of array $a$ ($-10^9 \leq a_i \leq 10^9$). The third line of each test case contains $n$ integers $b_1, b_2, \dots b_n$ --- the elements of array $b$ ($-10^9 \leq b_i \leq 10^9$). It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$.

Output Format

For each test case, output one integer --- the minimum possible number of operations required to satisfy both conditions. If it is impossible to satisfy both conditions simultaneously, output $-1$.