Pairs of vertices P14661


Statement
 

pdf   zip

You are given an undirected, connected graph with nn vertices and n−1n - 1 edges (i.e., a tree), and an integer pp. Every vertex xx has an integer value vxv_x. You must choose pp pairs of adjacent vertices such that all 2p2p endpoints are distinct. Your goal is to maximize the sum of the values of the chosen vertices.

Input

Input consists of several cases, each with nn, pp, v1,…,vnv_1, \dots, v_n (vertices are numbered from 1 to nn), and n−1n - 1 pairs xx yy indicating an edge between xx and yy. Assume 2≤n≤1052 \le n \le 10^5, 1≤p≤⌊n/2⌋1 \le p \le \lfloor n/2 \rfloor, −10−9≤vx≤109-10^{-9} \le v_x \le 10^9, and that the given edges indeed define a tree.

Output

For each case, if it is impossible to choose exactly pp pairs of adjacent vertices, just print “no”. Otherwise, print the largest possible total sum.

Public test cases
  • 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
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions