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;
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define MAX 200000
struct x {
    lli des;
    lli w;
    lli comp;
};
lli n,a,b,c,d,raiz,op,total,q;
vector<x> hijos[MAX+2];
lli sub[MAX+2];
void pre_dfs(lli pos, lli padre) {
    for(auto h : hijos[pos]) {
        if(h.des == padre) continue;
        pre_dfs(h.des,pos);
        sub[pos] += sub[h.des] + (h.comp-h.w);
    }
}
void busca_raiz(lli pos, lli padre,lli carga) {
    lli a = carga + sub[pos];
    if (a > op){
        op = a;
        raiz = pos;
    }  
    for(auto h : hijos[pos]){
        if(h.des == padre) continue;
        a = carga + sub[pos] - sub[h.des];
        a -= (h.comp-h.w);
        a += h.w;
        busca_raiz(h.des,pos,a);
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    rep(i,1,n-1) {
        cin >> a >> b >> c >> d;
        hijos[a].push_back({b,c,c+d});
        hijos[b].push_back({a,d,c+d});
        total += (c+d);
    }
    pre_dfs(1,0);
    busca_raiz(1,0,0);
    cin >> q >> a;
    cout << (total-op);
    return 0;  
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |