Instability
Time limit1.5sMemory limit1024 MB
Cut edges of a rooted tree so each piece is k-good for some k, minimizing the total of f(k) over all pieces.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming
- Solved
- No attempts yet
Problem
A pine forest on the far bank of the river, lit by the May sun only an hour ago, has blurred, smeared, and dissolved. Only one giant tree is left, a tree with nodes, firmly rooted at node .
Ivan, in room 119, was watching the tree. On a closer look, he noticed that each node holds a number . Then the definition of a -good subtree came to him. A subtree is -good if, for every edge where is the parent of , holds, and every node in the subtree satisfies . For each natural number , the instability of -good trees is given as .
When he looked back at the tree, Ivan was floating in front of it with a magic saw in his right hand. Ivan decided to cut some branches. Removing the cut edges splits the tree into several subtrees. For each resulting subtree, Ivan picks a number so that the subtree is -good. The pair of a chosen set of edges to cut and the numbers for each resulting subtree is called a cut. The instability of a cut is the sum of over all resulting subtrees. Help Ivan find the minimum possible instability of a cut!
Input
The first line contains the natural number , the number of nodes in the tree.
The second line contains integers, where the -th integer is ().
The third line contains integers, where the -th integer is ().
Each of the next lines contains two integers and , meaning there is an edge between nodes and (, ).
Output
Print the minimum possible instability of a cut on a single line.

