func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5 + 100;   const long long INF = 1e13;   int u, v, c, b, laz[MAXN], C[MAXN], n, m, st[MAXN];   bool vis[MAXN];   multiset<pair<int, int> > Q[MAXN];   multiset<pair<int, int> >::iterator it;   vector<pair<int, int> > W[MAXN];   vector<int> G[MAXN];   pair<int, int> k;   void merg(int v, int u) {     if (Q[v].size() >= Q[u].size()) {       while (!(Q[u].empty())) {         it = Q[u].begin();         int i = (*(it)).second;         C[i] += laz[u] - laz[v];         c = (*it).first;         Q[v].insert({c + laz[u] - laz[v], i});         Q[u].erase(it);       }       return;     }     merg(u, v);     Q[v].swap(Q[u]);     laz[v] = laz[u];     return;   }   long long ans = 0;   int TIM = 0;   bool dfs(int v, int f = -1) {     st[v] = TIM;     TIM++;     for (auto u : G[v]) {       if (u == f) {         continue;       }       bool r = dfs(u, v);       if (!r) {         return 0;       }       merg(v, u);     }     for (auto k : W[v]) {       if (st[k.first] < st[v]) {         C[k.second] -= laz[v];         Q[v].insert({C[k.second], k.second});       } else {         Q[v].erase({C[k.second], k.second});       }     }     if (!v) {       return 1;     }     if (!(Q[v].size())) {       return 0;     }     int h = (*(Q[v].begin())).first;     ans += (h + laz[v]);     laz[v] -= (h + laz[v]);     return 1;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n - 1; i++) {       cin >> u >> v;       u--;       v--;       G[u].push_back(v);       G[v].push_back(u);     }     for (int i = 0; i < m; i++) {       cin >> u >> v >> C[i];       u--;       v--;       W[u].push_back({v, i});       W[v].push_back({u, i});     }     bool r = dfs(0);     if (!r) {       cout << -1 << endl;       return 0;     }     cout << ans << endl;   }   