voiddfs1(Node *u, longlong k){ if (!u->adj.empty()) { int size = u->adj.size(); ans += k / size * size * u->s; for (Node *v : u->adj) dfs1(v, k / size); u->remain = k % size; } else ans += k * u->s; }
voiddfs2(Node *u){ if (u->deg == 0) u->q.push(u->s); for (Node *v : u->adj) { dfs2(v); if (!v->q.empty()) u->q.push(v->q.top() + u->s); while (!v->q.empty()) v->q.pop(); } while (u->remain--) { ans += u->q.top(); u->q.pop(); } }
voidsolve(){ int n, k; scanf("%d %d", &n, &k); std::vector<Node> nodes(n); for (int i = 1; i < n; i++) { int p; scanf("%d", &p); addEdge(&nodes[--p], &nodes[i]); } for (int i = 0; i < n; i++) scanf("%lld", &nodes[i].s);