93202024-02-20 13:05:19AblablablaÚtadócpp17Accepted 50/50104ms17396 KiB
#include <bits/stdc++.h>

using namespace std;


typedef long long ll;
typedef pair<ll, ll> pii;

const ll MOD = 32609;

vector<vector<ll>> csucsok;
vector<ll> alatta;

ll bejar(ll akt, ll elozo){
    for(ll x : csucsok[akt]){
        if(x == elozo) continue;

        alatta[akt] += bejar(x, akt);
    }

    alatta[akt]++;
    return alatta[akt];
}

int main()
{
    ll n;
    cin >> n;

    ll m = n - 1;

    csucsok.assign(n, vector<ll>());
    vector<pii> elek;
    for(ll i = 0; i < m; i++){
        ll a, b;
        cin >> a >> b;
        a--; b--;

        csucsok[a].push_back(b);
        csucsok[b].push_back(a);
        elek.push_back({a, b});
    }

    alatta.assign(n, 0);
    bejar(0, -1);

    vector<ll> adok(m);
    for(ll i = 0; i < m; i++){
        cin >> adok[i];
    }

    sort(adok.begin(), adok.end(), greater<ll>());

    vector<pii> sor(m);
    for(ll i = 0; i < m; i++){
        ll a = alatta[elek[i].first];
        ll b = alatta[elek[i].second];

        ll lent, fent;

        if(a < b){
            lent = a;
            fent = n - a;
        } else{
            lent = b;
            fent = n - b;
        }

        sor[i] = {lent * fent, i};
    }

    ll valasz = 0;

    sort(sor.begin(), sor.end(), greater<pii>());
    for(ll i = 0; i < m; i++){
        valasz += sor[i].first * adok[i];
        valasz %= MOD;
    }

    cout << (valasz * 2) % MOD << "\n";

    for(ll i = 0; i < m; i++){
        cout << elek[sor[i].second].first + 1 << " " << elek[sor[i].second].second + 1 << " " << adok[i] << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1996 KiB
2Accepted0/041ms6664 KiB
3Accepted2/23ms2384 KiB
4Accepted2/23ms2576 KiB
5Accepted2/23ms2788 KiB
6Accepted2/23ms2964 KiB
7Accepted2/23ms2864 KiB
8Accepted8/898ms17396 KiB
9Accepted2/24ms3076 KiB
10Accepted2/24ms3196 KiB
11Accepted2/24ms3324 KiB
12Accepted2/24ms3176 KiB
13Accepted2/24ms3172 KiB
14Accepted2/2101ms14080 KiB
15Accepted2/2100ms14372 KiB
16Accepted2/2101ms14276 KiB
17Accepted2/2101ms14280 KiB
18Accepted2/2101ms14280 KiB
19Accepted2/2101ms14152 KiB
20Accepted2/2101ms14152 KiB
21Accepted2/2101ms14152 KiB
22Accepted2/2103ms14288 KiB
23Accepted2/2104ms14360 KiB
24Accepted2/2103ms14600 KiB