Under construction

Still being built. Things may change

Protect the Pollen!

Interview

Time limit5sMemory limit1024 MB

Summary
Choose flower families so that no two adjacent flowers in the tree are both chosen, with at most S bees total, maximizing total pollination power.
Level

Medium5 of 10

Topics
Tree, Dynamic programming
Solved
No attempts yet

Problem

The Flariana flowers and the bumblebees form one of the nicest partnerships in the rainforest. In spring, several flowers bloom and start producing pollen. Special vines form a network of bridges between the flowers. Using the vines, there is exactly one way to get from each flower to any other flower. Every flower has a family of bees on it. This family protects all of the vines that touch that flower, so every vine is protected by two families. The family on flower kk has sks_k bees and a pollination power of pkp_k. One day, a bee scout reported a new flower patch over the hill, and it needs a group of bees to help pollinate it. As the bee queen, you select a set of families to send on the mission. For every vine, at least one of the two families protecting it must stay behind, so the vine remains protected. All bees in the selected families must go. You can send at most SS bees on the mission in total. Find the largest total pollination power you can send on the mission.

Input

The first line contains the integers NN (1≤N≤3001 \leq N \leq 300), the number of flowers, and SS (1≤S≤3001 \leq S \leq 300), the maximum number of bees you can send. The flowers are numbered 1 to NN. The next NN lines describe the families. Each line contains two integers sks_k (1≤sk≤3001 \leq s_k \leq 300), the number of bees in the family, and pkp_k (1≤pk≤1001 \leq p_k \leq 100), the pollination power of the family. The last N−1N-1 lines describe the vines. Each line contains two distinct integers uu (1≤u≤N1 \leq u \leq N) and vv (1≤v≤N1 \leq v \leq N), meaning there is a vine between flowers uu and vv.

Output

Print the largest total pollination power that you can send on the mission.

Examples2

  1. Example 1

    Input
    5 10
    2 1
    2 2
    2 4
    2 8
    2 16
    1 2
    2 3
    3 4
    4 5
    
    Expected output
    21
    
  2. Example 2

    Input
    7 10
    1 7
    2 4
    5 18
    2 3
    3 12
    9 20
    2 8
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    
    Expected output
    33