You are given an undirected, connected graph with vertices and edges (i.e., a tree), and an integer . Every vertex has an integer value . You must choose pairs of adjacent vertices such that all endpoints are distinct. Your goal is to maximize the sum of the values of the chosen vertices.
Input consists of several cases, each with , , (vertices are numbered from 1 to ), and pairs indicating an edge between and . Assume , , , and that the given edges indeed define a tree.
For each case, if it is impossible to choose exactly
pairs of adjacent vertices, just print “no”. Otherwise,
print the largest possible total sum.
Input
7 2 4 -3 -1 10 -7 6 2 1 2 2 6 6 5 5 7 2 4 2 3 6 3 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1 2 2 3 3 4 4 5 5 6 4 2 10 20 30 40 4 1 4 2 4 3
Output
6 -6000000000 no