# Solution

$f\left[u\right]\left[i\right]\left[j\right]$$f[u][i][j]$表示根节点到$u$$u$一共经过了$i$$i$条未修的铁路，$j$$j$条未修的公路。
$u$$u$是叶子节点，那么

$f\left[u\right]\left[i\right]\left[j\right]={c}_{i}\ast \left({a}_{i}+x\right)\ast \left({b}_{i}+y\right)$

$f\left[u\right]\left[i\right]\left[j\right]=min\left(f\left[lson\right]\left[i\right]\left[j\right]+f\left[rson\right]\left[i\right]\left[j+1\right],f\left[lson\right]\left[i+1\right]\left[j\right]+f\left[rson\right]\left[i\right]\left[j\right]\right)$

#include <bits/stdc++.h>
using namespace std;

typedef long long lint;
const int maxn = 20005;

int n, a[maxn], b[maxn], c[maxn], ch[maxn][2];

lint f[maxn * 2][41][41];
int depa[maxn * 2], depb[maxn * 2];

void dfs(int u)
{
if (u > n) {
for (int i = 0; i <= depa[u]; ++i)
for (int j = 0; j <= depb[u]; ++j)
f[u][i][j] = (lint)c[u - n] * (a[u - n] + i) * (b[u - n] + j);
return ;
}
depa[ch[u][0]] = depa[u] + 1;
depb[ch[u][0]] = depb[u];
dfs(ch[u][0]);

depa[ch[u][1]] = depa[u];
depb[ch[u][1]] = depb[u] + 1;
dfs(ch[u][1]);

for (int i = 0; i <= depa[u]; ++i)
for (int j = 0; j <= depb[u]; ++j)
f[u][i][j] = min(f[ch[u][0]][i][j] + f[ch[u][1]][i][j + 1], f[ch[u][0]][i + 1][j] + f[ch[u][1]][i][j]);
}

int main()
{
scanf("%d", &n);
for (int x, y, i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
if (x < 0) x = -x + n;
if (y < 0) y = -y + n;
ch[i][0] = x; ch[i][1] = y;
}

for (int i = 1; i <= n; ++i)
scanf("%d%d%d", &a[i], &b[i], &c[i]);

dfs(1);

printf("%lld\n", f[1][0][0]);

return 0;
}