Under construction

Still being built. Things may change

Gemini Tree (Ver.Jadeite)

Time limit5sMemory limit1024 MB

Summary
Given a weighted tree with green and blue stones, find the minimum swap cost to split it into single-color parts after each edge length update.
Level

Hard9 of 10

Topics
Tree, Divide and conquer, Segment tree
Solved
No attempts yet

Problem

Consider a tree with a green or blue stone placed on each vertex. Such a tree is called a "Gemini Tree" if condition 3 can be satisfied after performing the following operations 1 and 2.

  1. First, select pairs of vertices that are directly connected by an edge and exchange the stones on their two endpoints. Repeat this zero or more times.
  2. Second, select at most one edge and delete it.
  3. Then the tree splits into at most two connected components, and each component contains only one type of stone.

Consider a Gemini Tree where each edge has a given length, and define its value as follows.

  1. First, select pairs of vertices that are directly connected by an edge and exchange the stones on their endpoints. Repeat this zero or more times. Each exchange costs the length of that edge.
  2. Second, select at most one edge and delete it.
  3. Then the tree splits into at most two connected components, and each component contains only one type of stone.
  4. The value is the minimum total cost of operation 1 needed to satisfy condition 3.

Stones are not moved when computing the value.

You are given a Gemini Tree with NN vertices, where NN is odd. The ii-th edge connects vertices uiu_i and viv_i and has length wiw_i. The stone colors form the string S=s1s2…sNS = s_1s_2\dots s_N, where sis_i is the color of vertex ii.

You perform QQ operations on this tree in order. The jj-th operation is given by two integers eje_j and aja_j, and increases the length of the eje_j-th edge by aja_j. The change stays in effect for all later operations. After each operation, print the value of the tree.

Input

NN

s1s2…sNs_1s_2 \dots s_N

u1u_1 v1v_1 w1w_1

⋮\vdots

uN−1u_{N-1} vN−1v_{N-1} wN−1w_{N-1}

QQ

e1e_1 a1a_1

⋮\vdots

eQe_Q aQa_Q

Output

Print QQ lines. On the jj-th line, print the value of the tree after the jj-th operation. Print a newline at the end of each line.

Constraints

  • All inputs are integers.
  • 3≤N≤1053 \le N \le 10^5
  • NN is odd.
  • sis_i is either "G" or "B", the color of vertex ii. "G" means green and "B" means blue.
  • 1≤ui,vi≤N1 \le u_i, v_i \le N
  • 0≤wi≤1050 \le w_i \le 10^5
  • The given graph is a Gemini Tree.
  • 1≤Q≤1051 \le Q \le 10^5
  • 1≤ej≤N−11 \le e_j \le N-1
  • 1≤aj≤1051 \le a_j \le 10^5

Hint

In the first sample, there is one green stone. Therefore, the problem is to move this stone to one of the leaves at minimal cost.

Examples3

  1. Example 1

    Input
    5
    GBBBB
    1 2 0
    1 3 0
    2 4 0
    2 5 0
    5
    1 1
    2 3
    3 3
    4 10
    2 2
    
    Expected output
    0
    1
    1
    3
    4
    
  2. Example 2

    Input
    5
    BGBGB
    1 2 0
    1 3 0
    2 4 0
    2 5 1000
    4
    4 1
    3 1
    1 1
    2 1
    
    Expected output
    0
    1
    3
    4
    
  3. Example 3

    Input
    7
    GGGBBBB
    1 5 1
    2 5 1
    7 5 0
    7 6 0
    3 6 0
    4 6 0
    6
    5 1
    5 1
    5 1
    6 3
    3 3
    5 100000
    
    Expected output
    1
    2
    2
    3
    6
    11