Back to School '16: Cherry Tree

View as PDF

Submit solution

Points:12 (partial)
Time limit:2.0s
Memory limit:64M

Problem type

To celebrate the start of the school year, your school has planted a cherry tree in the front yard. However, this is no ordinary cherry tree. This cherry tree has N cherry patches, each with a specific number of cherries. It also has N - 1 branches, each with a specific weight, connecting the patches to one another.

Yui is a big fan of cherry trees, but unfortunately does not have one of her own. She decides to make a single cut to the tree and take that part back home. She will only take home a portion that has at least C cherries and a total branch weight which is at most K.

Since patch 1 is always connected to the trunk of the tree, she will never take home a portion that has patch 1 (she isn’t crazy enough to cut down the entire tree).

Since Yui isn’t taking computer science this year, help her by writing a program to determine where to cut the tree!

Input Specification

The first line contains three space separated integers, N (1 \le N \le 10000), C (1 \le C \le 10^5) and K (1 \le K \le 10^5), representing the number of patches on the tree, the number of cherries Yui wants, and the maximum weight she will take home.

The next line contains N space separated integers c_{i} (1 \le c_{i} \le 10^5), representing the number of cherries on the i^{th} patch.

The next N - 1 lines each contain three integers a_{i}, b_{i} (1 \le a_{i}, b_{i} \le N) and k_{i} (1 \le k_{i} \le 10^5), representing the two patches that the i^{th} branch connects as well as its weight.

Output Specification

Output one integer, indicating the number of possible unique cuts that Yui can make to fulfill her requirements.

Sample Input

8 5 15
1 5 9 4 3 4 8 2
1 2 3
1 3 2
2 4 5
2 5 4
3 6 6
3 7 9
3 8 1

Sample Output


Explanation for Sample Input

Shown below is a visualization of the tree in the sample input:


The larger number on each patch represents the number of cherries on that patch.

In this case, the branch connecting patches 1 and 2 can be cut to obtain a portion with 12 cherries and a weight of 12. The branch connecting patches 3 and 7 can also be cut for 8 cherries and a weight of 9.


  • 1
     commented on Jan. 8, 2017
    Wrong ans

    Got everything correct until batch 2 case 6 and batch 3 case 6, what could I be missing, code works for sample input and all of batch one.

    • 0
       commented on Jan. 8, 2017

      make sure your program works for certain edge cases (look at constraints)

  • 1
     commented on Jan. 8, 2017
    I am getting invalid Return IR?

    Why am I getting invalid return?

    • 1
       commented on Jan. 8, 2017

      Python has a relatively small recursion limit, try

      import sys

  • 2
     commented on Sept. 25, 2016 edited

    NVM got it. Apparently when you cut, the weight of the cut one is included and that was screwing me over.