#P10754. [COI 2023] Nestabilnost
[COI 2023] Nestabilnost
Background
Source: https://hsin.hr/hio2023/。
Problem Description
The pine forest across the river, which was still bathed in May sunshine an hour ago, has now become blurry, hazy, and faded away. Only one huge tree remains, a tree with nodes...
Ivan is in room 119, observing this tree, which is firmly rooted at node . After looking at the tree more carefully, he notices that each node has a number on it. Suddenly, the definition of a -good subtree ("k-dobrog podstabla") flashes through his mind. If for every edge connecting nodes in a subtree (where is the parent of ), we have , and for every node in that subtree, we have , then this subtree is -good. For each number , there is a natural instability of a -good subtree, denoted by .
When he comes back to his senses, he notices a raft floating in front of the tree, and in his right hand he is holding a magic saw. Ivan decides to saw off some branches (edges). For each connected component (subtree) obtained by removing the sawed edges, he will choose a number such that this component is -good. Ivan decides to call such a set of choices—choosing a set of edges to cut, and choosing for each resulting subtree a valid (so that the subtree is -good)—a cutting ("rezanjem"). The instability ("Nestabilnost") of a cutting is defined as the sum of over all resulting subtrees. Please help Ivan determine the minimum possible instability of a cutting!
Input Format
The first line contains a positive integer , the number of nodes in the tree.
The second line contains integers, where the -th number is ().
The third line contains integers, where the -th number is ().
The next lines describe the structure of the tree. The -th line contains two integers and (), indicating that there is an edge between nodes and .
Output Format
In the only line, output the minimum possible instability of a cutting.
7
2 3 0 3 2 0 0
6 8 2 9 9 9 9
1 2
2 3
1 4
4 5
5 6
5 7
11
7
2 3 0 3 2 0 0
6 8 2 9 9 9 1
1 2
2 3
1 4
4 5
5 6
5 7
4
Hint
[Sample Explanation]

The left picture is sample 1, and the right picture is sample 2.
[Constraints]
In all subtasks, .
- Subtask 1 (12 points): , the tree is a chain, and node is an endpoint of the chain.
- Subtask 2 (20 points): , the tree is a chain, and node is an endpoint of the chain.
- Subtask 3 (7 points): .
- Subtask 4 (22 points): .
- Subtask 5 (39 points): No additional constraints.
Translated by ChatGPT 5