This page is still under construction.

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

Escape Room

Time limit2sMemory limit1024 MB

Summary
Given N rooms with per-room exit costs and M candidate warps with costs, choose warps and exits so every room reaches the outside, minimizing the total installation time.
Level

Medium6 of 10

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

Problem

Wonbin went to an escape room cafe with his friends. The cafe has NN rooms numbered 11 through NN, and one friend is inside each room. Every room is completely isolated from the outside.

Wonbin felt bad for the friends who cannot get out, so he wants to install warps and emergency exits so that all of them can escape to the outside. He can install at most MM warps. Installing the ii-th warp takes cic_i time, and once installed it lets a person move between room aia_i and room bib_i. Each room can also have an emergency exit that connects directly to the outside, and installing an emergency exit in room ii takes tit_i time.

Unfortunately, Wonbin is not the brightest, so he cannot work on multiple installations of warps or emergency exits at the same time. In other words, he can start the next task only after the current one finishes.

Help Wonbin find the minimum time needed to install warps and emergency exits so that all of his friends can escape to the outside.

Input

The first line gives the number of rooms NN and the number of warps that can be installed MM. (2≤N≤200 0002 \le N \le 200\,000, 1≤M≤100 0001 \le M \le 100\,000)

The next MM lines give three integers aia_i, bib_i, cic_i separated by spaces, describing a warp: installing a warp between room aia_i and room bib_i takes cic_i time. Multiple warps may connect the same pair of rooms. (1≤ai,bi≤N1 \le a_i, b_i \le N, 1≤ci≤1041 \le c_i \le 10^4, ai≠bia_i \ne b_i)

The last line gives NN integers t1t_1, ..., tnt_n, where tit_i is the time needed to install an emergency exit in room ii. (1≤ti≤1041 \le t_i \le 10^4)

Output

Print the minimum time needed to install warps and emergency exits so that all of the friends can escape to the outside.

Examples2

  1. Example 1

    Input
    3 3
    1 2 2
    2 3 2
    3 1 2
    3 3 3
    
    Expected output
    7
    
  2. Example 2

    Input
    3 1
    1 2 2
    3 3 3
    
    Expected output
    8