#P15877. [ICPC 2026 NAC] Cable Pruning

[ICPC 2026 NAC] Cable Pruning

Description

You are helping to remodel a data center to make room for more GPUs. Over the years, the data center has become cluttered with superfluous network cable, and you have been asked to clean up the mess and reclaim as much unused cable as possible.

:::align{center}

An illustration of Sample Input 1. Servers in the same coupled pair are the same color. Cables to remove are indicated with a dashed line. :::

The data center has NN servers and NN network cables that link one server to another. Each cable has a length in feet. Traffic flows through network cables in both directions and the data center is initially connected: for every pair of servers (u,v)(u,v) there exists a path of network cables from uu to vv (possibly passing through intermediate servers). You've audited the data center network traffic to discover that only KK coupled pairs\emph{coupled pairs} of servers need to communicate with each other. (Note that some servers might not be part of any coupled pair, or might be part of two or more coupled pairs.)

You now need to remove as much total length of cable as possible from the data center while keeping all coupled pairs connected to each other: for each such pair of servers (a,b)(a,b) there must exist a path from aa to bb passing through original network cables that you've kept in place.

Find the minimum total length of cable that must be kept in place to satisfy this constraint.

Input Format

The first line of input contains two space-separated integers NN (3N105)(3 \le N \le 10^5) and KK (1K105)(1 \le K \le 10^5), the number of servers in the data center and number of coupled pairs of servers that you've discovered.

The next NN lines describe the network cables originally in the data center. Each line contains three space-separated integers uiu_i, viv_i (1ui,viN,uivi)(1 \le u_i, v_i \le N, u_i \ne v_i), and wiw_i (1wi109)(1 \le w_i \le 10^9), specifying that a cable connects server uiu_i to server viv_i and has length wiw_i feet. There is at most one network cable connecting a pair of servers and the graph of servers and cables is connected.

Each of the next KK lines contains two space-separated integers aja_j and bjb_j (1aj,bjN1 \le a_j, b_j \le N, ajbja_j \ne b_j) and describe a coupled pair of servers. Each coupled pair must remain connected by a path after you are done removing cable. All coupled pairs are distinct; (a,b)(a,b) and (b,a)(b,a) are considered the same and won't both be listed as coupled pairs.

Output Format

Print a single integer: the minimum total length of network cable (in feet) that must remain in place in order for all KK coupled server pairs to stay connected to each other.

8 3
5 3 5
1 7 20
3 8 8
7 5 15
5 2 12
1 6 9
5 1 10
7 4 7
3 4
8 2
1 7
57
5 1
1 3 3
4 2 4
3 4 2
5 2 2
4 1 6
2 1
9