#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rop(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define por(i,a,b) for(int i=a;i>b;i--)
#define endl '\n'
#define pii pair<int,int>
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double ld;
using namespace std;
int n,a[100005],b[100005],c[100005],ch[100005][2];
struct node{
int ans,ans1,ans2,sum;
node operator+(const node&x)const{
return(node){ans+x.ans,ans1+x.ans1,ans2+x.ans2,sum+x.sum};
}
bool operator<(const node&x)const{
return ans<x.ans;
}
}x[100005];
void dfs(int u){
if(u>n){
u-=n;
x[u+n]={1ll*c[u]*a[u]*b[u],1ll*c[u]*b[u],1ll*c[u]*a[u],c[u]};
return;
}
dfs(ch[u][0]);
dfs(ch[u][1]);
node r=x[ch[u][1]];
r.ans+=r.ans2;
r.ans1+=r.sum;
node l=x[ch[u][0]];
l.ans+=l.ans1;
l.ans2+=l.sum;
x[u]=min(l+x[ch[u][1]],r+x[ch[u][0]]);
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
rop(i,1,n){
cin>>ch[i][0]>>ch[i][1];
if(ch[i][0]<0)
ch[i][0]=n-ch[i][0];
if(ch[i][1]<0)
ch[i][1]=n-ch[i][1];
}
rep(i,1,n)
cin>>a[i]>>b[i]>>c[i];
dfs(1);
cout<<x[1].ans;
return 0;
}
https://www.luogu.com.cn/record/169316248