Mitya and Vasya are playing an interesting game. They have a rooted tree with n vertices, and the vertices are indexed from 1 to n. The root has index 1. Every other vertex i≥2 has its parent pi, and vertex i is called a child of vertex pi

.

There are some cookies in every vertex of the tree: there are xi

cookies in vertex i. It takes exactly ti time for Mitya to eat one cookie in vertex i. There is also a chip, which is initially located in the root of the tree, and it takes li time to move the chip along the edge connecting vertex i

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 T

. 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 v to its parent takes lv

time).

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

Input

The first line contains two integers n

and T — the number of vertices in the tree and the time he has to accomplish his task (2≤n≤105; 1≤T≤1018

).

The second line contains n

integers x1, x2, ..., xn — number of cookies located in the corresponding vertex (1≤xi≤106). The third line contains n integers t1, t2, ..., tn — how much time it takes Mitya to eat one cookie in vertex i (1≤ti≤106

).

Each of the following n−1

lines describe the tree. For every i from 2 to n, the corresponding line contains two integers pi and li, where pi denotes the parent of vertex i and li denotes the time it takes Mitya to move the chip along the edge from vertex i to its parent (1≤pi<i, 0≤li≤109

).

Output

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

Examples

Input
5 26
1 5 1 7 7
1 3 2 2 2
1 1
1 1
2 0
2 0
Output
11
Input
3 179
2 2 1
6 6 6
1 3
2 3
Output
4

Note

In the first example test case, Mitya can start by moving the chip to vertex 2

. In this case no matter how Vasya plays, Mitya is able to eat at least 11

cookies. Below you can find the detailed description of the moves:

  1. Mitya moves chip to vertex 2
  • .
  • Vasya removes edge to vertex 4
  • .
  • Mitya moves chip to vertex 5
  • .
  • Since vertex 5
  • has no children, Vasya does not remove any edges.
  • Mitya stops the game and moves the chip towards the root, eating cookies along the way (7

in vertex 5, 3 in vertex 2, 1 in vertex 1

  1. ).

Mitya spend 1+0

time to go down, 0+1 to go up, 7⋅2 to eat 7 cookies in vertex 5, 3⋅3 to eat 3 cookies in vertex 2, 1⋅1 to eat 1 cookie in vertex 1. Total time is 1+0+0+1+7⋅2+3⋅3+1⋅1=26.

题意:给定带权树,每个节点有一些a[i]个吃的,每个需要时间t[i],现在让你找一条路线,可以吃到最多的吃的。

思路:对于当前路线1-u,减去路上花费的时间,剩下的时间T-time用来吃东西,我们肯定是按照时间t排序后从小到大吃,这里不嫩想到用二分+前缀和来做。

线段树可以很好的解决这个问题。 (参考了WXK的代码,非常的漂亮,orz

题目还有博弈的限制,其实就是除了1号节点找第二大即可。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
ll sum[maxn],num[maxn],Mx[maxn][],ans[maxn];int t[maxn],x[maxn];
int Laxt[maxn],Next[maxn],To[maxn],Len[maxn<<],cnt;
void add(int u,int v,int l){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; Len[cnt]=l;
}
void update(int Now,int L,int R,int pos,int val)
{
sum[Now]+=(ll)val*pos; num[Now]+=val;
if(L==R) return ;int Mid=(L+R)>>;
if(pos<=Mid) update(Now<<,L,Mid,pos,val);
else update(Now<<|,Mid+,R,pos,val);
}
ll query(int Now,int L,int R,ll T)
{
if(sum[Now]<=T) return num[Now];
if(L==R) return T/L;
int Mid=(L+R)>>;
if(sum[Now<<]<=T) return num[Now<<]+query(Now<<|,Mid+,R,T-sum[Now<<]);
return query(Now<<,L,Mid,T);
}
void dfs(int u,ll T)
{
if(T<=) return ;
update(,,,t[u],x[u]);
ans[u]=query(,,,T);
for(int i=Laxt[u];i;i=Next[i]){
dfs(To[i],T-Len[i]*);
ll z=ans[To[i]];
if(z>Mx[u][]) swap(z,Mx[u][]);
if(z>Mx[u][]) swap(z,Mx[u][]);
}
if(u!=) ans[u]=max(Mx[u][],ans[u]);
else ans[u]=max(Mx[u][],ans[u]);
update(,,,t[u],-x[u]);
}
int main()
{
int N,p,l; ll T;
scanf("%d%lld",&N,&T);
rep(i,,N) scanf("%d",&x[i]);
rep(i,,N) scanf("%d",&t[i]);
rep(i,,N){
scanf("%d%d",&p,&l);
add(p,i,l);
}
dfs(,T);
printf("%lld\n",ans[]);
return ;
}

CodeForces - 1099F:Cookies (线段树)的更多相关文章

  1. CodeForces 1099F - Cookies - [DFS+博弈+线段树]

    题目链接:https://codeforces.com/problemset/problem/1099/F Mitya and Vasya are playing an interesting gam ...

  2. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  3. Codeforces 787D. Legacy 线段树建模+最短路

    D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  4. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

  5. Sereja and Brackets CodeForces - 380C (线段树+分治思路)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  6. CodeForces 91B Queue (线段树,区间最值)

    http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...

  7. Codeforces 343D WaterTree - 线段树, DFS序

    Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...

  8. codeforces 787D - Legacy 线段树优化建图,最短路

    题意: 有n个点,q个询问, 每次询问有一种操作. 操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w: 操作2:[l,r]→u的距离为w 操作3:u到v的距离为w 最 ...

  9. Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)

    题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...

  10. Codeforces 765F Souvenirs 线段树 + 主席树 (看题解)

    Souvenirs 我们将询问离线, 我们从左往右加元素, 如果当前的位置为 i ,用一棵线段树保存区间[x, i]的答案, 每次更新完, 遍历R位于 i 的询问更新答案. 我们先考虑最暴力的做法, ...

随机推荐

  1. Linux登录超时自动退出处理办法

    出于安全方面的考虑,机器常要求配置一个登录时间期限,当闲置超过这一期限就自动退出:但在某些场合我们需要时不时地就使用机器,如果每次都要重新ssh登录那是非常麻烦的 方法一:让当前会话一直处于工作状态 ...

  2. Nginx隐藏版本号操作

    1.定位当前nginx所使用的配置文件 ps -ef |grep nginx 如果-c参数.则-c文件即为nginx当前所用配置文件,如上图中配置文件即为/usr/local/nginx/conf/n ...

  3. valgrind 工具介绍和简单的使用

    最近老是遇上各种奇奇怪怪的core dump,不太会分析的情况下看到了这款工具.在这记录分享下. Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgri ...

  4. 自定义xadmin后台首页

    登陆xadmin后台,首页默认是空白,可以自己添加小组件,xadmin一切都是那么美好,但是添加小组件遇到了个大坑,快整了2个礼拜,最终实现想要的界面.初始的页面如图: 本机后台显示这个页面正常,do ...

  5. 我眼中的Linux系统和红帽RHCE认证

    牛顿曾经说过“我不知道在别人看来,我是什么样的人:但在我自己看来,我不过就象是一个在海滨玩耍的小孩,为不时发现比寻常更为光滑的一块卵石或比寻常更为美丽的一片贝壳而沾沾自喜,而对于展现在我面前的浩瀚的真 ...

  6. jquery 操作table样式拖动参考

    参考: http://blog.csdn.net/kdiller/article/details/6059727 http://www.jb51.net/article/59795.htm

  7. Python数据分析中对重复值、缺失值、空格的处理

    对重复值的处理 把数据结构中,行相同的数据只保留一行 函数语法: drop_duplicates() from pandas import read_csv df = read_csv(文件位置) n ...

  8. 1449 - The user specified as a definer('xxx'@'%') does not exist

    指定的用户不存在,创建相应的账户即可,注意主机那里填的内容,我的这个是@'%'所以不用填任何内容.

  9. Java语法基础学习DayThree

    一.流程控制语句补充 1.switch语句 格式: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体 ...

  10. webpack进阶构建项目(一):1.理解webpack加载器

    1.理解webpack加载器 webpack的设计理念,所有资源都是“模块”,webpack内部实现了一套资源加载机制,这与Requirejs.Sea.js.Browserify等实现有所不同. We ...