RT,讨论区咋没人 wa 16 的????
bool st;
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define re register
//#define int long long
#define pii pair<int,int>
#define ls ((p)<<1)
#define rs (((p)<<1)|1)
#define mid ((l+r)>>1)
#define lowbit(x) ((x)&(-(x)))
#define printc cerr<<"-----------------------------------------------------------\n"
#define yon(x) (x?cout<<"Yes\n":cout<<"No\n")
#define YoN(x) (x?cout<<"YES\n":cout<<"NO\n")
#define rep(x,qwq,qaq) for(re int (x)=(qwq);(x)<=(qaq);++(x))
#define per(x,qwq,qaq) for(re int (x)=(qwq);(x)>=(qaq);--(x))
#define dbg(x) cerr<<#x<<":"<<x<<" "
#define cend cerr<<'\n'
#define fsp(x) fixed<<setprecision(x)
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define inf 0x3f3f3f3f
#define maxn 200010
#define mod 0
int T;
int n,m;
long long a[maxn];
int b[maxn],c[maxn],K[maxn];
__int128 P[maxn];
il __int128 Pre(int a0,int d,int x) {
return (__int128)x*a0+(__int128)x*(1+x)/2*d;
}
il __int128 calc(int id,int day) {
// assert(day>=0);
__int128 res=0;
if(c[id]>0) {
int k=min(day,K[id]);//[1,k]都长1
res+=k;
if(k<day)res+=Pre(b[id],c[id],day)-P[id];
} else if(c[id]==0) {
if(b[id]<1)res=day;
else res=1ll*day*b[id];
} else {
int k=min(day+1,K[id]);
res+=day+1-k;
res+=Pre(b[id],c[id],k-1);
}
return res;
}
il __int128 Q(int id,int l,int r) { //问第l天到第r天长了多高
return calc(id,r)-calc(id,l-1);
}
vector<int>g[maxn];
int f[maxn];
il bool wrk_f(int id,int day) {
if(calc(id,day)<a[id])return false;
int l=1,r=day;
while(l<=r) {
if(Q(id,mid,day)>=a[id])f[id]=mid,l=mid+1;
else r=mid-1;
}
return true;
}
int lst[maxn];
int fa[maxn];
void dfs(int u,int fat) {
fa[u]=fat;
for(int v:g[u]) {
if(v==fat)continue;
dfs(v,u);
}
}
int p[maxn];
int tim[maxn];
il bool chck(int md) {
rep(i,1,n) {
if(!wrk_f(i,md))return false;
}
rep(i,1,n)p[i]=i,tim[i]=0;
stable_sort(p+1,p+n+1,[](int x,int y) {
return f[x]<f[y];
});
tim[1]=1;
// dbg(md),cend;
// rep(i,1,n)dbg(i),dbg(f[i]),cend;
int now=1;
rep(i,1,n) {
int u=p[i];
int t=u;
while(!tim[t]) {
t=fa[t];
++now;
}
int cnt=now;
while(u!=t) {
tim[u]=cnt;
--cnt;
u=fa[u];
}
if(tim[p[i]]>f[p[i]])return false;
}
// rep(i,1,n)dbg(i),dbg(tim[i]),cend;
return true;
}
int u,v;
bool ed;
signed main() {
// freopen("P9755_16.in","r",stdin);
cerr<<(&st-&ed)/1024.0/1024.0<<" MB\n";
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
rep(i,1,n) {
cin >> a[i]>>b[i]>>c[i];
if(c[i]>0) {
K[i]=(1-b[i])/c[i];//[1,k]都长1
P[i]=Pre(b[i],c[i],K[i]);
} else if(c[i]<0) {
K[i]=(int)ceil(1.0*(1-b[i])/c[i]);
}
}
rep(i,2,n) {
cin>>u>>v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1,0);
int l=1,r=1e9,ans=0;
while(l<=r) {
if(chck(mid))ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans;
return 0;
}
/*
input:
output:
*/