Problem 879
Minimum Absolute Sum Difference
Medium · 20 pts · Array, Binary Search, Sorting
Problem
Two arrays A and B of length N are given. The absolute sum difference is the sum of |A[i] - B[i]| over all i. You may replace at most one element of A with any other element of A (a copy of some A[j]). Minimise the absolute sum difference and print it modulo 1000000007. For each i, the best replacement is the element of A closest to B[i], found by binary search in a sorted copy of A; take the largest saving. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).
Input
The first line contains N. The second line contains A. The third line contains B.
Output
Print the minimum absolute sum difference modulo 1000000007.
Constraints
1 <= N <= 100000; 1 <= A[i], B[i] <= 10^5
Example input
3 1 7 5 2 3 5
Example output
3
Explanation
With A = 1 7 5 and B = 2 3 5, the difference is 1 + 4 + 0 = 5. Replacing 7 by 5 makes it 1 + 2 + 0 = 3, which is the best possible.