#abc290h. [abc290_h]Bow Meow Optimization
[abc290_h]Bow Meow Optimization
Problem Statement
There are dogs numbered through and cats numbered through . You will arrange the animals in a line from left to right. An arrangement causes each animal's frustration as follows:
- The frustration of dog is , where and are the numbers of cats to the left and right of that dog, respectively.
- The frustration of cat is , where and are the numbers of dogs to the left and right of that cat, respectively.
Find the minimum possible sum of the frustrations.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer as an integer.
Sample Input 1
2 2
1 3
2 4
Sample Output 1
6
Consider the following arrangement: dog , cat , dog , cat , from left to right. Then,
- dog 's frustration is ;
- dog 's frustration is ;
- cat 's frustration is ; and
- cat 's frustration is ,
so the sum of the frustrations is . Rearranging the animals does not make the sum less than , so the answer is .
Sample Input 2
1 2
100
100 290
Sample Output 2
390
Sample Input 3
5 7
522 575 426 445 772
81 447 629 497 202 775 325
Sample Output 3
13354