Under construction

Still being built. Things may change

Crystalfly

Time limit2sMemory limit1024 MB

Summary
Given a tree where each vertex has flies and a 1 to 3 second disappearance timer, find the most flies Paimon can catch starting from vertex 1.
Level

Hard9 of 10

Topics
Dynamic programming, Tree, DFS
Solved
No attempts yet

Problem

Paimon is catching crystalflies on a tree, which are a special kind of butterflies in Teyvat. A tree is a connected graph consisting of nn vertices and (n−1)(n - 1) undirected edges.

Pixiv ID: 93964680

There are initially aia_i crystalflies on the ii-th vertex. When Paimon reaches a vertex, she can catch all the remaining crystalflies on the vertex immediately. However, the crystalflies are timid. When Paimon reaches a vertex, all the crystalflies on the adjacent vertices will be disturbed. For the ii-th vertex, if the crystalflies on the vertex are disturbed for the first time at the beginning of the t′t'-th second, they will disappear at the end of the (t′+ti)(t' + t_i)-th second.

At the beginning of the 00-th second, Paimon reaches vertex 11 and stays there before the beginning of the 11-st second. Then at the beginning of each following second, she can choose one of the two operations:

  • Move to one of the adjacent vertices of her current vertex and stay there before the beginning of the next second (if the crystalflies in the destination will disappear at the end of that second, she can still catch them).
  • Stay still in her current vertex before the beginning of the next second.

Calculate the maximum number of crystalflies Paimon can catch in 101010101010^{10^{10^{10^{10}}}} seconds.

Input

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains an integer nn (1≤n≤1051 \le n \le 10^5) indicating the number of vertices.

The second line contains nn integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_n (1≤ai≤1091 \le a_i \le 10^9), where aia_i is the number of crystalflies on the ii-th vertex.

The third line contains nn integers t1,t2,⋯ ,tnt_1, t_2, \cdots, t_n (1≤ti≤31 \le t_i \le 3), where tit_i is the time before the crystalflies on the ii-th vertex disappear after being disturbed.

For the next n−1n - 1 lines, the ii-th line contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n) indicating an edge connecting vertices uiu_i and viv_i in the tree.

The sum of nn over all test cases does not exceed 10610^6.

Output

For each test case, output one line containing one integer: the maximum number of crystalflies Paimon can catch.

Hint

For the first sample test case, follow the strategy below.

  • During the 00-th second

    • Paimon arrives at vertex 11;
    • Paimon catches 11 crystalfly;
    • Crystalflies in vertices 22 and 33 are disturbed.
  • During the 11-st second

    • Paimon arrives at vertex 33;
    • Paimon catches 100100 crystalflies.
  • During the 22-nd second

    • Paimon arrives at vertex 11;
    • Crystalflies in vertex 22 disappear.
  • During the 33-rd second

    • Paimon arrives at vertex 22;
    • Crystalflies in vertices 44 and 55 are disturbed.
  • During the 44-th second

    • Paimon arrives at vertex 55;
    • Paimon catches 1000010000 crystalflies;
    • Crystalflies in vertex 44 disappear.

For the second sample test case, the optimal strategy is the same as for the first. The crystalflies in vertex 22 are scheduled to disappear at the end of the 33-rd second (instead of the 22-nd), which lets Paimon catch them.

Examples1

  1. Example 1

    Input
    2
    5
    1 10 100 1000 10000
    1 2 1 1 1
    1 2
    1 3
    2 4
    2 5
    5
    1 10 100 1000 10000
    1 3 1 1 1
    1 2
    1 3
    2 4
    2 5
    
    Expected output
    10101
    10111