#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 3e5 + 10;
int st[N], ed[N];
vector<pair<int, long long> > fix[N];
vector<int> adj[N], cancel[N];
long long T[N << 2], lazy[N << 2];
int n, m, cnt;
void push(int s, int l, int r) {
if (!lazy[s]) return;
T[s] += lazy[s];
if (l != r) {
lazy[s << 1] += lazy[s];
lazy[s << 1 | 1] += lazy[s];
}
lazy[s] = 0;
}
void up(int s, int l, int r, int from, int to, long long val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
lazy[s] += val;
push(s, l, r);
return;
}
int mid = l + r >> 1;
up(s << 1, l, mid, from, to, val);
up(s << 1 | 1, mid + 1, r, from, to, val);
T[s] = min(T[s << 1], T[s << 1 | 1]);
}
long long get(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from) return 1e15;
if (from <= l && r <= to) return T[s];
int mid = l + r >> 1;
return min(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to));
}
void dfs(int u, int p = 0) {
st[u] = 1e9;
long long sum = 0;
for(int &v : adj[u]) if (v != p) {
dfs(v, u);
st[u] = min(st[u], st[v]);
sum += get(1, 1, m, st[v], ed[v]);
if (sum >= 1e18) {
cout << -1;
exit(0);
}
}
for(int &v : adj[u]) if (v != p) {
long long ex = get(1, 1, m, st[v], ed[v]);
up(1, 1, m, st[v], ed[v], sum - ex);
}
for(auto &[highest, cost] : fix[u]) {
++cnt; if (st[u] == 1e9) st[u] = cnt;
cancel[highest].push_back(cnt);
up(1, 1, m, cnt, cnt, cost + sum);
}
ed[u] = cnt;
if (u == 1) return;
for(auto &idx : cancel[u]) up(1, 1, m, idx, idx, 1e15);
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> m;
if (n == 1) return cout << 0, 0;
for(int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1; i <= m; i++) {
int u, v, c; cin >> u >> v >> c;
fix[u].emplace_back(v, c);
}
dfs(1, 0);
long long ans = get(1, 1, m, st[1], ed[1]);
if (ans >= 1e15) ans = -1;
cout << ans;
}
1733A - Consecutive Sum | 1733B - Rule of League |
1733C - Parity Shuffle Sorting | 1264A - Beautiful Regional Contest |
1695A - Subrectangle Guess | 467B - Fedor and New Game |
252C - Points on Line | 735C - Tennis Championship |
992A - Nastya and an Array | 554A - Kyoya and Photobooks |
79B - Colorful Field | 265B - Roadside Trees (Simplified Edition) |
1362C - Johnny and Another Rating Drop | 1214C - Bad Sequence |
1091B - New Year and the Treasure Geolocation | 244A - Dividing Orange |
1061C - Multiplicity | 1312A - Two Regular Polygons |
801A - Vicious Keyboard | 510B - Fox And Two Dots |
616D - Longest k-Good Segment | 1604A - Era |
555B - Case of Fugitive | 551A - GukiZ and Contest |
1399F - Yet Another Segments Subset | 1371C - A Cookie for You |
430B - Balls Game | 1263A - Sweet Problem |
1332B - Composite Coloring | 254A - Cards with Numbers |