# include <algorithm>
# include <iostream>
# include <queue>
# include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
vector<pii> v[252020];
ll ans = 0;
template<class T>
struct MergeSet {
priority_queue<T, std::vector<T>, std::greater<T>> s;
T shift;
void update(T val) {
shift = shift + val;
}
T popFront() {
auto elem = s.top();
s.pop();
return elem + shift;
}
T front() {
auto elem = s.top();
return elem + shift;
}
void insert(T elem) {
s.push(elem - shift);
}
void clear() {
s.clear();
shift = T{};
}
};
template<class T>
void merge(MergeSet<T>& a, MergeSet<T>& b) {
if (a.s.size() < b.s.size()) {
swap(a, b);
}
while (!b.s.empty()) {
auto elem = b.s.top();
b.s.pop();
a.insert(elem + b.shift);
}
}
ll need[250202];
MergeSet<ll> have[250202];
MergeSet<ll> matched[250202];
int cnt[250202];
void go(int cur, int p = -1) {
if (cnt[cur] < 0) {
need[cur] = -cnt[cur];
} else {
for (int i = 0; i < cnt[cur]; ++i) {
have[cur].insert(0);
}
}
for (pii e : v[cur]) {
int to = e.first;
if (to == p) continue;
go(to, cur);
int len = e.second;
ans += need[to] * 1LL * len;
have[to].update(len);
matched[to].update(len);
merge(have[cur], have[to]);
merge(matched[cur], matched[to]);
need[cur] += need[to];
}
while (have[cur].s.size() > 0 && need[cur] > 0) {
ll a = have[cur].popFront();
ans += a;
matched[cur].insert(-a);
need[cur]--;
}
while (have[cur].s.size() > 0 && matched[cur].s.size() > 0) {
auto a = have[cur].front();
auto b = matched[cur].front();
if (a + b < 0) {
ans += a + b;
have[cur].popFront();
matched[cur].popFront();
have[cur].insert(-b);
matched[cur].insert(-a);
} else {
break;
}
}
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int x, y, w;
cin >> x >> y >> w;
--x; --y;
v[x].push_back({y, w});
v[y].push_back({x, w});
}
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
cnt[i] = x - y;
}
go(0);
cout << ans << endl;
return 0;
}
IyBpbm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ICAgICAgICBtYXRjaGVkW2N1cl0uaW5zZXJ0KC1hKTsKICAgICAgICBuZWVkW2N1cl0tLTsKICAgIH0KICAgIHdoaWxlIChoYXZlW2N1cl0ucy5zaXplKCkgPiAwICYmIG1hdGNoZWRbY3VyXS5zLnNpemUoKSA+IDApIHsKICAgICAgICBhdXRvIGEgPSBoYXZlW2N1cl0uZnJvbnQoKTsKICAgICAgICBhdXRvIGIgPSBtYXRjaGVkW2N1cl0uZnJvbnQoKTsKICAgICAgICBpZiAoYSArIGIgPCAwKSB7CiAgICAgICAgICAgIGFucyArPSBhICsgYjsKICAgICAgICAgICAgaGF2ZVtjdXJdLnBvcEZyb250KCk7CiAgICAgICAgICAgIG1hdGNoZWRbY3VyXS5wb3BGcm9udCgpOwogICAgICAgICAgICBoYXZlW2N1cl0uaW5zZXJ0KC1iKTsKICAgICAgICAgICAgbWF0Y2hlZFtjdXJdLmluc2VydCgtYSk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7Y2luLnRpZSgwKTsKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG4gLSAxOyArK2kpIHsKICAgICAgICBpbnQgeCwgeSwgdzsKICAgICAgICBjaW4gPj4geCA+PiB5ID4+IHc7CiAgICAgICAgLS14OyAtLXk7CiAgICAgICAgdlt4XS5wdXNoX2JhY2soe3ksIHd9KTsKICAgICAgICB2W3ldLnB1c2hfYmFjayh7eCwgd30pOwogICAgfQogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBpbnQgeCwgeTsKICAgICAgICBjaW4gPj4geCA+PiB5OwogICAgICAgIGNudFtpXSA9IHggLSB5OwogICAgfQogICAgZ28oMCk7CiAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwogICAgcmV0dXJuIDA7Cn0KCgo=