#ABC270F. [ABC270F] Transportation

[ABC270F] Transportation

Score : 500500 points

Problem Statement

The Republic of AtCoder has NN islands. Initially, none of the islands has an airport or harbor, and there is no road between any two of them. Takahashi, the king, will provide some means of transportation between these islands. Specifically, he can do the following operations any number of times in any order.

  • Choose an integer ii such that 1iN1\leq i\leq N and pay a cost of XiX_i to build an airport on island ii.
  • Choose an integer ii such that 1iN1\leq i\leq N and pay a cost of YiY_i to build a harbor on island ii.
  • Choose an integer ii such that 1iM1\leq i\leq M and pay a cost of ZiZ_i to build a road that bidirectionally connects island AiA_i and island BiB_i.

Takahashi's objective is to make it possible, for every pair of different islands UU and VV, to reach island VV from island UU when one can perform the following actions any number of times in any order.

  • When islands SS and TT both have airports, go from island SS to island TT.
  • When islands SS and TT both have harbors, go from island SS to island TT.
  • When islands SS and TT are connected by a road, go from island SS to island TT.

Find the minimum total cost Takahashi needs to pay to achieve his objective.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 1M2×1051 \leq M \leq 2\times 10^5
  • 1Xi1091\leq X_i\leq 10^9
  • 1Yi1091\leq Y_i\leq 10^9
  • $1\leq A_i
  • 1Zi1091\leq Z_i\leq 10^9
  • (Ai,Bi)(Aj,Bj)(A_i,B_i)\neq (A_j,B_j), if iji\neq j.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM

X1X_1 X2X_2 \ldots XNX_N

Y1Y_1 Y2Y_2 \ldots YNY_N

A1A_1 B1B_1 Z1Z_1

A2A_2 B2B_2 Z2Z_2

\vdots

AMA_M BMB_M ZMZ_M

Output

Print the minimum total cost Takahashi needs to pay to achieve his objective.

4 2
1 20 4 7
20 2 20 3
1 3 5
1 4 6
16

Takahashi will build the following infrastructure.

  • Pay a cost of X1=1X_1=1 to build an airport on island 11.
  • Pay a cost of X3=4X_3=4 to build an airport on island 33.
  • Pay a cost of Y2=2Y_2=2 to build a harbor on island 22.
  • Pay a cost of Y4=3Y_4=3 to build a harbor on island 44.
  • Pay a cost of Z2=6Z_2=6 to build a road connecting island 11 and island 44.

Then, the objective will be achieved at a cost of 1616. There is no way to achieve the objective for a cost of 1515 or less, so 1616 should be printed.

3 1
1 1 1
10 10 10
1 2 100
3

It is not mandatory to build all three types of facilities at least once.

7 8
35 29 36 88 58 15 25
99 7 49 61 67 4 57
2 3 3
2 5 36
2 6 89
1 6 24
5 7 55
1 3 71
3 4 94
5 6 21
160