CF BUDDY
← Problems·

1099F · Cookies

2400 · binary search, data structures, dfs and similar

Problem: Mitya and Vasya are playing an interesting game. They have a rooted tree with nn vertices, and the vertices are indexed from 11 to nn. The root has index 11. Every other vertex i2i \ge 2 has its parent pip_i, and vertex ii is called a child of vertex pip_i.

There are some cookies in every vertex of the tree: there are xix_i cookies in vertex ii. It takes exactly tit_i time for Mitya to eat one cookie in vertex ii. There is also a chip, which is initially located in the root of the tree, and it takes lil_i time to move the chip along the edge connecting vertex ii with its parent.

Mitya and Vasya take turns playing, Mitya goes first.

  • Mitya moves the chip from the vertex, where the chip is located, to one of its children.
  • Vasya can remove an edge from the vertex, where the chip is located, to one of its children. Vasya can also decide to skip his turn.

Mitya can stop the game at any his turn. Once he stops the game, he moves the chip up to the root, eating some cookies along his way. Mitya can decide how many cookies he would like to eat in every vertex on his way. The total time spent on descend, ascend and eating cookies should not exceed TT. Please note that in the end of the game the chip is always located in the root of the tree: Mitya can not leave the chip in any other vertex, even if he has already eaten enough cookies — he must move the chip back to the root (and every move from vertex vv to its parent takes lvl_v time).

Find out what is the maximum number of cookies Mitya can eat, regardless of Vasya's actions.

Input Format: The first line contains two integers nn and TT — the number of vertices in the tree and the time he has to accomplish his task (2n1052\le n \le 10^5; 1T10181\le T\le10^{18}).

The second line contains nn integers x1x_1, x2x_2, ..., xnx_n — number of cookies located in the corresponding vertex (1xi1061\le x_i\le10^6). The third line contains nn integers t1t_1, t2t_2, ..., tnt_n — how much time it takes Mitya to eat one cookie in vertex ii (1ti1061\le t_i\le10^6).

Each of the following n1n - 1 lines describe the tree. For every ii from 22 to nn, the corresponding line contains two integers pip_i and lil_i, where pip_i denotes the parent of vertex ii and lil_i denotes the time it takes Mitya to move the chip along the edge from vertex ii to its parent (1pi<i1\le p_i < i, 0li1090\le l_i \le 10^9).

Output Format: Output a single integer — maximum number of cookies Mitya can eat.

Note: In the first example test case, Mitya can start by moving the chip to vertex 22. In this case no matter how Vasya plays, Mitya is able to eat at least 1111 cookies. Below you can find the detailed description of the moves:

  1. Mitya moves chip to vertex 22.
  2. Vasya removes edge to vertex 44.
  3. Mitya moves chip to vertex 55.
  4. Since vertex 55 has no children, Vasya does not remove any edges.
  5. Mitya stops the game and moves the chip towards the root, eating cookies along the way (77 in vertex 55, 33 in vertex 22, 11 in vertex 11).

Mitya spend 1+01+0 time to go down, 0+10+1 to go up, 727\cdot 2 to eat 77 cookies in vertex 5, 333\cdot 3 to eat 33 cookies in vertex 2, 111\cdot 1 to eat 11 cookie in vertex 1. Total time is 1+0+0+1+72+33+11=261+0+0+1+7\cdot 2+3\cdot 3+1\cdot 1=26.

Sample Cases

Case 1

Input

5 26
1 5 1 7 7
1 3 2 2 2
1 1
1 1
2 0
2 0

Output

11

Case 2

Input

3 179
2 2 1
6 6 6
1 3
2 3

Output

4

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback