#include "bits/stdc++.h" using namespace std; #define DEBUG(x) cout<<#x<<": "< #define vl vector #define vii vector< vector > #define vll vector< vector > #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair #define fi first #define se second #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() const int inf = 1000000001; const ll INF = 2e18 * 2; #define MOD 1000000007 #define mod 1000000009 #define pi 3.14159265358979323846 #define Sp(p) cout<= mod ? a.n - a.Mod : a.n); } Modint operator-(Modint a, Modint b) { return Modint((a.n -= b.n) < 0 ? a.n + a.Mod : a.n); } Modint operator*(Modint a, Modint b) { return Modint(1LL * a.n * b.n % a.Mod); } Modint &operator+=(Modint &a, Modint b) { return a = a + b; } Modint &operator-=(Modint &a, Modint b) { return a = a - b; } Modint &operator*=(Modint &a, Modint b) { return a = a * b; } struct HLDecomposition { int n, pos; vector > G; vector vid, head, sub, hvy, par, dep, inv, type; HLDecomposition() {} HLDecomposition(int sz) : n(sz), pos(0), G(n), vid(n, -1), head(n), sub(n, 1), hvy(n, -1), par(n), dep(n), inv(n), type(n) {} void add_edge(int u, int v) { G[u].push_back(v); G[v].push_back(u); } void build(vector rs = { 0 }) { int c = 0; for (int r : rs) { dfs(r); bfs(r, c++); } } void dfs(int rt) { using T = pair; stack st; par[rt] = -1; dep[rt] = 0; st.emplace(rt, 0); while (!st.empty()) { int v = st.top().first; int &i = st.top().second; if (i<(int)G[v].size()) { int u = G[v][i++]; if (u == par[v]) continue; par[u] = v; dep[u] = dep[v] + 1; st.emplace(u, 0); } else { st.pop(); int res = 0; for (int u : G[v]) { if (u == par[v]) continue; sub[v] += sub[u]; if (res q({ r }); while (!q.empty()) { int h = q.front(); q.pop(); for (int i = h; i != -1; i = hvy[i]) { type[i] = c; vid[i] = k++; inv[vid[i]] = i; head[i] = h; for (int j : G[i]) if (j != par[i] && j != hvy[i]) q.push(j); } } } // for_each(vertex) // [l,r] <- attention!! void for_each(int u, int v, const function& f) { while (1) { if (vid[u]>vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]); if (head[u] != head[v]) v = par[head[v]]; else break; } } // for_each(edge) // [l,r] <- attention!! // 値が辺に乗っているときは、頂点から遠いほう(すなわちvidが大きいほう)の // 頂点に値を移す。yuki650 void for_each_edge(int u, int v, const function& f) { while (1) { if (vid[u]>vid[v]) swap(u, v); if (head[u] != head[v]) { f(vid[head[v]], vid[v]); v = par[head[v]]; } else { if (u != v) f(vid[u] + 1, vid[v]); break; } } } int lca(int u, int v) { while (1) { if (vid[u]>vid[v]) swap(u, v); if (head[u] == head[v]) return u; v = par[head[v]]; } } int distance(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } }; #define N 100010 vii G(N); vl u(N); vl sum(N); void make_sum(int parent, int now) { if (now == 0) { sum[now] = u[now]; } else { sum[now] = sum[parent] + u[now]; } rep(i, G[now].size()) { if (G[now][i] == parent) { continue; } make_sum(now, G[now][i]); } } int main() { int n, i, j; cin >> n; HLDecomposition hld(n); vi a(n - 1), b(n - 1); rep(i, n - 1) { cin >> a[i] >> b[i]; hld.add_edge(a[i], b[i]); G[a[i]].push_back(b[i]); G[b[i]].push_back(a[i]); } hld.build(); u.resize(n); rep(i, n) { cin >> u[i]; } //DEBUG_VEC(u); sum.resize(n); make_sum(-1, 0); //DEBUG_VEC(sum); int m; cin >> m; ll ans = 0; while (m--) { int a, b, c; cin >> a >> b >> c; int lca = hld.lca(a, b); int lcap = hld.par[lca]; ll temp = sum[a] + sum[b] - sum[lca]; if (lcap != -1) { temp -= sum[lcap]; } ans += temp * c; } cout << ans << endl; }