Pairs of vertices

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.

Problem information

Author: Salvador Roura
Event: Vint-i-quatrè Concurs de Programació de la UPC - Final
Date: 2026-10-07

Generation: 2026-10-03T14:43:21.705Z

© Jutge.org, 2006–2026.
https://jutge.org