This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
tuple<int,int,int,int> E[N];
vector<int> g[N];
int sz[N], up[N], dep[N], pos[N], idx[N], top[N], s[N<<2], ti, L, R, n;
void init(int ind, int low, int high){
	if(low == high){ idx[low] = ind; return;}
	int mid = low + ((high-low)>>1);
	init(ind<<1, low, mid); init(ind<<1|1, mid+1, high);
}
void upd(int ind, int low, int high){
	if(low > R || high < L) return;
	if(L <= low && high <= R){ ++s[ind]; return;}
	int mid = low + ((high-low)>>1);
	upd(ind<<1, low, mid); upd(ind<<1|1, mid+1, high);
}
void dfs(int u){
	sz[u] = 1;
	for(int& v: g[u]){
		g[v].erase(find(g[v].begin(), g[v].end(), up[v] = u));
		dep[v] = dep[u] + 1; dfs(v); sz[u] += sz[v];
		if(sz[v] > sz[g[u][0]]) swap(v, g[u][0]);
	}
}
void efs(int u){
	pos[u] = ti++;
	for(int v: g[u]){
		top[v] = (u == g[u][0]) ? top[u] : v;
		efs(v);
	}
}
void UPD(int u, int v){
	if(top[u] == top[v]){
		if(dep[u] > dep[v]) swap(u, v);
		L = pos[u]; R = pos[v]; upd(1, 0, n-1);
		return;
	}
	if(dep[top[u]] > dep[top[v]]) swap(u, v);
	L = pos[top[v]]; R = pos[v]; upd(1, 0, n-1);
	UPD(u, up[top[v]]);
}
signed main(){
	scanf("%d", &n);
	for(int i = 0, u, v, w[2]; i<n-1; ++i){
		scanf("%d %d %d %d", &u, &v, w, w+1); --u; --v;
		g[u].push_back(v); g[v].push_back(u);
		E[i] = {u, v, w[0], w[1]};
	}
	dfs(0); efs(0);
	for(int i = 0; i<n-1; ++i) UPD(i, i+1);
	ll tot = 0;
	init(1, 0, n-1);
	for(int i = 0, u, v, w[2]; i<n-1; ++i){
		tie(u, v, w[0], w[1]) = E[i];
		if(dep[u] > dep[v]) swap(u, v);
		ll cnt = 0;
		for(int x = idx[pos[v]]; x; x>>=1) cnt += s[x];
		tot += min(cnt * w[0], 1ll*w[1]);
	}	
	printf("%lld", tot);
}
Compilation message (stderr)
putovanje.cpp: In function 'int main()':
putovanje.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
putovanje.cpp:47:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |   scanf("%d %d %d %d", &u, &v, w, w+1); --u; --v;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |