int n, rt; int a[kMaxN], p[kMaxN], dep[kMaxN], res[kMaxN]; i64 r[kMaxN], f[kMaxN][25], g[kMaxN][25], dis[kMaxN][25]; std::vector<std::pair<int, i64>> G[kMaxN]; std::vector<int> T[kMaxN], vid[kMaxN]; std::vector<int> v1[kMaxN], v2[kMaxN];
template<class T> inlinevoidchkmax(T &x, T y){ x = (x > y ? x : y); } template<class T> inlinevoidchkmin(T &x, T y){ x = (x < y ? x : y); }
namespace Build { int rt, sz[kMaxN], mx[kMaxN]; bool del[kMaxN], vis[kMaxN];
voidgetsz(int u, int fa){ sz[u] = 1, mx[u] = 0; for (auto [v, w] : G[u]) { if (v == fa || del[v]) continue; getsz(v, u); sz[u] += sz[v], mx[u] = std::max(mx[u], sz[v]); } }
voidgetrt(int u, int fa, int tot){ mx[u] = std::max(mx[u], tot - sz[u]); if (!rt || mx[u] < mx[rt]) rt = u; for (auto [v, w] : G[u]) { if (v == fa || del[v]) continue; getrt(v, u, tot); } }
voiddfs2(int u, int fa, int rt, i64 d){ dis[u][dep[u] - dep[rt]] = d; for (auto [v, w] : G[u]) { if (v == fa || !vis[v]) continue; dfs2(v, u, rt, d + w); } }
voiddfs1(int u){ del[u] = 1; for (int i = u; i; i = p[i]) vid[i].emplace_back(u); for (auto [v, w] : G[u]) { if (del[v]) continue; rt = 0, getsz(v, 0), getrt(v, 0, sz[u]); T[u].emplace_back(rt), p[rt] = u, dep[rt] = dep[u] + 1; dfs1(rt); } for (auto x : vid[u]) vis[x] = 1; dfs2(u, 0, u, 0); for (auto x : vid[u]) vis[x] = 0; }
namespace Part2 { voiddfs(int t){ std::sort(v1[t].begin(), v1[t].end(), [&] (int i, int j) { return f[i][dep[i] - dep[t]] < f[j][dep[j] - dep[t]]; }); std::sort(v2[t].begin(), v2[t].end(), [&] (int i, int j) { return g[i][dep[i] - dep[t]] < g[j][dep[j] - dep[t]]; }); { // get f staticint mx[kMaxN]; for (auto i : vid[t]) mx[i] = -2e18; for (int i = 0, j = 0; i < v1[t].size(); ++i) { int x = v1[t][i]; for (; j < v2[t].size(); ++j) { int y = v2[t][j]; if (f[x][dep[x] - dep[t]] < g[y][dep[y] - dep[t]]) break; for (int pp = y; dep[pp] >= dep[t]; pp = p[pp]) chkmax(mx[pp], r[y] - dis[y][dep[y] - dep[pp]]); } std::vector<int> vec; for (int pp = x; dep[pp] > dep[t]; pp = p[pp]) { for (int qq = pp; dep[qq] >= dep[t]; qq = p[qq]) if (mx[qq] - dis[pp][dep[pp] - dep[qq]] >= 0) chkmax(f[x][dep[x] - dep[pp]], mx[qq] - dis[pp][dep[pp] - dep[qq]]); } } } // { // get g staticint mi[kMaxN]; for (auto i : vid[t]) mi[i] = 2e18; for (int i = (int)v2[t].size() - 1, j = (int)v1[t].size() - 1; ~i; --i) { int x = v2[t][i]; for (; ~j; --j) { int y = v1[t][j]; if (g[x][dep[x] - dep[t]] > f[y][dep[y] - dep[t]]) break; for (int pp = y; dep[pp] >= dep[t]; pp = p[pp]) chkmin(mi[pp], dis[y][dep[y] - dep[pp]]); } for (int pp = x; dep[pp] > dep[t]; pp = p[pp]) { for (int qq = pp; dep[qq] >= dep[t]; qq = p[qq]) chkmin(g[x][dep[x] - dep[pp]], mi[qq] + dis[pp][dep[pp] - dep[qq]]); } } } // { // get res int sum = 0; for (int i = 0, j = 0; i < v1[t].size(); ++i) { int x = v1[t][i]; for (; j < v2[t].size(); ++j) { int y = v2[t][j]; if (f[x][dep[x] - dep[t]] < g[y][dep[y] - dep[t]]) break; sum += a[y]; } res[x] += sum; } for (auto v : T[t]) { int sum = 0; std::vector<int> v1 = ::v1[v], v2 = ::v2[v]; std::sort(v1.begin(), v1.end(), [&] (int i, int j) { return f[i][dep[i] - dep[t]] < f[j][dep[j] - dep[t]]; }); std::sort(v2.begin(), v2.end(), [&] (int i, int j) { return g[i][dep[i] - dep[t]] < g[j][dep[j] - dep[t]]; }); for (int i = 0, j = 0; i < v1.size(); ++i) { int x = v1[i]; for (; j < v2.size(); ++j) { int y = v2[j]; if (f[x][dep[x] - dep[t]] < g[y][dep[y] - dep[t]]) break; sum += a[y]; } res[x] -= sum; } } } for (auto v : T[t]) dfs(v); }
voidsolve(){ dfs(rt); } } // namespace Part2
intLCA(int x, int y){ if (dep[x] < dep[y]) std::swap(x, y); for (; dep[x] > dep[y]; x = p[x]) {} for (; x != y; x = p[x], y = p[y]) {} return x; }
boolcheck(int x, int y){ int lca = LCA(x, y); return f[x][dep[x] - dep[lca]] >= g[y][dep[y] - dep[lca]]; }
voiddickdreamer(){ std::cin >> n; for (int i = 1; i <= n; ++i) std::cin >> a[i]; // for (int i = 1; i <= n; ++i) a[i] = 1; for (int i = 1; i <= n; ++i) std::cin >> r[i]; for (int i = 1; i < n; ++i) { int u, v, w; std::cin >> u >> v >> w; G[u].emplace_back(v, w), G[v].emplace_back(u, w); } Build::build(), Part1::solve(), Part2::solve(); for (int i = 1; i <= n; ++i) std::cout << res[i] << " \n"[i == n]; }