Under construction

Still being built. Things may change

Maximum Weight Matching in a General Graph

Time limit2sMemory limit512 MB

Summary
Given a weighted undirected graph, find a matching with maximum total edge weight.
Level

Hard9 of 10

Topics
Graph, Greedy, Dynamic programming
Solved
No attempts yet

Problem

You are given a weighted undirected graph with NN vertices and MM edges. Write a program that prints the total weight of a maximum weight matching of that graph.

In a graph G=(V,E)G = (V, E), a matching TT is a subset of EE in which no two edges share a vertex. A maximum weight matching is a matching whose edge weights add up to the largest total. The empty set, which takes no edge at all, is also a matching.

Input

The first line contains the number of vertices NN (1≤N≤4001 \le N \le 400) and the number of edges MM (1≤M≤N(N−1)/21 \le M \le N(N-1)/2).

Each of the next MM lines holds the information of one edge: the numbers of the two distinct vertices it joins, and its weight.

A vertex number is a natural number between 1 and NN, and an edge weight is a natural number between 1 and 500,000,000. There is at most one edge between any two vertices.

Output

Print the total weight of a maximum weight matching on the first line.

Examples2

  1. Example 1

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

    Input
    7 20
    5 7 9
    3 7 4
    3 6 6
    2 5 8
    5 1 9
    1 3 6
    6 5 1
    2 7 4
    2 3 5
    6 4 2
    7 1 5
    5 4 4
    4 1 3
    5 3 9
    7 6 4
    2 1 3
    4 3 9
    6 2 7
    4 2 8
    6 1 10
    
    Expected output
    28