Tree
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

For a rootless tree with n vertices and n-1 edges, each edge has edge weights. In the beginning, each vertex has a color (black or white), you can choose to reverse the color of some vertex, but this will pay the \text{cost}.

For a tree, we define its \text{earnings} as \sum\limits_{x\in V_1}\sum\limits_{y\in V_2}val(x,y), where V_1 is the set of white vertices and V_2 is the set of black vertices.

Where val(x,y) is the maximum edge weight in the shortest path from x to y.

You need to reverse some vertices (or not) so that the \text{earnings-cost} is maximum? You need to calculate the answer.

输入描述:

The first line contains a positive integer n\ (1\le n\le3000), representing the number of vertices in the tree.

The second line contains a row of n integers a_i \ (0\le a_i\le 1), representing the initial color of the i-th vertex (0 for white, 1 for black).

The third line contains a row of n integers cost_i\ (1\leq cost_i\leq 10^9), representing the cost of changing the i-th vertex.

The next n-1 line, each line contains three positive integers u_j,v_j,w_j\ (1\leq u_j,v_j,w_j\le n) represents an undirected edge and edge weight. It is guaranteed that the input is a tree.

输出描述:

A line of output with an integer represents the maximum \text{earnings-cost}.
示例1

输入

复制
3
0 0 0
1 2 3
1 2 1
2 3 2

输出

复制
2

说明

change the color of the first vertex
示例2

输入

复制
8
1 0 0 0 0 1 0 0
3 9 8 9 5 9 9 4
1 2 2
1 3 8
1 4 5
1 5 5
5 6 7
2 7 3
3 8 3

输出

复制
100