#include #include #include using namespace std; int main(){ int n; cin >> n; vector> graph(n); for(int i=1; i> a >> b; graph[a].push_back(b); graph[b].push_back(a); } vector u(n); for(int &i: u) cin >> i; int maxlog = 0; while((1<> parent(n, vector(maxlog)); vector cost(u), depth(n); queue que; que.push(0); while(!que.empty()){ int i = que.front(); que.pop(); for(int j: graph[i]){ if(j == parent[i][0]) continue; parent[j][0] = i; depth[j] = depth[i] + 1; cost[j] += cost[i]; que.push(j); } } for(int i=1; i> m; long long res = 0; for(int i=0; i> a >> b >> c; res += (cost[a] + cost[b]) * c; if(depth[a] > depth[b]) swap(a, b); for(int i=maxlog-1; i>=0; --i) if(depth[parent[b][i]] >= depth[a]) b = parent[b][i]; if(a != b){ for(int i=maxlog-1; i>=0; --i) if(parent[a][i] != parent[b][i]){ a = parent[a][i]; b = parent[b][i]; } a = parent[a][0]; } res -= (cost[a]*2 - u[a]) * c; } cout << res << endl; return 0; }