问题 A: Tree

内存限制:256 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:4 通过:3

题目描述

For a rootless tree with nnn vertices and n−1n-1n1 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 cost\text{cost}cost.

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

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

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

输入格式

The first line contains a positive integer n (1≤n≤3000)n\ (1\le n\le3000)n (1n3000), representing the number of vertices in the tree.
The second line contains a row of nnn integers ai (0≤ai≤1)a_i \ (0\le a_i\le 1)ai (0ai1), representing the initial color of the iii-th vertex (000 for white, 111 for black).
The third line contains a row of nnn integers costi (1≤costi≤109)cost_i\ (1\leq cost_i\leq 10^9)costi (1costi109), representing the cost of changing the iii-th vertex.
The next n−1n-1n1 line, each line contains three positive integers uj,vj,wj (1≤uj,vj,wj≤n)u_j,v_j,w_j\ (1\leq u_j,v_j,w_j\le n)uj,vj,wj (1uj,vj,wjn) 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 earnings-cost\text{earnings-cost}earnings-cost.

输入样例 复制

3
0 0 0
1 2 3
1 2 1
2 3 2

输出样例 复制

2

数据范围与提示

change the color of the first vertex