Under construction

Still being built. Things may change

Kuru Kuru Sushi

Time limit5sMemory limit512 MB

Summary
Orient each conveyor on a cycle so all q food transfers are possible, then print the minimum total shortest-path cost, or -1.
Level

Medium7 of 10

Topics
Graph, Segment tree, Shortest path
Solved
No attempts yet

Problem

Zephir is the owner of the kuru kuru sushi restaurants. In these restaurants, sushi is placed on a rotating circular belt conveyor and delivered to customers.

Zephir owns nn restaurants numbered from 0 to n−1n-1. The restaurants are placed on the circumference of a circle, and a large belt conveyor runs underground between each pair of adjacent restaurants. One day, some restaurants ran short of sushi ingredients, so he decided to transport ingredient foods between restaurants using these conveyors.

The ii-th (0≤i≤n−20 \leq i \leq n-2) belt conveyor connects restaurants ii and i+1i+1. The (n−1)(n-1)-th belt conveyor connects restaurants n−1n-1 and 00. The length of the ii-th conveyor is wiw_i. He wants to transport qq foods, where the ii-th food is transported from restaurant sis_i to restaurant tit_i.

Each conveyor can transport food in only one direction, and Zephir can choose that direction. The directions cannot change while all qq foods are being transported. The transportation cost of the ii-th food is the sum of the lengths of the conveyors on the shortest path from sis_i to tit_i. Zephir must set the direction of every conveyor so that all foods can be transported. Calculate the minimum total cost, which is the sum of the transportation costs of all qq foods.

Input

The first line contains two integers nn and qq. Here nn (3≤n≤1053 \leq n \leq 10^5) is the number of restaurants, and qq (1≤q≤1051 \leq q \leq 10^5) is the number of foods. The next line contains nn integers wiw_i (1≤wi≤1051 \leq w_i \leq 10^5). Each of the following qq lines contains two integers sis_i and tit_i (0≤si,ti≤n−10 \leq s_i, t_i \leq n-1). It is guaranteed that si≠tis_i \neq t_i for every ii, and that si≠sjs_i \neq s_j or ti≠tjt_i \neq t_j for every pair i≠ji \neq j.

$n$ $q$

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

$s_0$ $t_0$

...

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

Output

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

Examples2

  1. Example 1

    Input
    4 4
    1 1 1 1
    0 1
    0 3
    2 3
    3 0
    
    Expected output
    6
    
  2. Example 2

    Input
    5 3
    4 5 6 7 8
    0 1
    0 3
    4 2
    
    Expected output
    32