#include using namespace std; using ll = long long; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))< ostream& operator<<(ostream& o, const pair &p){o<<"("< ostream& operator<<(ostream& o, const vector &v){o<<"[";for(T t:v){o< G[MAX_V]; //根のノード番号 int root = 0; //親を2^k回辿って到達するノード(根を通り過ぎる場合,-1) int parent[MAX_LOG_V][MAX_V]; //根からの深さ int depth[MAX_V]; //現在vに注目、親はp、深さd void lca_dfs(int v, int p, int d){ parent[0][v]=p; depth[v]=d; rep(i,G[v].size()){ if(G[v][i]!=p){ //親でなければ子 lca_dfs(G[v][i], v, d+1); } } } //初期化 void lca_init(int V){ //parent[0]とdepthの初期化 lca_dfs(root, -1, 0); //parentの初期化 for(int k=0; k+1 depth[v]) swap(u,v); for(int k=0; k>k & 1) v = parent[k][v]; } if(u==v) return u; //二分探索でLCAを求める for(int k=MAX_LOG_V-1; k>=0; --k){ if(parent[k][u] != parent[k][v]){ u=parent[k][u]; v=parent[k][v]; } } return parent[0][u]; } int par[MAX_V]={}; ll dp[MAX_V]={}; void dfs(int cur){ for(int nx:G[cur])if(nx != par[cur]){ par[nx] = cur; dp[nx] += dp[cur]; dfs(nx); } } int main(){ int n; scanf(" %d", &n); rep(i,n-1){ int a,b; scanf(" %d %d", &a, &b); G[a].pb(b); G[b].pb(a); } lca_init(n); vector u(n); rep(i,n){ scanf(" %d", &u[i]); dp[i] += u[i]; } par[0] = -1; dfs(0); int m; scanf(" %d", &m); ll ans = 0; while(m--){ int a,b,c; scanf(" %d %d %d", &a, &b, &c); int x = lca(a,b); ans += (dp[a]+dp[b]-2*dp[x]+u[x])*c; } printf("%lld\n", ans); return 0; }