Training Site
Sideways banner

The Grand Tree

Input: Standard Input (stdin)
Output: Standard Output (stdout)
Memory limit: 128 megabytes
Time limit: 1.0 seconds

Time has come to pick the apples from The Grand Tree. The Grand Tree has N apples numbered 0 to N - 1, and N - 1 branches. Each branch connects two different apples. Any two apples are connected by exactly one sequence of branches. Each apple has a tastiness level that you, as a skilled apple picker, can determine just by looking at it. Unfortunately, The Grand Tree is rather Grand™, which means that you can only pick an apple by climbing up to it. Even more unfortunately, you are a bit clumsy, and cannot climb past an apple without knocking it off and damaging it. As such, you can climb along the branches to reach any apples you want, but you must pick every apple you climb past on the way. The trunk of the tree is located at apple 0, meaning you always start there, and you must pick apple 0 in order to begin climbing the tree. You may always pick apple 0.

You have decided ahead of time that you wish to harvest a selection of apples where the sum of the tastiness of all the apples you have picked is equal to M. You however, are aware that that might not always be possible, so you have decided to settle for the smallest possible sum of tastiness greater than or equal to M. Due to the staggering size of the tree, you cannot work out which apples to pick in your head, and thus have decided to sit down and write a program instead.


The first line of input consists of 2 integers N, and M.

The second line of input contains N integers t, the ith of which denotes the tastiness of the ith apple.

The next N - 1 lines each contain two integers x and y, denoting a branch between apple x and apple y.


Output a single integer: the lowest total achievable tastiness greater than or equal to M. It is guaranteed that there exists such a value.


  • 1 ≤ N, M ≤ 2{,}000
  • 1 ≤ t_i ≤ 2{,}000 for all (0 ≤ i ≤ N - 1)
  • 0 ≤ x, y ≤ N - 1


  • Subtask 1 (+20%): M ≤ 2
  • Subtask 2 (+30%): N = 10
  • Subtask 3 (+15%): x_i = i and y_i = i + 1 (for all 0 ≤ i ≤ N - 2). That is to say, the tree forms a line.
  • Subtask 4 (+35%): No further constraints apply

Sample Explanation

  • In Sample 1, we can pick apple 0 for a tastiness value of 3. This opens up apples 1 and 2. Picking apple 1 for a tastiness value of 1 opens up apple 3. Picking apple 3 for a tastiness value of 1 gets us up to a total tastiness of 5. Exactly what we need!
  • In Sample 2, apple 0 has a tastiness significantly more than what we need, but as we are not allowed to settle for a total tastiness any less than 4, we must pick it.


If you are using Python, select Python 3.6 (PyPy 7.3) among the language options when making a submission. Otherwise, your solution may not pass the time limit for some subtasks even if it is maximally efficient.

  • Sample Input 1

    5 5
    3 1 5 1 2
    0 1
    1 3
    0 2
    1 4

    Sample Output 1

  • Sample Input 2

    3 4
    100 1 3
    0 1
    0 2

    Sample Output 2