using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Magatro { static int N, M; static List[] Root; static int[] U; static int[] toZero; static List[] R; static void Main() { Read(); int cnt = 0; int m = int.Parse(Console.ReadLine()); for(int i = 0; i < m; i++) { int[] abc = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); int c = calc(abc[0], abc[1]) * abc[2]; // Console.WriteLine(c); cnt += c; } Console.WriteLine(cnt); } static int calc(int s, int g) { int sc = R[s].Count, gc = R[g].Count ; int cvill = 0; for(int i=0; ; i++) { if (i[N]; for (int i = 0; i < N; i++) { Root[i] = new List(); } for (int i = 0; i < N - 1; i++) { string[] s = Console.ReadLine().Split(' '); int a = int.Parse(s[0]); int b = int.Parse(s[1]); Root[a].Add(b); Root[b].Add(a); } U = new int[N]; R = new List[N]; for (int i = 0; i < N; i++) { R[i] = new List(); U[i] = int.Parse(Console.ReadLine()); } toZero = new int[N]; bool[] ed = new bool[N]; Queue q = new Queue(); q.Enqueue(0); toZero[0] = U[0]; ed[0] = true; R[0].Add(0); while (q.Count > 0) { int w = q.Dequeue(); for(int i = 0; i < Root[w].Count; i++) { int e = Root[w][i]; if (!ed[e]) { q.Enqueue(e); ed[e] = true; toZero[e] = toZero[w] + U[e]; R[e] = R[w].ToList(); R[e].Add(e); } } } } }