#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 nn restaurants numbered from 00 to n1n-1. 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 ii-th (0leqileqn20 \\leq i \\leq n-2) belt conveyor connects the restaurants ii and i+1i+1. The (n1)(n-1)-th belt conveyor connects the restaurants n1n-1 and 00. The length of the ii-th belt conveyor is w_iw\_i. He wants to transport qq foods from some restaurants to other restaurants. The ii-th food should be transported from restaurant s_is\_i to t_it\_i.

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 qq foods. The transportation cost of the ii-th food is the sum of the length of the belt conveyors in the shortest path from restaurant s_is\_i to t_it\_i. 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 qq foods.


Input

Each input is formatted as follows:

nn qq
w_0w\_0 w_1w\_1 ... w_n1w\_{n-1}
s_0s\_0 t_0t\_0
...
s_q1s\_{q-1} t_q1t\_{q-1}

The first line contains two integers nn and qq. nn (3leqnleq1053 \\leq n \\leq 10^5) denotes the number of the restaurants, and qq (1leqqleq1051 \\leq q \\leq 10^5) denotes the number of the foods to transport. The next line contains nn integers. w_iw\_i (1leqw_ileq1051 \\leq w\_i \\leq 10^5) denotes the length of the ii-th belt conveyor. Each of the following qq lines contains two integers s_is\_i (0leqs_ileqn10 \\leq s\_i \\leq n-1) and t_it\_i (0leqt_ileqn10 \\leq t\_i \\leq n-1). Each denotes the ii-th food should be transported from restaurant s_is\_i to t_it\_i. You may assume s_ineqt_is\_i \\neq t\_i for any 0leqileqq10 \\leq i \\leq q-1, and s_ineqs_js\_i \\neq s\_j or t_ineqt_jt\_i \\neq t\_j if ineqji \\neq j for any 0leqi,jleqq10 \\leq i , j \\leq q-1.

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```

* * *