#P12007. Minimizing Dissonance in the Wind Ensemble

    ID: 14110 Type: Default 1000ms 256MiB

Minimizing Dissonance in the Wind Ensemble

Minimizing Dissonance in the Wind Ensemble

Consider a wind ensemble with nn students numbered from 11 to nn. Before Takishō arrives, there are already mm established cooperation relations. Each relation is given by three integers uu, vv, and ww, meaning that when either student uu or student vv plays, the other can follow and finish his part exactly ww time units later. If two students do not have a direct relation, they may still cooperate indirectly through a sequence of relations; the total error time between them is defined as the sum of the waiting times along the chain.

Now the ensemble is in utter disarray! To solve this, Takishō has nm1n - m - 1 special training schemes available. The ii-th scheme lets any two members establish a new cooperation relation and guarantees that after training the two can cooperate in aia_i time units. Once all training schemes are applied, the overall coordination error (or dissonance) is defined as the sum over all pairs 1x<yn1 \le x < y \le n of the minimum error time between xx and yy. (If some pair of students cannot cooperate in the final network, the training plan is considered invalid.) To qualify for the national league, Takishō wants to choose the training schemes so that the dissonance is minimized. Compute the optimal dissonance modulo 109+710^9+7.

More formally, suppose the mm fixed relations form a forest (each relation is represented by an edge with weight ww) and let the training schemes, with values a1,a2,,anm1a_1,a_2,\dots,a_{n-m-1}, be used to add edges among the components. In the final spanning tree (with n1n-1 edges) the dissonance is [ \sum_{\text{edge } e}; (\text{edge weight})\times (\text{# of pairs separated by } e), \quad\text{where the number of separated pairs by edge } e \text{ is } x\times (n-x) \text{ if one side of } e \text{ has } x \text{ nodes}. ]

The fixed edges are predetermined. You are allowed to choose any nm1n-m-1 training edges among different components; furthermore, you can assign the training schemes arbitrarily to these edges. It can be shown that an optimal strategy always exists.

Your task is to output the minimum possible dissonance modulo 109+710^9+7.

inputFormat

The first line contains two integers nn and mm (1n1051\le n\le 10^5, 0mn10\le m\le n-1).

Then follow mm lines. Each of these lines contains three integers uu, vv, and ww (1u,vn1\le u,v\le n, 1w1091\le w\le 10^9), representing an existing cooperation relation between students uu and vv with error time ww. It is guaranteed that these mm relations form a forest (i.e. there are no cycles).

The next line contains nm1n-m-1 integers a1,a2,,anm1a_1,a_2,\dots,a_{n-m-1} (1ai1091\le a_i\le 10^9), representing the time required for cooperation if a training scheme is used.

outputFormat

Output a single integer: the minimum possible dissonance modulo 109+710^9+7.

sample

4 2
1 2 1
3 4 2
3
15