This page is still under construction.

Parts of this page are still being built. What you see may change.

Railway Connection

Time limit1sMemory limit1024 MB

Summary
Given existing rail links and city flows, find the minimum cost to connect all cities, where an edge costs the product of its endpoint flows.
Level

Medium5 of 10

Topics
Minimum spanning tree, Union-find, Greedy, Sorting
Solved
No attempts yet

Problem

The railway infrastructure of Bitlandia is being reorganized. This task has been assigned to Martynas, the head of the Bitlandia Train Company.

First, Martynas estimated the inbound passenger flow SiS_i for each city ii. Martynas designs railway lines between cities so that:

  • From any city in Bitlandia it is possible to travel by rail to every other city (not necessarily directly).
  • Building one railway line between cities ii and jj costs Si×SjS_i \times S_j biteuros — a larger flow requires more investment (a bigger station, a larger parking lot, and so on).

Some railways in Bitlandia have already been built, but with a reduced budget Martynas wants to build the missing lines as cheaply as possible.

Determine the minimum cost of building the remaining railway lines so that all of Martynas's requirements are satisfied.

Input

The first line contains two space-separated integers NN and MM — the number of cities in Bitlandia and the number of railway lines already built.

The second line contains NN space-separated integers SiS_i.

Each of the next MM lines contains two integers viv_i and uiu_i, meaning that a direct railway line already exists between cities viv_i and uiu_i.

Output

Output the minimum cost, in biteuros, of building all the remaining railway lines.

Constraints

  • 1≤N≤1000001 \le N \le 100000
  • 0≤M≤1000000 \le M \le 100000
  • 1≤Si≤1001 \le S_i \le 100
  • 1≤vi,ui≤N1 \le v_i, u_i \le N
  • All pairs (vi,ui)(v_i, u_i) are distinct and vi≠uiv_i \ne u_i.

Examples2

  1. Example 1

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

    Input
    3 3
    100 100 100
    1 2
    2 3
    3 1
    
    Expected output
    0