You are given a tree with vertices numbered from to . Each edge has a non-negative weight and one of two colors: yellow or blue.
You need to perform the following operation exactly once: choose two distinct vertices and and add a new edge between them. The color of the new edge may be chosen arbitrarily, and its weight is .
It is allowed to add a new edge between vertices that are already connected by an edge of the tree. In this case, the two parallel edges form a cycle of length two, and such a cycle is also considered.
After adding the new edge, exactly one cycle is formed.
We call a cycle good if, when traversing it, the colors of the edges alternate. In other words, any two adjacent edges of the cycle have different colors, including the last and the first edges of the cycle.
The weight of a cycle is the sum of the weights of all its edges.
Find the maximum possible weight of a good cycle that can be obtained after performing the operation.
The first line contains an integer (2 \leq n \leq 2 \cdot 10^5) — the number of vertices in the tree.
Each of the next n-1 lines contains four values , , , and (1 \leq u, v \leq n, 0 \leq w \leq 10^9) — the endpoints of the edge, its weight, and its color, respectively.
If Y, the edge is yellow, and if B, the edge is blue.
It is guaranteed that the given edges form a tree.
Print one integer — the maximum possible weight of a good cycle.
Example 1
Example 2
In the first example, you can add an edge between vertices and of yellow color.
Then the cycle 3 \to 2 \to 1 \to 4 \to 3 is formed. The colors of its edges in traversal order are blue, yellow, blue, yellow, so the cycle is good.
Its weight is 6 + 4 + 10 = 20.
In the second example, you can add an edge between vertices and of blue color.
Then a cycle is formed that contains the path 4 \to 3 \to 2 \to 1 \to 7 \to 8. The colors of the edges on this path are yellow, blue, yellow, blue, yellow. After adding the blue edge between vertices and , the colors of all edges in the cycle will alternate.
The weight of this cycle is 7 + 6 + 8 + 9 + 5 = 35.
Subtask ( points): n \le 20.
Subtask ( points): n \le 2000.
Subtask ( points): the tree is a path.
Subtask ( points): all edges have the same color.
Subtask ( points): if the tree is rooted at vertex , each vertex has at most two children.
Subtask ( points): n \le 5 \cdot 10^4.
Subtask ( points): No additional constraints.