## Tootsie Rolls

View as PDF

Points: 12 (partial)
Time limit: 0.5s
Memory limit: 512M

Author:
Problem types

Emili has decided to buy a literal tree to put in her bedroom! She wants to decorate it with pieces of candy, attached together with strings. Each piece of candy has a tastiness value of that Emili assigns it. Some candy, such as Tootsie rolls, are so bad that they have a negative tastiness value! There are exactly strings connecting the candy, and all pieces of candy are connected together by the strings. The candies are numbered from to . Candy is at the very top of the tree, which is known as the root of the tree.

Having all the candy sitting on the tree has made Emily hungry! She plans to choose at most pieces of candy and eat all of the candy at and below it on the tree. In other words, she will eat all the candy in a candy's subtree. Of course, she is greedy, and so she wishes to choose pieces of candy such that the sum of the tastiness values of all the candy she eats is maximized. If she chooses a piece of candy, and there is a piece of candy with a negative tastiness below it, she will be forced to eat it :(.

Given the tree and the value , can you determine the the maximum sum of the tastiness values of the candies she eats?

#### Input Specification

The first line will contain two integers , the number of pieces of candy, and the special value .

The next line will contain integers, , the integer indicating the tastiness value of the piece of candy.

The next lines will each contain two integers, , indicating that candies and are connected by a string. It is guaranteed that all the pieces of candy are connected.

#### Output Specification

Output the maximum sum of the tastiness values of the candies Emili can eat.

No further constraints.

#### Sample Input 1

3 2
-1 1 1
1 2
1 3

#### Sample Output 1

2

#### Explanation For Sample 1

She can take candies and , along with all the candies below it (none). This gives a sum of , which is the maximum. If she took candies , and all of the candies below it , she would only have a sum of . The tree is drawn below, and the taken candies are highlighted in green.

#### Sample Input 2

4 1
-1 1 1 1
1 2
1 3
1 4

#### Sample Output 2

2

#### Explanation For Sample 2

Sometimes it may be better for Emili to take a negative tastiness candy for the extra sum of the other tastiness!

#### Sample Input 3

7 3
-5 0 5 -4 5 4 3
1 4
4 3
4 5
4 6
4 7
5 2

#### Sample Output 3

14

#### Explanantion For Sample 3

It is optimal for Emili to take candies , along with all the candies below it , for a total sum of . This is shown in the tree below. The candies Emili takes directly are in green, and the candies she takes as a result are shown in blue.