Coloring a tree
Practice
2 (4 votes)
Easy Medium
Problem
4% Success 669 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a tree with n nodes. You are required to color the tree with r colors. Cost of coloring a node with color i is \(A_i\). Also, for each edge, such that the nodes at its end point are colored with the same color i, there is an additional cost of \(B_i\). You are required to find the minimum cost to color all the nodes of the tree.

Input

The first line of the input contains 2 integers n, number of nodes in the tree and r, number of colors.

The second line of the input contains r integers deonting the sequence A.

The third line of the input contains r integers deonting the sequence B.

Each of the next \(n-1\) lines conatins two integers u and v denoting the two nodes which are connected by an edge.

Output

Print one line denoting the minimum cost to color all the nodes in the tree.

Constraints

  • \( 1\le n \le 100000\)
  • \( 1 \le r \le 1000\)
  • \(0 \le A_i, B_i \le 1000000000\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
Dynamic ProgrammingAlgorithms1-DTreesDFS
Points:30
4 votes
Tags:
Dynamic ProgrammingGraphMath BasicTreeTreesAlgorithms