Kuru Kuru Sushi

가중치가 있는 원형 그래프의 각 간선 방향을 정해 q개의 출발지-도착지 쌍에 대한 최단 경로 길이 합을 최소화하고, 불가능하면 -1을 출력한다.

어려움8그래프그리디누적 합구현아직 제출이 없습니다시간 제한5초메모리 제한512 MB

문제

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 (0in20 \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.

입력

Each input is formatted as follows:

$n$ $q$

$w_0$ $w_1$ ... $w_{n-1}$

$s_0$ $t_0$

...

$s_{q-1}$ $t_{q-1}$

The first line contains two integers nn and qq. nn (3n1053 \leq n \leq 10^5) denotes the number of the restaurants, and qq (1q1051 \leq q \leq 10^5) denotes the number of the foods to transport. The next line contains nn integers. w_iw\_i (1w_i1051 \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 (0s_in10 \leq s\_i \leq n-1) and t_it\_i (0t_in10 \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_it_is\_i \neq t\_i for any 0iq10 \leq i \leq q-1, and s_is_js\_i \neq s\_j or t_it_jt\_i \neq t\_j if iji \neq j for any 0i,jq10 \leq i , j \leq q-1.

출력

Print the minimum total cost of the foods transportation. If it is impossible to transport all foods to each destination, print -1.