Time limit
2s
Memory limit
128 MB
You are given two sets S and T of non-negative integers. We want to choose pairs (s, t) with s from S and t from T.
The chosen pairs must satisfy both conditions below.
S may be paired with any element of T, and any element of T may be paired with any element of S.S must appear in at least one chosen pair, and every element of T must also appear in at least one chosen pair.The cost of a pair (a, b) is |a - b|. The total cost is the sum of the costs of all chosen pairs.
Given S and T, compute the minimum possible total cost.
The first line contains two integers N and M, the number of elements in S and T.
The second line contains the N elements of S in increasing order. The third line contains the M elements of T in increasing order.
Each set has at most 500,000 elements. Every element is an integer between 0 and 1,000,000,000, inclusive.
Print the minimum possible total cost.