using System; using System.Linq; using System.Linq.Expressions; using System.Collections.Generic; using Debug = System.Diagnostics.Debug; using StringBuilder = System.Text.StringBuilder; //using System.Numerics; using Number = System.Int64; namespace Program { public class Solver { public void Solve() { var n = sc.Integer(); var G = Enumerate(n, x => new List()); var tree = new TreeGraph(n); for (int i = 0; i < n - 1; i++) { var f = sc.Integer(); var t = sc.Integer(); G[f].Add(t); G[t].Add(f); tree.AddEdge(f, t); } tree.Build(0); var v = sc.Long(n); var sum = v.ToArray(); { var q = new Queue(); var used = new bool[n]; q.Enqueue(0); while (q.Count > 0) { var p = q.Dequeue(); used[p] = true; foreach (var to in G[p]) { if (used[to]) continue; q.Enqueue(to); sum[to] += sum[p]; } } } long ans = 0; var m = sc.Integer(); for (int i = 0; i < m; i++) { var f = sc.Integer(); var t = sc.Integer(); var c = sc.Integer(); var lca = tree.GetLCA(f, t); ans += c * (sum[f] + sum[t] - 2 * sum[lca] + v[lca]); } IO.Printer.Out.WriteLine(ans); } public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput()); static T[] Enumerate(int n, Func f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; } static public void Swap(ref T a, ref T b) { var tmp = a; a = b; b = tmp; } } } #region main static class Ex { static public string AsString(this IEnumerable ie) { return new string(System.Linq.Enumerable.ToArray(ie)); } static public string AsJoinedString(this IEnumerable ie, string st = " ") { return string.Join(st, ie); } static public void Main() { var solver = new Program.Solver(); solver.Solve(); Program.IO.Printer.Out.Flush(); } } #endregion #region Ex namespace Program.IO { using System.IO; using System.Text; using System.Globalization; public class Printer: StreamWriter { static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; } public static Printer Out { get; set; } public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } } public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { } public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { } public void Write(string format, T[] source) { base.Write(format, source.OfType().ToArray()); } public void WriteLine(string format, T[] source) { base.WriteLine(format, source.OfType().ToArray()); } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } public readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) return 0; if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; } public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } public string ScanLine() { var sb = new StringBuilder(); for (var b = Char(); b != '\n'; b = (char)read()) if (b == 0) break; else if (b != '\r') sb.Append(b); return sb.ToString(); } public long Long() { if (isEof) return long.MinValue; long ret = 0; byte b = 0; var ng = false; do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b)); if (b == 0) return long.MinValue; if (b == '-') { ng = true; b = read(); } for (; true; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; else ret = ret * 10 + b - '0'; } } public int Integer() { return (isEof) ? int.MinValue : (int)Long(); } public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; } private T[] enumerate(int n, Func f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(); return a; } public char[] Char(int n) { return enumerate(n, Char); } public string[] Scan(int n) { return enumerate(n, Scan); } public double[] Double(int n) { return enumerate(n, Double); } public int[] Integer(int n) { return enumerate(n, Integer); } public long[] Long(int n) { return enumerate(n, Long); } } } #endregion #region Treegraph public class TreeGraph { public int n; public List[] G; public int[] par; public int[] d; public int[] ord, ordl, ordr; public TreeGraph(int N) { n = N; G = new List[N]; for (int i = 0; i < N; i++) G[i] = new List(); par = new int[N]; d = new int[N]; ord = new int[2 * N - 1]; ordl = new int[N]; ordr = new int[N]; } public void AddEdge(int f, int t) { G[f].Add(new Edge(f, t)); G[t].Add(new Edge(t, f)); } void Walk(int root) { var stack = new Stack(); var iter = new int[n]; stack.Push(root); par[root] = -1; d[root] = 0; var ptr = 0; while (stack.Any()) { var p = stack.Peek(); ord[ptr] = p; ptr++; if (iter[p] < G[p].Count && G[p][iter[p]].to == par[p]) iter[p]++; if (iter[p] == G[p].Count) { stack.Pop(); ordr[p] = ptr; } else { var to = G[p][iter[p]].to; iter[p]++; stack.Push(to); par[to] = p; d[to] = d[p] + 1; ordl[to] = ptr; } } } sparseTable rmq; void BuildLCA() { var dep = new int[2 * n - 1]; for (int i = 0; i < 2 * n - 1; i++) dep[i] = d[ord[i]]; rmq = new sparseTable(dep); } public void Build(int root) { Walk(root); BuildLCA(); } public int GetLCA(int u, int v) { //ordl: u ordl[v]) { var tmp = u; u = v; v = tmp; } return ord[rmq.Query(ordl[u], ordl[v])]; } /// /// 木を縮約してLCAを作って返す /// static public TreeGraph Compress(int[] v, TreeGraph G, List ord = null) { if (ord == null) ord = new List(); for (int i = 0; i < v.Length; i++) ord.Add(G.ordl[v[i]]); ord.Sort(); for (int i = 0; i < v.Length - 1; i++) ord.Add(G.ordl[G.GetLCA(G.ord[ord[i]], G.ord[ord[i + 1]])]); { var next = ord.Distinct().ToArray(); ord.Clear(); foreach (var x in next) ord.Add(x); } ord.Sort(); var n = ord.Count; var ret = new TreeGraph(n); var stack = new Stack(); stack.Push(0); var ptr = 1; while (ptr < n) { var cur = stack.Peek(); var a = G.ord[ord[cur]]; if (G.ordr[a] > ord[ptr]) { ret.AddEdge(cur, ptr); stack.Push(ptr++); } else stack.Pop(); } return ret; } #region SparseTable class sparseTable { int n; int[] A; int[] id; public sparseTable(int[] a) { var k = 1; n = a.Length; for (int i = 1; i < n; i <<= 1) k++; A = a; id = new int[n * k]; for (int i = 0; i < n; i++) id[i] = i; var d = 0; for (int i = 1; i < n; i <<= 1) { for (int j = 0; j < n; j++) id[d + n + j] = id[d + j]; d += n; for (int j = 0; j < n - i; j++) if (A[id[d + j]] > A[id[d + j + i]]) id[d + j] = id[d + j + i]; } } /// /// value of [l,r) /// public int Query(int l, int r) { int z = r - l, k = 0, e = 1, s; s = ((z & 0xffff0000) != 0 ? 1 : 0) << 4; z >>= s; e <<= s; k |= s; s = ((z & 0x0000ff00) != 0 ? 1 : 0) << 3; z >>= s; e <<= s; k |= s; s = ((z & 0x000000f0) != 0 ? 1 : 0) << 2; z >>= s; e <<= s; k |= s; s = ((z & 0x0000000c) != 0 ? 1 : 0) << 1; z >>= s; e <<= s; k |= s; s = ((z & 0x00000002) != 0 ? 1 : 0) << 0; z >>= s; e <<= s; k |= s; var x = id[l + n * k]; var y = id[r - e + 1 + (n * k)]; if (A[x] <= A[y]) return x; else return y; } } #endregion } #endregion #region TreeEdge public struct Edge { public int from, to; public Edge(int f, int t) { from = f; to = t; } public override string ToString() { return string.Format("{0}->{1}", from, to); } } #endregion