atcoder#ABC232F. [ABC232F] Simple Operations on Sequence
[ABC232F] Simple Operations on Sequence
Score : points
Problem Statement
Given are two sequences of integers each: and .
On the sequence , you can do the two operations below any number of times (possibly zero) in any order.
- Choose an integer satisfying , and increase or decrease by , for the cost of yen (Japanese currency).
- Choose an integer satisfying , and swap the values of and , for the cost of yen.
Print the minimum total cost needed to make the sequence equal the sequence by repeating the above.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum total cost needed to make equal .
4 3 5
4 2 5 2
6 4 2 1
16
Initialy, we have . The following sequence of operations makes equal .
- Pay yen to increase the value of by , making .
- Pay yen to swap the values of and , making .
- Pay yen to swap the values of and , making .
- Pay yen to decrease the value of by , making .
The total cost of these operations is yen, which is the minimum possible.
5 12345 6789
1 2 3 4 5
1 2 3 4 5
0
and are equal from the beginning, so no operation is needed.
18 20719114 5117250357733867
10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712
14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077
13104119429316474
Note that values in input or output may not fit into -bit integer types.