Harvesting Apples at Tree Nodes Within Distance k
InterviewTime limit1sMemory limit512 MB
Count the apples on vertices whose distance from root 0 is at most k in a given tree.
Statement
There is a tree with vertices and edges. The vertices are numbered from 0 to , and vertex 0 is the root. Every edge has length 1. Each vertex of has either 0 or 1 apple. You want to harvest the apples on the nodes whose distance from the root node is at most . Print the number of apples you can harvest.
Input
The first line contains the number of vertices and the integer , separated by a space.
From the second line onward, the next lines describe the edges. Each line contains one edge, given as a parent vertex number and a child vertex number , separated by a space.
The next line contains integers, separated by spaces, that give the apple count of each vertex from 0 to . The -th number is the number of apples on vertex . Each number is 0 or 1.
Output
Print the number of apples you can harvest on the first line.
Constraints
- ,
- The graph formed by the edges is a tree.
- The number of apples on each vertex is 0 or 1.