This page is still under construction.

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

Power Shortage

Interview

Time limit1sMemory limit256 MB

Summary
Given a connected weighted undirected graph, keep a subset of roads so every pair of houses stays connected while maximizing the total length of removed roads.
Level

Medium5 of 10

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

Problem

Seongjin is the mayor of a city, but the city is short on money and struggling with a power shortage. So he decides to turn off some of the streetlights that were originally lit on every road. Keeping a road's streetlights on costs money each day equal to the road's length in meters, so turning some off saves exactly that much money.

However, it is dangerous if traveling between two houses forces you to pass along a road whose lights are off. Therefore, for every pair of houses in the city, it must be possible to travel between them using only lit roads.

Find the maximum amount of money that can be saved while satisfying this condition.

Input

The input consists of several test cases.

The first line of each test case contains the number of houses mm and the number of roads nn. (1≤m≤2000001 \le m \le 200000, m−1≤n≤200000m - 1 \le n \le 200000)

Each of the next nn lines describes a road with three integers xx, yy, zz, meaning there is a bidirectional road between house xx and house yy whose length is zz meters. (0≤x,y<m0 \le x, y < m, x≠yx \ne y)

The city is always a connected graph; that is, for any two houses there exists a path between them. The sum of the lengths of all roads in the city is less than 2312^{31} meters.

The last line of the input contains two zeros in place of mm and nn; this line is not processed.

Output

For each test case, print on one line the maximum cost that can be saved.

Examples3

  1. Example 1

    Input
    7 11
    0 1 7
    0 3 5
    1 2 8
    1 3 9
    1 4 7
    2 4 5
    3 4 15
    3 5 6
    4 5 8
    4 6 9
    5 6 11
    0 0
    
    Expected output
    51
    
  2. Example 2

    Input
    2 1
    0 1 10
    0 0
    
    Expected output
    0
    
  3. Example 3

    Input
    3 3
    0 1 1
    1 2 2
    0 2 3
    0 0
    
    Expected output
    3