#P1948G. MST with Matching
MST with Matching
Description
You are given an undirected connected graph on vertices. Each edge of this graph has a weight; the weight of the edge connecting vertices and is (or if there is no edge between and ). All weights are positive integers.
You are also given a positive integer .
You have to build a spanning tree of this graph; i. e. choose exactly edges of this graph in such a way that every vertex can be reached from every other vertex by traversing some of the chosen edges. The cost of the spanning tree is the sum of two values:
- the sum of weights of all chosen edges;
- the maximum matching in the spanning tree (i. e. the maximum size of a set of edges such that they all belong to the chosen spanning tree, and no vertex has more than one incident edge in this set), multiplied by the given integer .
Find any spanning tree with the minimum cost. Since the graph is connected, there exists at least one spanning tree.
The first line contains two integers and (; ).
Then lines follow. The -th of them contains integers (), where denotes the weight of the edge between and (or if there is no such edge).
Additional constraints on the input:
- for every , ;
- for every pair of integers such that and , ;
- the given graph is connected.
Print one integer — the minimum cost of a spanning tree of the given graph.
Input
The first line contains two integers and (; ).
Then lines follow. The -th of them contains integers (), where denotes the weight of the edge between and (or if there is no such edge).
Additional constraints on the input:
- for every , ;
- for every pair of integers such that and , ;
- the given graph is connected.
Output
Print one integer — the minimum cost of a spanning tree of the given graph.
Note
In the first example, the minimum cost spanning tree consists of edges , and . The maximum matching for it is .
In the second example, the minimum cost spanning tree consists of edges , and . The maximum matching for it is .