# 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;
}


