CF846E.Chemistry in Berland

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment.

Fortunately, chemical laws allow material transformations (yes, chemistry in Berland differs from ours). But the rules of transformation are a bit strange.

Berland chemists are aware of n materials, numbered in the order they were discovered. Each material can be transformed into some other material (or vice versa). Formally, for each i (2 ≤ i ≤ n) there exist two numbers x__i and k__i that denote a possible transformation: k__i kilograms of material x__i can be transformed into 1 kilogram of material i, and 1 kilogram of material i can be transformed into 1 kilogram of material x__i. Chemical processing equipment in BerSU allows only such transformation that the amount of resulting material is always an integer number of kilograms.

For each i (1 ≤ i ≤ n) Igor knows that the experiment requires a__i kilograms of material i, and the laboratory contains b__i kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)?

伊戈尔是贝尔兰国立大学(BerSU)化学系的一名研究生。他需要进行一项复杂的实验来完成他的毕业论文,但BerSU的实验室中缺少该实验所需的全部材料。

幸运的是,贝尔兰的化学定律允许材料之间的相互转化(是的,贝尔兰的化学与我们的有所不同)。不过,转化规则略显奇特。

贝尔兰的化学家们已知存在 $ n $ 种材料,按其被发现的先后顺序编号。每种材料均可转化为某些其他材料(反之亦然)。形式化地讲,对每个 $ i $(其中 $ 2 \leq i \leq n $),均存在两个数 $ x_i $ 和 $ k_i ,表示一种可能的转化方式:,表示一种可能的转化方式: k_i $ 千克的材料 $ x_i $ 可转化为 1 千克的材料 $ i $;同时,1 千克的材料 $ i $ 也可转化为 1 千克的材料 $ x_i $。BerSU 的化学处理设备仅允许进行这样的转化,即转化后所得材料的质量必须为整数千克。

对每个 $ i $(其中 $ 1 \leq i \leq n $),伊戈尔已知该实验需要 $ a_i $ 千克的材料 $ i $,而实验室当前拥有 $ b_i $ 千克的该材料。在经过若干次(可能为零次)材料转化之后,是否能够满足实验所需的所有材料?

输入格式

The first line contains one integer number n (1 ≤ n ≤ 105) — the number of materials discovered by Berland chemists.

The second line contains n integer numbers _b_1, _b_2... b__n (1 ≤ b__i ≤ 1012) — supplies of BerSU laboratory.

The third line contains n integer numbers _a_1, _a_2... a__n (1 ≤ a__i ≤ 1012) — the amounts required for the experiment.

Then n - 1 lines follow. j-th of them contains two numbers x__j + 1 and k__j + 1 that denote transformation of (j + 1)-th material (1 ≤ x__j + 1 ≤ j, 1 ≤ k__j + 1 ≤ 109).

第一行包含一个整数 $ n (( 1 \leq n \leq 10^5 $)—— Berland 化学家发现的材料种类数。

第二行包含 $ n $ 个整数 $ b_1, b_2, \dots, b_n (( 1 \leq b_i \leq 10^{12} $)—— BerSU 实验室现有的各种材料供应量。

第三行包含 $ n $ 个整数 $ a_1, a_2, \dots, a_n (( 1 \leq a_i \leq 10^{12} $)—— 实验所需的各种材料量。

随后是 $ n-1 $ 行。其中第 $ j $ 行包含两个数 $ x_{j+1} $ 和 $ k_{j+1} $,表示第 $ j+1 $ 种材料的转化关系($ 1 \leq x_{j+1} \leq j ,, 1 \leq k_{j+1} \leq 10^9 $)。

输出格式

Print YES if it is possible to conduct an experiment. Otherwise print NO.

如果可以进行实验,则输出 YES;否则输出 NO。

输入输出样例

  • 输入#1

    3
    1 2 3
    3 2 1
    1 1
    1 1

    输出#1

    YES
  • 输入#2

    3
    3 2 1
    1 2 3
    1 1
    1 2

    输出#2

    NO

输入解题思路,AI测评打分。不知道怎么写?

首页