10292022-02-25 16:48:03Kevinke12Útadócpp14Accepted 50/5097ms23696 KiB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long


ll N;
ll be1, be2;
vector<ll> ellista[50005];
int fel[50005];
vector<ll> adok;

struct adat
{
    int cs;
    ll alatta=0;
    ll suly=0;
    ll felette=0;

};
bool Rend(adat a, adat b)
{
    return (a.alatta*a.felette>b.alatta*b.felette);
}
adat al[50005];

ll Mely(int cs)
{
    ll akt=1;
    for(int a:ellista[cs])
    {
        akt+=Mely(a);
    }
    al[cs].alatta=akt;
    al[cs].cs=cs;
    al[cs].felette = N-al[cs].alatta;
    return akt;
}

int main()
{
    cin >> N;
    for(int i = 0; i < N-1; i++)
    {
        cin >> be1 >> be2;
        ellista[be1].push_back(be2);
        fel[be2]=be1;
    }
    for(int i = 0; i < N-1; i++)
    {
        cin >> be1; adok.push_back(be1);
    }

    Mely(1);
    sort(al+1, al+N+1, Rend);
    sort(adok.begin(), adok.end(), greater<ll>());

    /*for(ll a:adok)
        cout << a << " ";
    cout << "\n";*/


    ll ert=0;
    for(int i = 1; i < N; i++)
    {
        al[i].suly = adok[i-1];
        ert += ((((al[i].alatta)*(N-al[i].alatta))%32609)*al[i].suly*2)%32609;
    }
    cout << ert%32609 << "\n";

    for(int i = 1; i <= N-1; i++)
    {
        cout << fel[al[i].cs] << " " << al[i].cs << " " << al[i].suly << "\n";
    }


    /*for(int i = 1; i <= N; i++)
    {
        cout << al[i].cs << ":" << al[i].alatta << " " << al[i].felette  << "=" << al[i].suly << "\n";
    }*/

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms7336 KiB
2Accepted0/039ms9424 KiB
3Accepted2/24ms7752 KiB
4Accepted2/23ms7756 KiB
5Accepted2/23ms7768 KiB
6Accepted2/24ms7768 KiB
7Accepted2/24ms7768 KiB
8Accepted8/897ms18012 KiB
9Accepted2/24ms8760 KiB
10Accepted2/24ms8772 KiB
11Accepted2/24ms8804 KiB
12Accepted2/24ms8824 KiB
13Accepted2/24ms8836 KiB
14Accepted2/292ms12932 KiB
15Accepted2/289ms14008 KiB
16Accepted2/293ms15092 KiB
17Accepted2/290ms16132 KiB
18Accepted2/290ms17004 KiB
19Accepted2/292ms18228 KiB
20Accepted2/292ms19472 KiB
21Accepted2/294ms20524 KiB
22Accepted2/294ms21584 KiB
23Accepted2/293ms22640 KiB
24Accepted2/297ms23696 KiB