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>
#define fore(i, a, b) for (int i = (a), i##_last = (b); i < i##_last; ++i)
#define fort(i, a, b) for (int i = (a), i##_last = (b); i <= i##_last; ++i)
#define ford(i, a, b) for (int i = (a), i##_last = (b); i >= i##_last; --i)
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double;
template<class A, class B> bool maxi(A &a, const B &b) {return (a < b) ? (a = b, true):false;};
template<class A, class B> bool mini(A &a, const B &b) {return (a > b) ? (a = b, true):false;};
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const int maxN = 400005;
int n, t, a[maxN], b[maxN], c[maxN][2], p[maxN], g[maxN], f[maxN], spt[20][maxN], Log2[maxN];
vii adj[maxN];
ll res;
void input() {
    cin >> n;
    fore(i, 1, n) {
        cin >> a[i] >> b[i] >> c[i][0] >> c[i][1];
        adj[a[i]].emplace_back(i, b[i]);
        adj[b[i]].emplace_back(i, a[i]);
    }
}
void subtask1() {
    const int N = n + 5;
    ll res = 0;
    int parent[N], f[N], r[N];
    const function<void(int)> dfs = [&](const int u) {
        for (const auto &[id, v] : adj[u]) {
            if (v == parent[u])
                continue;
            parent[v] = u;
            r[v] = id;
            dfs(v);
        }
    };
    fore(e, 1, n)
        f[e] = 0;
    fore(i, 1, n) {
        fort(u, 1, n)
            parent[u] = -1;
        dfs(i);
        for (int u = i + 1; u != i; u = parent[u])
            ++f[r[u]];
    }
    fore(e, 1, n)
        res += min(1LL * f[e] * c[e][0], 1LL * c[e][1]);
    cout << res << '\n';
}
void dfs(int u) {
    g[u] = t;
    spt[0][t++] = u;
    for (const auto &[id, v] : adj[u]) {
        if (v == p[u])
            continue;
        p[v] = u;
        dfs(v);
        spt[0][t++] = u;
    }
}
int getMin(int x, int y) {
    return g[x] < g[y] ? x : y;
}
int lca(int l, int r) {
    l = g[l];
    r = g[r];
    if (l > r)
        swap(l, r);
    const int &j = Log2[r - l + 1];
    return getMin(spt[j][l], spt[j][r - (1 << j) + 1]);
}
void dfs1(int u) {
    for (const auto &[e, v] : adj[u]) {
        if (v == p[u])
            continue;
        dfs1(v);
        res += min(1LL * f[v] * c[e][0], 1LL * c[e][1]);
        f[u] += f[v];
    }
}
void subtask3() {
    dfs(1);
    Log2[1] = 0;
    fore(i, 2, t)
        Log2[i] = Log2[i >> 1] + 1;
    for (int j = 1; (1 << j) <= t; ++j)
        fore(i, 0, t - (1 << j) + 1)
            spt[j][i] = getMin(spt[j - 1][i], spt[j - 1][i + (1 << j - 1)]);
    for (int x = 1, y; x < n; ++x) {
        y = lca(x, x + 1);
        ++f[x];
        ++f[x + 1];
        f[y] -= 2;
    }
    dfs1(1);
    cout << res << '\n';
}
int main() {
    #ifdef LOCAL
        freopen("input.INP", "r", stdin);
    #endif // LOCAL
    cin.tie(0) -> sync_with_stdio(0);
    cout.tie(0);
    input();
//    if (n <= 7000)
//        subtask1();
//    else
        subtask3();
    return 0;
}
Compilation message (stderr)
putovanje.cpp: In lambda function:
putovanje.cpp:67:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   67 |         for (const auto &[id, v] : adj[u]) {
      |                          ^
putovanje.cpp: In function 'void dfs(int)':
putovanje.cpp:92:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   92 |     for (const auto &[id, v] : adj[u]) {
      |                      ^
putovanje.cpp: In function 'void dfs1(int)':
putovanje.cpp:115:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  115 |     for (const auto &[e, v] : adj[u]) {
      |                      ^
putovanje.cpp: In function 'void subtask3()':
putovanje.cpp:131:70: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  131 |             spt[j][i] = getMin(spt[j - 1][i], spt[j - 1][i + (1 << j - 1)]);
      |                                                                    ~~^~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |