Ashish has a tree consisting of $n$ nodes numbered $1$ to $n$ rooted at node $1$. The $i$-th node in the tree has a cost $a_i$, and binary digit $b_i$ is written in it. He wants to have binary digit $c_i$ written in the $i$-th node in the end.

To achieve this, he can perform the following operation any number of times:   Select any $k$ nodes from the subtree of any node $u$, and shuffle the digits in these nodes as he wishes, incurring a cost of $k \cdot a_u$. Here, he can choose $k$ ranging from $1$ to the size of the subtree of $u$. 

He wants to perform the operations in such a way that every node finally has the digit corresponding to its target.

Help him find the minimum total cost he needs to spend so that after all the operations, every node $u$ has digit $c_u$ written in it, or determine that it is impossible.


-----Input-----

First line contains a single integer $n$ $(1 \le n \le 2 \cdot 10^5)$ denoting the number of nodes in the tree.

$i$-th line of the next $n$ lines contains 3 space-separated integers $a_i$, $b_i$, $c_i$ $(1 \leq a_i \leq 10^9, 0 \leq b_i, c_i \leq 1)$  — the cost of the $i$-th node, its initial digit and its goal digit.

Each of the next $n - 1$ lines contain two integers $u$, $v$ $(1 \leq u, v \leq n, \text{ } u \ne v)$, meaning that there is an edge between nodes $u$ and $v$ in the tree.


-----Output-----

Print the minimum total cost to make every node reach its target digit, and $-1$ if it is impossible.


-----Examples-----
Input
5
1 0 1
20 1 0
300 0 1
4000 0 0
50000 1 0
1 2
2 3
2 4
1 5

Output
4
Input
5
10000 0 1
2000 1 0
300 0 1
40 0 0
1 1 0
1 2
2 3
2 4
1 5

Output
24000
Input
2
109 0 1
205 0 1
1 2

Output
-1


-----Note-----

The tree corresponding to samples $1$ and $2$ are: [Image]

In sample $1$, we can choose node $1$ and $k = 4$ for a cost of $4 \cdot 1$ = $4$ and select nodes ${1, 2, 3, 5}$, shuffle their digits and get the desired digits in every node.

In sample $2$, we can choose node $1$ and $k = 2$ for a cost of $10000 \cdot 2$, select nodes ${1, 5}$ and exchange their digits, and similarly, choose node $2$ and $k = 2$ for a cost of $2000 \cdot 2$, select nodes ${2, 3}$ and exchange their digits to get the desired digits in every node.

In sample $3$, it is impossible to get the desired digits, because there is no node with digit $1$ initially.

Difficulty: interview

Starter code:

Solutions:
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import deque
N = int(input())
C, Y = [], []
for _ in range(N):
    a, b, c = list(map(int, input().split()))
    C.append(a)
    Y.append(c - b)

if sum(Y):
    print(-1)
    return

X = [[] for i in range(N)]
for i in range(N-1):
    x, y = list(map(int, input().split()))
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

for i in R[1:]:
    C[i] = min(C[i], C[P[i]])

ans = 0
for i in R[1:][::-1]:
    if Y[i] * Y[P[i]] < 0:
        ans += C[P[i]] * min(abs(Y[i]), abs(Y[P[i]]))
    Y[P[i]] += Y[i]

print(ans * 2)


import sys
input = sys.stdin.readline
n = int(input())
cbc = [list(map(int,input().split())) for i in range(n)]
ab = [list(map(int,input().split())) for i in range(n-1)]
graph = [[] for i in range(n+1)]
if n == 1:
  if cbc[0][1] != cbc[0][2]:
    print(-1)
  else:
    print(0)
  return
deg = [0]*(n+1)
for a,b in ab:
  graph[a].append(b)
  graph[b].append(a)
  deg[a] += 1
  deg[b] += 1
deg[1] += 1
stack = [1]
par = [0]*(n+1)
par[1] = -1
leaf = []
while stack:
  x = stack.pop()
  if x != 1 and len(graph[x]) == 1:
    leaf.append(x)
  for y in graph[x]:
    if par[y]:
      continue
    par[y] = x
    cbc[y-1][0] = min(cbc[y-1][0],cbc[x-1][0])
    stack.append(y)
dp = [[0,0] for i in range(n+1)]
ans = 0
while leaf:
  x = leaf.pop()
  p = par[x]
  if cbc[x-1][1] != cbc[x-1][2]:
    if cbc[x-1][1] == 1:
      dp[x][0] += 1
    else:
      dp[x][1] += 1
  if min(dp[x][0],dp[x][1]):
    if dp[x][0] > dp[x][1]:
      dp[x][0] -= dp[x][1]
      ans += cbc[x-1][0]*dp[x][1]*2
      dp[x][1] = 0
    else:
      dp[x][1] -= dp[x][0]
      ans += cbc[x-1][0]*dp[x][0]*2
      dp[x][0] = 0
  dp[p][0] += dp[x][0]
  dp[p][1] += dp[x][1]
  deg[p] -= 1
  if deg[p] == 1:
    leaf.append(p)
if dp[1][0] != dp[1][1]:
  print(-1)
else:
  print(ans)

#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(10000000)
mod = 1000000007
def solve():
    n = I()
    a = []
    b = []
    c = []
    for _ in range(n):
        x,y,z = LI()
        a.append(x)
        b.append(y)
        c.append(z)
    v = [[] for i in range(n)]
    for _ in range(n-1):
        x,y = LI()
        x -= 1
        y -= 1
        v[x].append(y)
        v[y].append(x)
    if b.count(1) != c.count(1):
        print(-1)
        return
    q = deque([0])
    q2 = deque()
    d = [1]*n
    d[0] = 0
    ans = 0
    p = [[0]*2 for i in range(n)]
    while q:
        x = q.popleft()
        ax = a[x]
        if b[x] != c[x]:
            p[x][b[x]] = 1
        for y in v[x]:
            if d[y]:
                d[y] = 0
                if ax < a[y]:
                    a[y] = ax
                q.append(y)
                q2.append((x,y))
    while q2:
        x,y = q2.pop()
        p[x][0] += p[y][0]
        p[x][1] += p[y][1]
        m = min(p[x])
        ans += m*a[x]
        p[x][0] -= m
        p[x][1] -= m
    print(ans*2)
    return

#Solve
def __starting_point():
    solve()

__starting_point()