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>
using namespace std;
/*
  John Watson
  Handle codeforces : quangminh98
  
  Mua Code nhu mua Florentino !!
*/
#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
const int maxn = 2e5 + 9;
struct Data
{
	int v, c1, c2;
	
	Data() = default;
	Data(int v, int c1, int c2) : v(v), c1(c1), c2(c2) {}
};
vector<Data> adj[maxn];
int diff[maxn], par[20][maxn], h[maxn];
int n;
void DFS(int u, int p = -1)
{
	for (Data nxt : adj[u])
	{
		int v = nxt.v;
		if (v == p) continue;
		
		h[v] = h[u] + 1;
		par[0][v] = u;
		for (int i = 1; i < 20; i++)
			par[i][v] = par[i - 1][par[i - 1][v]];
		
		DFS(v, u);
	}
}
int LCA(int u, int v)
{
	if (h[u] < h[v]) swap(u, v);
	int k = h[u] - h[v];
	
	for (int i = 0; i < 20; i++)
		if (k >> i & 1)
			u = par[i][u];
	
	if (u == v) return u;
	
	for (int i = 19; i >= 0; i--)
		if (par[i][u] != par[i][v])
		{
			u = par[i][u];
			v = par[i][v];
		}
		
	return par[0][u];
}
void DFS_GET(int u, int p = -1)
{
	for (Data nxt : adj[u])
	{
		int v = nxt.v;
		if (v == p) continue;
		
		DFS_GET(v, u);
		diff[u] += diff[v];
	}
}
ll res = 0;
void DFS_RES(int u, int p = -1)
{
	for (Data nxt : adj[u])
	{
		int v = nxt.v;
		int c1 = nxt.c1, c2 = nxt.c2;
		if (v == p) continue;
		
		res += min(1ll * diff[v] * c1, 1ll * c2);
		DFS_RES(v, u);
	}
}
void add(int x, int y)
{
	diff[x]++;
	diff[y]++;
	int lca = LCA(x, y);
	diff[lca] -= 2;
}
signed main()
{
	if (fopen("test.inp", "r"))
	{
		freopen("test.inp", "r", stdin);
		freopen("test.out", "w", stdout);
	}
	faster();
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		int u, v, c1, c2; cin >> u >> v >> c1 >> c2;
		adj[u].push_back({v, c1, c2});
		adj[v].push_back({u, c1, c2});
	}
	
	DFS(1);
	
	for (int i = 1; i < n; i++)
		add(i, i + 1);
		
	DFS_GET(1);
	DFS_RES(1);
	
	cout << res << '\n';
	return 0;
}
Compilation message (stderr)
putovanje.cpp: In function 'int main()':
putovanje.cpp:103:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |   freopen("test.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
putovanje.cpp:104:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |   freopen("test.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |