Under construction

Still being built. Things may change

Morning Sweet Potato

Time limit3sMemory limit512 MB

Summary
Given a cactus-like graph with root 1, find the maximum total squared length of tuber cycles that stay pulled out under a minimum-strength cut to the root tips.
Level

Hard8 of 10

Topics
Graph, Tree, DFS, Dynamic programming
Solved
No attempts yet

Problem

Junpyo has gained a lot of weight. His body fat is 25%, which is obese. To eat a balanced diet, Junpyo started eating a sweet potato every morning. After he came to appreciate the true taste of sweet potatoes, he decided to farm them himself and dig up the best sweet potato every morning.

sweet potato graph

A sweet potato plant is divided into an aboveground part and an underground part, and Junpyo cares only about the underground part. Junpyo calls the abstract graph of the underground part a sweet potato graph.

A sweet potato graph is a connected undirected graph that can be divided into five kinds of components. Each edge has a strength cc.

  1. Root of the sweet potato graph

The root is the single vertex that connects to the aboveground part. It is always vertex 1.

  1. Hard roots

The hard roots form a tree rooted at vertex 1 of the sweet potato graph. Any two vertices are connected by exactly one path.

  1. Tuber (sweet potato)

The tuber is the edible part of a sweet potato. Each tuber is attached to one of the ends of the hard roots. If a tuber has NN vertices, it is drawn as a convex NN-gon. In other words, a tuber is a simple cycle.

The mass of a tuber is the square of its number of vertices. For example, a tuber with 5 vertices has a mass of 52=255^2=25.

  1. Thin roots

Thin roots hang from every vertex of a tuber except the vertex that connects to the hard roots. They form a tree.

  1. Root tips

Root tips are the leaves of the sweet potato graph. They are the ends of the hard roots that have no tuber attached, and the ends of the thin roots. Root tips grip the soil tightly, so when the root is pulled out, the tips stay in the ground.

To harvest a sweet potato, Junpyo holds the root of the sweet potato graph and pulls it out of the ground with a single very strong force. Pulling the graph splits its vertices into two groups: the vertices pulled out of the ground and the vertices left in the ground. Every vertex belongs to exactly one group. Every pulled-out vertex is connected to the root, and every vertex left in the ground is connected to some root tip.

To split the graph into two groups, some edges must be cut. A sweet potato graph is always cut so that the total strength of the cut edges is minimized. If bad luck strikes, an edge of a tuber may be cut. In that case, the sweet potato is said to be broken. Junpyo is a sweet potato master, so he pulls out the graph so that the total mass of the tubers pulled out is maximized. He only handles perfect sweet potatoes, so broken ones are not counted.

Compute the total mass of the sweet potatoes Junpyo harvests by pulling out the graph.

Input

The first line contains the number of vertices NN and the number of edges MM of the sweet potato graph.

The next MM lines each contain three integers uu, vv, and cc. Each describes an edge with strength cc that connects vertices uu and vv.

Output

Print the total mass of the sweet potatoes that Junpyo harvests.

Constraints

2≤N≤1,000,0002 \le N \le 1{,}000{,}000

N−1≤M≤2,000,000N - 1 \le M \le 2{,}000{,}000

1≤u,v≤N1 \le u, v \le N, u≠vu \ne v

1≤c≤1,000,000,0001 \le c \le 1{,}000{,}000{,}000

An edge of the hard roots or of a tuber is never cut in a way that leaves a sweet potato (a vertex of a tuber) in the ground.

Examples1

  1. Example 1

    Input
    19 19
    1 2 23
    2 3 7
    3 4 6
    4 5 8
    2 6 17
    6 7 4
    7 8 9
    8 9 7
    9 10 4
    10 6 12
    7 11 4
    11 12 1
    11 13 2
    8 14 2
    14 15 1
    9 16 4
    16 17 3
    16 18 3
    10 19 6
    
    Expected output
    25