10262022-02-25 07:55:02Szin AttilaÚtadócpp14Accepted 50/5068ms25192 KiB
#include <bits/stdc++.h>
using namespace std;
#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using ll = long long;

const ll maxN = 2e5 + 5;
const ll MOD = 1e9 + 7;

ll n;
vector<vector<ll> > g;
vector<ll> v, cnt;

vector<pair<ll, pair<ll, ll> > > mo;

ll count(ll x, ll p) {
    ll ret = 1;
    for(ll sz : g[x]) {
        if(sz == p) continue;
        ret += count(sz, x);
    }

    return cnt[x] = ret;
}

void solve(ll x, ll p) {
    for(ll sz : g[x]) {
        if(sz == p) continue;
        mo.push_back({cnt[sz] * (n-cnt[sz]), {sz, x}});
        solve(sz, x);
    }
}


int main() {
   InTheNameOfGod;

    
    cin >> n;

    g.resize(n+1);
    v.resize(n-1);
    cnt.resize(n+1);

    for(ll i = 0; i < n-1; i++) {
        ll x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    for(ll &i : v) cin >> i;

    count(1, -1);
    solve(1, -1);

    sort(v.begin(), v.end());
    sort(mo.begin(), mo.end());


    ll ki = 0;
    for(ll i = 0; i < n-1; i++) {
        ki += mo[i].first * v[i] * 2;
        ki %= 32609;
    }
    cout << ki << "\n";
    for(ll i = 0; i < n-1; i++) {
        cout << mo[i].second.first << " " << mo[i].second.second << " " << v[i] << "\n";
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1760 KiB
2Accepted0/032ms7160 KiB
3Accepted2/21ms2284 KiB
4Accepted2/21ms2288 KiB
5Accepted2/21ms2296 KiB
6Accepted2/21ms2292 KiB
7Accepted2/21ms2304 KiB
8Accepted8/857ms22220 KiB
9Accepted2/22ms3600 KiB
10Accepted2/22ms3624 KiB
11Accepted2/22ms3644 KiB
12Accepted2/22ms3648 KiB
13Accepted2/22ms3660 KiB
14Accepted2/261ms15848 KiB
15Accepted2/265ms16800 KiB
16Accepted2/268ms17864 KiB
17Accepted2/265ms18912 KiB
18Accepted2/263ms19952 KiB
19Accepted2/259ms20796 KiB
20Accepted2/263ms21772 KiB
21Accepted2/268ms21996 KiB
22Accepted2/264ms23092 KiB
23Accepted2/257ms24148 KiB
24Accepted2/265ms25192 KiB