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;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i) 
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define fi first
#define se second
#define file "input"
void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  if (fopen(file".inp", "r")) {
    freopen(file".inp", "r", stdin);
    freopen(file".out", "w", stdout);
  }
}
const int N = 2e5+5, LOG = 18;
int n;
int pre[N], anc[N][LOG], high[N];
vector<pair<int, ii>> graph[N];
ll ans = 0;
void dfs(int u, int p) {
  foru(i, 1, LOG-1) {
    anc[u][i] = anc[anc[u][i-1]][i-1];
  }
  fore(x, graph[u]) {
    int v = x.fi;
    if (v == p) continue;
    high[v] = high[u] + 1;
    anc[v][0] = u;
    dfs(v, u);
  }
}
int lca(int u, int v) {
  if (high[u] < high[v]) swap(u, v);
  ford(i, LOG-1, 0) if (high[anc[u][i]] >= high[v])
    u = anc[u][i];
  
  if (u == v) return u;
  ford(i, LOG-1, 0) if (anc[u][i] != anc[v][i]) {
    u = anc[u][i];
    v = anc[v][i];
  }
  return anc[u][0];
}
int calc(int u, int p) {
  int sum = pre[u];
  fore(x, graph[u]) { 
    int v = x.fi;
    if (v == p) continue;
    int use = calc(v, u);
    // cout << u << " " << v << " " << use << "\n";
    
    ans += min(1LL * x.se.fi * use, 1LL * x.se.se);
    sum += use;
  }
  return sum;
}
int main() {
  setIO();
  cin >> n;
  foru(i, 2, n) {
    int u, v, c1, c2;
    cin >> u >> v >> c1 >> c2;
    graph[u].push_back({v, {c1, c2}});
    graph[v].push_back({u, {c1, c2}});
  }
  high[0] = -1;
  dfs(1, 0);
  // cout << lca(3, 4) << "\n";
  foru(i, 1, n-1) {
    int l = lca(i, i+1);
    pre[i] += 1;
    pre[i+1] += 1;
    pre[l] -= 2;
    // cout << i << " " << i+1 << " " << l << "\n";
  }
  // foru(i, 1, n) {
  //   cout << pre[i] << " ";
  // }
  // cout << "\n";
  calc(1, 0);
  cout << ans;
}
Compilation message (stderr)
putovanje.cpp: In function 'void setIO()':
putovanje.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen(file".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
putovanje.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     freopen(file".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... |