func_code_string: #include <bits/stdc++.h>   using namespace std;   int gi() {     int x = 0, w = 1;     char ch = getchar();     while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar();     if (ch ==  - ) w = 0, ch = getchar();     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     return w ? x : -x;   }   const int N = 3e5 + 5;   int n, m, to[N << 1], nxt[N << 1], head[N], cnt, ls[N], rs[N], dis[N], rt[N],       top[N], vis[N], key[N], tag[N];   long long ans;   void cover(int x, int v) {     if (!x) return;     key[x] += v;     tag[x] += v;   }   void down(int x) {     if (!tag[x]) return;     cover(ls[x], tag[x]);     cover(rs[x], tag[x]);     tag[x] = 0;   }   int merge(int x, int y) {     if (!x || !y) return x | y;     if (key[x] > key[y]) swap(x, y);     down(x);     rs[x] = merge(rs[x], y);     if (dis[ls[x]] < dis[rs[x]]) swap(ls[x], rs[x]);     dis[x] = dis[rs[x]] + 1;     return x;   }   void dfs(int u, int f) {     for (int e = head[u]; e; e = nxt[e])       if (to[e] ^ f) dfs(to[e], u), rt[u] = merge(rt[u], rt[to[e]]);     if (u ^ 1) {       vis[u] = 1;       while (vis[top[rt[u]]]) rt[u] = merge(ls[rt[u]], rs[rt[u]]);       if (!rt[u]) puts( -1 ), exit(0);       ans += key[rt[u]];       cover(rt[u], -key[rt[u]]);     }   }   int main() {     n = gi();     m = gi();     for (int i = 1; i < n; ++i) {       int u = gi(), v = gi();       to[++cnt] = v;       nxt[cnt] = head[u];       head[u] = cnt;       to[++cnt] = u;       nxt[cnt] = head[v];       head[v] = cnt;     }     for (int i = 1; i <= m; ++i) {       int u = gi();       top[i] = gi();       key[i] = gi();       rt[u] = merge(rt[u], i);     }     dfs(1, 0);     printf( %lld n , ans);     return 0;   }   