2322021-03-05 10:50:12mraronÚtadócpp14Accepted 50/5061ms22688 KiB
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


const double PI=acos(-1);
const ll INF = 1LL<<62;
const ll MINF = -(1LL<<62);

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)

const int mod=32609;
const int MAXN=50001;
vector<int> adj[MAXN];
int w[MAXN], sz[MAXN], st[MAXN];
int n;
vector<array<int,3>> lst;

void dfs(int x) {
	st[x]=1;
	sz[x]=1;
	for(auto i:adj[x]) {
		if(st[i]) continue ;
		dfs(i);
		lst.push_back({2*sz[i]*(n-sz[i]),x,i});
		sz[x]+=sz[i];
	}
}

int main() {
	IO;
	
	cin>>n;
	for(int i=1;i<n;++i) {
		int a,b;
		cin>>a>>b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	
	for(int i=0;i<n-1;++i) {
		cin>>w[i];
	}
	sort(w,w+n-1);
	
	dfs(1);
	sort(all(lst));
	
	int ans=0, ind=0;
	for(auto i:lst) {
		ans=(ans+(i[0]%mod)*(w[ind]%mod))%mod;
		ind++;
	}
	
	
	
	cout<<ans<<"\n";

	ind=0;
	for(auto i:lst) {
		cout<<i[1]<<" "<<i[2]<<" "<<w[ind]<<"\n";
		ind++;
	}
	
	
	
	return 0;	
}

SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms4360 KiB
2Accepted0/024ms7356 KiB
3Accepted2/24ms4772 KiB
4Accepted2/24ms4984 KiB
5Accepted2/24ms5192 KiB
6Accepted2/24ms5508 KiB
7Accepted2/23ms5620 KiB
8Accepted8/857ms22688 KiB
9Accepted2/24ms6104 KiB
10Accepted2/24ms6452 KiB
11Accepted2/24ms6460 KiB
12Accepted2/24ms6340 KiB
13Accepted2/24ms6576 KiB
14Accepted2/257ms12380 KiB
15Accepted2/257ms12636 KiB
16Accepted2/257ms12600 KiB
17Accepted2/261ms12724 KiB
18Accepted2/257ms12692 KiB
19Accepted2/257ms12692 KiB
20Accepted2/257ms13036 KiB
21Accepted2/259ms13168 KiB
22Accepted2/259ms13124 KiB
23Accepted2/259ms13132 KiB
24Accepted2/259ms13128 KiB