PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000221. Sửa chữa đường sá (Road Reparation)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sửa chữa đường sá (Road Reparation)

Road Reparation

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn cities and mm undirected roads, but none of the roads can currently be used. Each road has a known repair cost. You may repair any subset of the roads. After the repairs, every city must be reachable from every other city using repaired roads only.

Find the minimum possible total repair cost. Report impossibility if no such subset exists.

Input

  • The first line contains integers n,mn,m: the number of cities and roads.
  • Each of the next mm lines contains a,b,ca,b,c, describing a two-way road between cities a,ba,b with repair cost cc. Cities are numbered from 11 to nn. There are no self-loops or parallel roads.

Output

Print the minimum total repair cost, or IMPOSSIBLE if the cities cannot all be connected.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤m≤2⋅1051\le m\le 2\cdot 10^5; 1≤a,b≤n, a≠b1\le a,b\le n,\ a\ne b; 1≤c≤1091\le c\le 10^9; m≤n(n−1)/2m\le n(n-1)/2; đường không trùng.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤m≤2⋅1051\le m\le 2\cdot 10^5; 1≤a,b≤n, a≠b1\le a,b\le n,\ a\ne b; 1≤c≤1091\le c\le 10^9; m≤n(n−1)/2m\le n(n-1)/2; đường không trùng.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤m≤2⋅1051\le m\le 2\cdot 10^5; 1≤a,b≤n, a≠b1\le a,b\le n,\ a\ne b; 1≤c≤1091\le c\le 10^9; m≤n(n−1)/2m\le n(n-1)/2; đường không trùng.

Examples

Input

5 6
1 2 3
2 3 5
2 4 2
3 4 8
5 1 7
5 4 4

Output

14

Explanation

Choose roads (2,4)(2,4) of cost 22, (1,2)(1,2) of cost 33, (4,5)(4,5) of cost 44, and (2,3)(2,3) of cost 55. They connect all five cities, costing 2+3+4+5=142+3+4+5=14.