Under construction

Still being built. Things may change

Omnes Viae Yokohamam Ducunt?

Interview

Time limit3sMemory limit2048 MB

Summary
Find a spanning tree rooted at city 1 that minimizes the sum over edges of vulnerability times the significance total of the cities cut off from city 1 by that edge.
Level

Medium6 of 10

Topics
Shortest path, Graph, Heap
Solved
No attempts yet

Statement

Time limit: 3000 ms. Memory limit: 2048 MB.

"Omnes viae Romam ducunt" is an old Latin proverb meaning "all roads lead to Rome." It is still desirable to have access to the capital from all the regions of a country.

The Kingdom of Kanagawa has a number of cities, including the capital city, Yokohama. The Ministry of Transport of the Kingdom is now planning to construct a highway network that connects all those cities.

There are several candidate highway segments, and each one directly connects two cities. A highway network is a set of segments chosen from the candidates. The network must satisfy the following conditions.

  • All the cities must be connected via highway segments in the network, directly or indirectly.
  • The number of segments must be minimal to save the budget. In other words, the network must not be redundant, so the path between any two cities is unique.

Access to Yokohama matters most. Because the network has no redundancy, if a natural disaster makes one segment unavailable, some cities become inaccessible from Yokohama.

Each city has a significance value based on its population and economic scale. When a natural disaster hits one segment of a given network, the damage is the sum of the significance values of the cities that become inaccessible from Yokohama. The vulnerability of each candidate segment is also assessed. The risk severity of a segment is the product of its damage and its vulnerability. The total risk severity of a network is the sum of the risk severities of all its segments. Find the minimum total risk severity.

Input

The first line contains two integers nn and mm (2≤n≤1052 \le n \le 10^5, 1≤m≤3×1051 \le m \le 3 \times 10^5), the numbers of cities and candidate segments. The cities are numbered from 11 to nn, and Yokohama is city 11.

The second line contains nn integers p1,…,pnp_1, \dots, p_n, where each pip_i (1≤pi≤10001 \le p_i \le 1000) is the significance value of city ii.

The next mm lines describe the candidate segments. Line jj contains three integers uju_j, vjv_j, and qjq_j, meaning the candidate segment between cities uju_j and vjv_j has vulnerability qjq_j (1≤uj<vj≤n1 \le u_j < v_j \le n, 1≤qj≤1061 \le q_j \le 10^6). Each pair (uj,vj)(u_j, v_j) appears at most once.

It is guaranteed that a highway network connecting all the cities can be built from some of these segments.

Output

Print the minimum possible total risk severity on one line.

Examples2

  1. Example 1

    Input
    3 3
    1 2 3
    1 2 2
    2 3 3
    1 3 4
    
    Expected output
    16
    
  2. Example 2

    Input
    5 7
    2 6 7 7 10
    1 5 8
    1 4 6
    3 4 9
    2 3 6
    2 4 7
    1 3 4
    4 5 4
    
    Expected output
    210