Mural
Output: Standard Output (stdout)
Memory limit: 128 megabytes
Time limit: 1.5 seconds
You have been commissioned to paint a mural on a very long wall. The mural will consist of N segments (1 \le N \le 100,000), the i-th of which needs to be spray painted with the colour A_i. You have brought exactly N cans of spray paint, each of which can be used to paint exactly one segment, but unfortunately in your excitement you grabbed N random cans of spray paint and didn't check their colours. The i-th can of spray paint has colour B_i.
Colours are of course RGB
values, and so are represented by integers between 0 and 16{,}777{,}215 (inclusive). Luckily for you, colours that are similar enough to each other will not be hugely noticeable. Specifically, the inaccuracy of using a colour c instead of a colour d will be |c - d|, that is the absolute difference between the two numbers.
Given the colours of the segments and the cans of spray paint at your disposal, you wish to calculate the minimum possible sum of all inaccuracies after using the cans of spray paint you have available to paint the mural.
Input
The first line contains N, the number of segments in the mural and the number of cans of spray paint.
The second line contains N space separated integers, A_1, \dots, A_N, that is the colours of the segments of the mural.
The third line contains N space separated integers, B_1, \dots, B_N, that is the colours of the cans of spray paint you have available.
Output
Output a single integer, the minimum total inaccuracy that you can achieve.
Notes
If you're using a type-sensitive language such as C, C++, C# or Java, you should use 64-bit integer types to prevent integer overflow. 64-bit integer types include long long for C or C++, and long for C# or Java.
Subtasks
- Subtask 1 (12%): N = 2
- Subtask 2 (38%): N \le 1,000
- Subtask 3 (50%): No further constraints.
Sample Explanations
Sample 1
You can use the first can to paint the first segment and the second can to paint the second segment for a total inaccuracy of |3 - 4| + |7 - 6| = 2.
Sample 2
You can use cans 4, 1, 2, 3 to paint segments 1, 2, 3, 4 respectively. This gives a total inaccuracy of |4 - 4| = |2 - 2| + |1 - 1| + |2 - 3| = 1.
-
Sample Input 1
2 3 7 4 6
Sample Output 1
2
-
Sample Input 2
4 4 2 1 2 2 1 3 4
Sample Output 2
1