All Tracks Algorithms Dynamic Programming Problem

Coloring a tree
Tag(s):

Easy-Medium

Problem
Editorial
Analytics

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\)
SAMPLE INPUT
3 2
2 3
10 3
2 1
3 1
SAMPLE OUTPUT
7
Explanation

Color vertex 1 with color 2 (cost = 3)

Color vertices 2 & 3 with color 1 (cost = 2 each).

No additional cost because there is no edge such that both its end points are colored with same color.

Total cost = 3 + 2 + 2 = 7.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

IIT Delhi

Challenge Name

ICPC de Tryst

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications

?