The Treeland 2.0

0

0 votes
Hard
Problem

In the country Treeland there are n cities and n1 bidirectional roads. Each road connects some pair of cities and from any city you can get to any other one using only the given roads. City 1 is the root of the tree. There is a value ai associated with each city i. For any city i, there exists a set Si which contains the distinct values of all cities j such that j lies in sub-tree rooted at city i and it's distance from city i is divisible by magic number m. Hence no element of set Si repeats. Strength of city i is the number of elements in above defined set Si whose value is strictly less than ai. Find aixi where xi is Strength of city i.

Input Format

First line contains two integers n - number of cities and m - magic number. Next n1 line contains two integers u and v denoting edge between city u and city v. Next line contains n integers - where each integer ai is value of city i.

Output Format

Print single integer which is answer of the problem - aixi.

Input Constraints

  • 1 <= n <= 105

  • 1 <= m <= 13

  • 1 <= u,v <= n

  • 1 <= ai <= 106

Setter : Tanmay Patel

Time Limit: 1.5
Memory Limit: 256
Source Limit:
Editor Image

?