Universities

On a tree whose nodes are black or white with weighted happiness, find the maximum total weight of a path that contains equally many black and white nodes.

Medium6TreePrefix sumDynamic programmingNo attempts yetTime limit1sMemory limit1024 MB

Problem

You have just graduated from high school and are looking for a university to enroll in. Bytelandia has NN magic universities, and each one teaches either black magic or white magic. There are N1N - 1 bidirectional roads between the universities, and each road connects two different universities. The roads are laid out so that exactly one path exists between any two universities.

You plan to visit some of the universities. Each university has a happiness factor, and visiting it increases your overall happiness by that amount. If the happiness factor is negative, your overall happiness decreases instead.

To plan the trip you pick two different universities, the departure and the destination. You visit every university on the path between them, including both endpoints. To keep the trip balanced, you must visit the same number of white magic universities as black magic universities.

Find the largest possible sum of happiness factors over all trips that visit the same number of black magic and white magic universities.

Input

The input consists of four lines.

The first line contains the number of universities NN (2N1052 \le N \le 10^5). The universities are numbered from 11 to NN.

The second line contains a string of length NN made only of the characters "B" and "W". If the ii-th character is "B", university ii teaches black magic, and if it is "W", university ii teaches white magic. At least one university teaches black magic and at least one teaches white magic.

The third line contains the happiness factors h1,h2,,hNh_1, h_2, \dots, h_N separated by spaces (105hi105-10^5 \le h_i \le 10^5).

The fourth line contains N1N - 1 integers v1,v2,,vN1v_1, v_2, \dots, v_{N-1} separated by spaces. viv_i means a road connects university viv_i and university i+1i + 1 (1vii1 \le v_i \le i).

Output

Print one integer, the maximum sum of happiness factors over all trips that visit the same number of black magic and white magic universities.