QOJ.ac

QOJ

حد الوقت: 1.0 s حد الذاكرة: 512 MB مجموع النقاط: 100 قابلة للهجوم ✓

#9580. Insertion Sort Series

الإحصائيات

As is well known, power strips have a limited power rating. Once the total power drawn by multiple high-power appliances connected to a power strip simultaneously exceeds this limit, it can lead to unpredictable consequences. Therefore, when setting up electrical circuits, we must ensure that the power consumption of each power strip does not exceed its power limit.

However, planning the use of power strips is not easy, especially in a laboratory that only provides one power outlet. Consider a laboratory with $n+1$ nodes connected in a rooted tree structure, with nodes numbered $0$ to $n$. The root, node $0$, is the power outlet (by common knowledge, the power limit of an outlet is $2200\text{W}$). All leaf nodes are appliances, and the remaining nodes are power strips. Each appliance has its own operating power, and each power strip has its own power limit. A node representing a power strip or an outlet may contain both leaf nodes (appliances) and other power strips (which in turn provide power to subsequent appliances). Therefore, the actual power of a power strip or outlet is the sum of the operating power of all appliances in its subtree. If the actual power of any power strip or outlet exceeds its power limit, it is considered "illegal power usage."

Unfortunately, the wiring is already fixed, and it is clearly inconvenient to change the inherent positions of the power strips and appliances. The only thing that can be done, without changing the tree structure, is to swap the positions of two power strips. In other words, swap the power limits of two nodes. This operation can be performed any number of times (including zero).

The laboratory's safety officer wants you to write a program to determine whether it is possible to perform any number of swaps such that there is no illegal power usage in the circuit.

Input

The first line contains an integer $n$ ($1 \le n \le 10^5$), representing the number of nodes excluding the root node $0$ (the total number of power strips and appliances).

The next $n$ lines each contain two integers $f_i$ and $a_i$ ($0 \le f_i < i$, $0 \le a_i \le 10^9$), where the $i$-th line represents that the parent of node $i$ is node $f_i$. If node $i$ is a leaf node, the operating power of the corresponding appliance is $a_i\text{W}$; otherwise, the power limit of the power strip is $a_i\text{W}$.

Output

Output "YES" or "NO" (case-sensitive) in a single line, indicating whether it is possible to ensure there is no illegal power usage in the circuit.

Examples

Input 1

5
0 500
1 700
1 400
2 100
2 200

Output 1

YES

Input 2

5
0 500
1 700
1 400
2 100
2 300

Output 2

NO

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.