## 思路

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll N = 1e6 + 10;

struct Node {
ll v, w;
} p[N];
vector<Node> g[N];
ll n, T;

#define LD (t << 1)
#define RD (t << 1 | 1)

ll val[N << 2], siz[N << 2];

void insert(ll t, ll l, ll r, ll pos, ll cursiz) {
val[t] += pos * cursiz;
siz[t] += cursiz;
if (l == r) return;
ll mid = (l + r) >> 1;
if (pos <= mid) insert(LD, l, mid, pos, cursiz);
else insert(RD, mid + 1, r, pos, cursiz);
}

ll query(ll t, ll l, ll r, ll vl) {
if (l == r) return min(siz[t], vl / l);
ll mid = (l + r) >> 1;
if (val[LD] <= vl) return siz[LD] + query(RD, mid + 1, r, vl - val[LD]);
else return query(LD, l, mid, vl);
}

ll dfs(ll u, ll lef) {
insert(1, 1, 1e6, p[u].w, p[u].v);
ll res = query(1, 1, 1e6, lef);
ll f1 = 0, f2 = 0;
for (auto now : g[u]) {
ll v = now.v;
if (lef <= now.w * 2) continue;
ll cur = dfs(v, lef - now.w * 2);
if (cur > f1) f2 = f1, f1 = cur;
else if (cur > f2) f2 = cur;
}
insert(1, 1, 1e6, p[u].w, -p[u].v);
if (u == 1) return max(res, f1);
else return max(res, f2);
}

int main() {
scanf("%lld %lld", &n, &T);
for (ll i = 1; i <= n; i++) scanf("%lld", &p[i].v);
for (ll i = 1; i <= n; i++) scanf("%lld", &p[i].w);
for (ll i = 2; i <= n; i++) {
ll u, w; scanf("%lld %lld", &u, &w);
g[u].push_back((Node) {i, w});
}
printf("%lld", dfs(1, T));
return 0;
}
posted @ 2019-01-06 21:13 Dream_maker_yk 阅读(...) 评论(...) 编辑 收藏