Under construction

Still being built. Things may change

Harvesting apples and pears from k tree nodes

Interview

Time limit3sMemory limit512 MB

Summary
Pick a connected set of at most k nodes containing root 0 to maximize apples times pears, then print the apple and pear counts.
Level

Medium5 of 10

Topics
Dynamic programming, Tree
Solved
No attempts yet

Problem

A tree TT has nn nodes and n−1n-1 edges. The nodes are numbered from 0 to n−1n-1, and node 0 is the root. The edges have no weights. Each node in TT has one apple, has one pear, or has neither. Starting from the root, you visit neighboring nodes to harvest apples and pears. When you visit at most kk nodes, output the number of apples and the number of pears that maximize (number of apples) ×\times (number of pears). If several pairs give the maximum product, output the pair with more apples. If several pairs have the same maximum product and the same number of apples, output the pair with more pears. A node visited more than once counts as visited once. The root counts as visited. Even if you visit a node with an apple or a pear several times, you can harvest only one apple or pear from it, on the first visit.

Input

The first line contains the number of nodes nn and the integer kk, separated by a space.

The next n−1n-1 lines each contain one edge. Each edge is given as a parent node number pp and a child node number cc, separated by a space.

The next line contains nn integers, separated by spaces. The ii-th integer gives the information of node i−1i-1, for nodes 0 to n−1n-1. A value of 0 means the node has neither an apple nor a pear. A value of 1 means the node has one apple only. A value of 2 means the node has one pear only.

Output

On the first line, print the number of apples and the number of pears that satisfy the conditions of the problem, separated by a space.

Constraints

  • 1≤k≤n≤171 \le k \le n \le 17
  • 0≤p,c≤n−10 \le p, c \le n-1, p≠cp \neq c
  • The graph formed by the edges is a tree.
  • The information of each node is 0, 1, or 2.

Examples1

  1. Example 1

    Input
    7 3
    0 1
    0 2
    1 3
    1 4
    2 5
    2 6
    1 1 2 2 2 2 1
    
    Expected output
    2 1