#codefestival2016qualBc. [codefestival_2016_qualB_c]Gr-idian MST
[codefestival_2016_qualB_c]Gr-idian MST
Problem Statement
On an plane, in an area satisfying , there is one house at each and every point where both and are integers.
There are unpaved roads between every pair of points for which either the coordinates are equal and the difference between the coordinates is , or the coordinates are equal and the difference between the coordinates is .
The cost of paving a road between houses on coordinates and is for any value of , while the cost of paving a road between houses on coordinates and is for any value of .
Mr. Takahashi wants to pave some of these roads and be able to travel between any two houses on paved roads only. Find the solution with the minimum total cost.
Constraints
- is an integer.
- is an integer.
Input
Inputs are provided from Standard Input in the following form.
: :
Output
Output an integer representing the minimum total cost.
Sample Input 1
2 2
3
5
2
7
Sample Output 1
29
It is enough to pave the following eight roads.
- Road connecting houses at and
- Road connecting houses at and
- Road connecting houses at and
- Road connecting houses at and
- Road connecting houses at and
- Road connecting houses at and
- Road connecting houses at and
- Road connecting houses at and
Sample Input 2
4 3
2
4
8
1
2
9
3
Sample Output 2
60