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 void Main() { Read(); int m = int.Parse(Console.ReadLine()); long cnt = 0; for(int i = 0; i < m; i++) { int[] abc = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); cnt += calc(abc[0], abc[1]) * abc[2]; } Console.WriteLine(cnt); } static int calc(int s, int g) { int[] miti = new int[N]; bool[] ed = new bool[N]; miti[s] = U[s]; ed[s] = true; Queue q = new Queue(); q.Enqueue(s); while (q.Count > 0) { int i = q.Dequeue(); for (int j = 0; j < Root[i].Count; j++) { int w = Root[i][j]; if (!ed[w]) { q.Enqueue(w); ed[w] = true; miti[w] = miti[i] + U[w]; if (w == g) { return miti[g]; } } } } return -1; } static void Read() { N = int.Parse(Console.ReadLine()); Root = new List[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]; for(int i = 0; i < N; i++) { U[i] = int.Parse(Console.ReadLine()); } } }