Power Supply

Given a tree whose vertices are supplies or demands and whose edges have capacities, decide if deleting some edges yields subtrees each holding exactly one supply that meets its demands.

Medium7TreeDFSGreedyNo attempts yetTime limit1sMemory limit512 MB

Problem

A tree TT with NN vertices is a power delivery network. Each vertex of TT is either a supply vertex or a demand vertex, and it carries one positive integer: the supply of a supply vertex, or the demand of a demand vertex. Each demand vertex receives power from exactly one supply vertex along the edges of TT, so power flows through the edges. Each edge carries a positive integer called its capacity.

Delete edges to partition TT into subtrees. Deleting no edge is allowed, so TT itself can be the partition. The partition must satisfy both conditions below.

  1. Each subtree contains exactly one supply vertex, and that supply is no less than the sum of all demands in the subtree.
  2. The power flowing through each edge is no more than the capacity of that edge.

Decide whether TT has such a partition.

Figure 1(a) shows an example of a tree TT. Each supply vertex is drawn as a rectangle and each demand vertex as a circle, the supply or the demand is written inside the shape, and the capacity is attached to each edge. Figure 1(b) shows one partition that satisfies the conditions. The deleted edges are dashed, the border of each subtree is dotted, the number on an edge is the power flowing through that edge, and the arrow gives the direction of the flow.

Figure 1. An example of a partition.

Input

The first line contains the number of vertices NN of the tree TT (1N3000001 \le N \le 300000). The vertices are numbered 1,2,,N1, 2, \ldots, N.

The ii-th of the next NN lines contains two integers aa and bb (aa is 0 or 1, 1b1091 \le b \le 10^9). If a=0a = 0, vertex ii is a supply vertex with supply bb. If a=1a = 1, vertex ii is a demand vertex with demand bb.

Each of the next N1N - 1 lines contains three integers xx, yy, and zz (1x,yN1 \le x, y \le N, 1z1091 \le z \le 10^9), describing an edge that joins vertex xx and vertex yy and has capacity zz.

Output

Print 1 on one line if TT has a partition that satisfies the conditions, and 0 otherwise.