atcoder#NIKKEI2019QUALE. Weights on Vertices and Edges

Weights on Vertices and Edges

Score : 800800 points

Problem Statement

There is a connected undirected graph with NN vertices and MM edges. The vertices are numbered 11 to NN, and the edges are numbered 11 to MM. Also, each of these vertices and edges has a specified weight. Vertex ii has a weight of XiX_i; Edge ii has a weight of YiY_i and connects Vertex AiA_i and BiB_i.

We would like to remove zero or more edges so that the following condition is satisfied:

  • For each edge that is not removed, the sum of the weights of the vertices in the connected component containing that edge, is greater than or equal to the weight of that edge.

Find the minimum number of edges that need to be removed.

Constraints

  • 1N1051 \leq N \leq 10^5
  • N1M105N-1 \leq M \leq 10^5
  • 1Xi1091 \leq X_i \leq 10^9
  • 1Ai<BiN1 \leq A_i < B_i \leq N
  • 1Yi1091 \leq Y_i \leq 10^9
  • (Ai,Bi)(Aj,Bj)(A_i,B_i) \neq (A_j,B_j) (iji \neq j)
  • The given graph is connected.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

X1X_1 X2X_2 ...... XNX_N

A1A_1 B1B_1 Y1Y_1

A2A_2 B2B_2 Y2Y_2

::

AMA_M BMB_M YMY_M

Output

Find the minimum number of edges that need to be removed.

4 4
2 3 5 7
1 2 7
1 3 9
2 3 12
3 4 18
2

Assume that we removed Edge 33 and 44. In this case, the connected component containing Edge 11 contains Vertex 1,21, 2 and 33, and the sum of the weights of these vertices is 2+3+5=102+3+5=10. The weight of Edge 11 is 77, so the condition is satisfied for Edge 11. Similarly, it can be seen that the condition is also satisfied for Edge 22. Thus, a graph satisfying the condition can be obtained by removing two edges.

The condition cannot be satisfied by removing one or less edges, so the answer is 22.

6 10
4 4 1 1 1 7
3 5 19
2 5 20
4 5 8
1 6 16
2 3 9
3 6 16
3 4 1
2 6 20
2 4 19
1 2 9
4
10 9
81 16 73 7 2 61 86 38 90 28
6 8 725
3 10 12
1 4 558
4 9 615
5 6 942
8 9 918
2 7 720
4 7 292
7 10 414
8