Permutations of Two Arrays
Limits: 2 sec., 512 MiB
You are given two sequences: \(a\) of length \(n\) and \(b\) of length \(m\).
Let \(k = \min(n, m)\).
You want to choose a permutation \(c\) of the sequence \(a\) and a permutation \(d\) of the sequence \(b\) to maximize the following score: \[\sum_{i=1}^k |c_i - d_i|.\]
Input
The first line contains two integers \(n\) and \(m\) – the lengths of the sequences \(a\) and \(b\), respectively.
The second line contains \(n\) integers \(a_i\).
The third line contains \(m\) integers \(b_i\).
Output
In the single line print an integer – the maximum score.
Constraints
\(1 \le n, m \le 10^5\),
\(0 \le a_i, b_i \le 10^9\).
Samples
Input (stdin) | Output (stdout) |
---|---|
4 4 4 7 7 4 44 47 4 7 | 86 |
Input (stdin) | Output (stdout) |
---|---|
4 7 1 2 3 4 10 20 30 40 50 60 70 | 210 |
Notes
In the first example, we have \(a = (4, 7, 7, 4), b = (44, 47, 4, 7)\). When we choose \(c = (7, 4, 4, 7), d = (7, 47, 44, 4)\), the score will be \(|c_1 - d_1| + |c_2 - d_2| + |c_3 - d_3| + |c_4 - d_4| = |7 - 7| + |4 - 47| + |4 - 44| + |7 - 4| = 0 + 43 + 40 + 3 = 86\). It is the maximum score.
Submit a solution
Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
---|
Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
---|