#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; }
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> ui(0, 1 << 30);
int rng() { 
    return ui(mrand);
}
const int N=100100;
int n;
vector<pair<int,int>> adj[N];
ll speed[N],depth[N],ans[N],S[N];
/**
 * Author: Simon Lindholm
 * Date: 2017-04-20
 * License: CC0
 * Source: own work
 * Description: Container where you can add lines of the form kx+m, and query maximum values at points x.
 *  Useful for dynamic programming (``convex hull trick'').
 * Time: O(\log N)
 * Status: stress-tested
 */
struct Line {
	mutable ll k, m, p;
	bool operator<(const Line& o) const { return k < o.k; }
	bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
	// (for doubles, use inf = 1/.0, div(a,b) = a/b)
	static const ll inf = LLONG_MAX;
	ll div(ll a, ll b) { // floored division
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) return x->p = inf, 0;
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m) {
		auto z = insert({k, m, 0}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p)
			isect(x, erase(y));
	}
	ll query(ll x) {
		assert(!empty());
		auto l = *lower_bound(x);
		return l.k * x + l.m;
	}
};
LineContainer st[4*N];
int tin[N],tout[N],timer=0;
void dfs(int u=0,int p=-1) {
  tin[u]=timer++;
  for (auto &[v,w]: adj[u]) if (v != p) {
    depth[v]=depth[u]+w;
    dfs(v,u);
  }
  tout[u]=timer-1;
}
ll query(int p,ll s,int u=1,int tl=0,int tr=n-1) {
  ll res = (st[u].empty() ? 0LL: st[u].query(s));
  if (tl == tr) {
    return res;
  }
  int mid=(tl+tr)/2;
  if (p <= mid)
    cmax(res,query(p,s,2*u,tl,mid));
  else  
    cmax(res,query(p,s,2*u+1,mid+1,tr));
  return res;
}
void insert(int l,int r,ll m,ll q,int u=1,int tl=0,int tr=n-1) {
  if (l > r) return;
  if (l == tl && r == tr) {
    st[u].add(m,q);
    return;
  }
  int mid=(tl+tr)/2;
  insert(l,min(mid,r),m,q,2*u,tl,mid);
  insert(max(mid+1,l),r,m,q,2*u+1,mid+1,tr);
}
void dfs2(int u=0,int p=-1) {
  if (u > 0) {
    ans[u] = S[u]+depth[u]*speed[u]-query(tin[u],speed[u]);
  }
  insert(tin[u],tout[u],depth[u],-ans[u]);
  for (auto &[v,w]: adj[u]) if (v!=p)
    dfs2(v,u);
}
void solve() {
  cin >> n;
  for (int u,v,w,i=1;i<n;i++) {
    cin >> u >> v >> w;
    --u,--v;
    adj[u].emplace_back(v,w);
    adj[v].emplace_back(u,w);
  }
  for (int i=1;i<n;i++) {
    cin >> S[i] >> speed[i]; 
  }
  dfs();
  dfs2();
  for (int i=1;i<n;i++)
    cout << ans[i] << " ";
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false); 
  cin.tie(nullptr);
  
  int testcases = 1;
  // cin >> testcases;
  for (int i = 1; i <= testcases; i++) 
    solve();
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |