#icpc2015summerday4c. [icpc2015summer_day4_c]Kuru Kuru Sushi
[icpc2015summer_day4_c]Kuru Kuru Sushi
MathJax.Hub.Config({ tex2jax: { inlineMath: [[""], ["\\(","\\)"]], processEscapes: true }}); blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 12px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }
Problem Statement
Zephir is an owner of the “kuru kuru sushi” restaurants. In these restaurants, sushis are placed on a rotating circular belt conveyor and delivered to customers.
Zephir owns restaurants numbered from to . Due to his passion for rotation, these restaurants are placed on the circumference of a circle, and have huge belt conveyors under the ground between adjacent restaurants. One day, the ingredients of sushis were short in some restaurants. Therefore, he decided to transport some ingredient foods between restaurants by using these belt conveyors.
The -th () belt conveyor connects the restaurants and . The -th belt conveyor connects the restaurants and . The length of the -th belt conveyor is . He wants to transport foods from some restaurants to other restaurants. The -th food should be transported from restaurant to .
Zephir wants to minimize the total cost of transportation. The transportation cost of each food can be changed by the settings of the direction of the belt conveyors. Each belt conveyor can transport foods in only a single direction, which can be set by Zephir. Moreover, the settings cannot be changed while transporting all foods. The transportation cost of the -th food is the sum of the length of the belt conveyors in the shortest path from restaurant to . He should set the direction of belt conveyors to transport all foods. Write a program to calculate the minimum value of the total cost, which is the sum of the transportation costs of all the foods.
Input
Each input is formatted as follows:
...
...
The first line contains two integers and . () denotes the number of the restaurants, and () denotes the number of the foods to transport. The next line contains integers. () denotes the length of the -th belt conveyor. Each of the following lines contains two integers () and (). Each denotes the -th food should be transported from restaurant to . You may assume for any , and or if for any .
Output
Print the minimum total cost of the foods transportation. If it is impossible to transport all foods to each destination, print -1.
Sample Input 1
4 4
1 1 1 1
0 1
0 3
2 3
3 0```
### Output for the Sample Input 1
```plain
6```
* * *
### Sample Input 2
```plain
5 3
4 5 6 7 8
0 1
0 3
4 2```
### Output for the Sample Input 2
```plain
32```
* * *