Under construction

Still being built. Things may change

Instability

Time limit1.5sMemory limit1024 MB

Summary
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 NN nodes, firmly rooted at node 11.

Ivan, in room 119, was watching the tree. On a closer look, he noticed that each node ii holds a number aia_i. Then the definition of a kk-good subtree came to him. A subtree is kk-good if, for every edge (u,v)(u, v) where uu is the parent of vv, av=(au+1) mod ka_v = (a_u + 1) \bmod k holds, and every node vv in the subtree satisfies av<ka_v < k. For each natural number kk, the instability of kk-good trees is given as f(k)f(k).

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 kik_i so that the subtree is kik_i-good. The pair of a chosen set of edges to cut and the numbers kik_i for each resulting subtree is called a cut. The instability of a cut is the sum of f(ki)f(k_i) over all resulting subtrees. Help Ivan find the minimum possible instability of a cut!

Input

The first line contains the natural number NN, the number of nodes in the tree.

The second line contains NN integers, where the ii-th integer is aia_i (0≤ai≤N−10 \le a_i \le N-1).

The third line contains NN integers, where the kk-th integer is f(k)f(k) (1≤f(k)≤1091 \le f(k) \le 10^9).

Each of the next N−1N-1 lines contains two integers uiu_i and viv_i, meaning there is an edge between nodes uiu_i and viv_i (1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \ne v_i).

Output

Print the minimum possible instability of a cut on a single line.

Hint

(a) Cut sketch for the first input(b) Cut sketch for the second input

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    11
    
  2. Example 2

    Input
    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
    
    Expected output
    4