Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair <int, int> pii;
- typedef pair <ll, ll> pll;
- #define precision(n) fixed << setprecision(n)
- #define pb push_back
- #define ub upper_bound
- #define lb lower_bound
- #define mp make_pair
- #define eps (double)1e-9
- #define PI 2*acos(0.0)
- #define endl "\n"
- #define sz(v) int((v).size())
- #define all(v) v.begin(),v.end()
- #define rall(v) v.rbegin(),v.rend()
- #define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- #define OK cout << "OK" << endl;
- const int N = 1e4+7;
- vector <int> graph[N], values;
- int c[N];
- void dfs(int v = 1, int p = 1) {
- for (auto to : graph[v]) {
- if (to == p) continue;
- dfs(to, v);
- }
- c[v] = values.back();
- values.pop_back();
- }
- int main() {
- do_not_disturb
- int n;
- cin >> n;
- for (int i = 1; i < n; i++) {
- int a, b;
- cin >> a >> b;
- graph[a].pb(b);
- graph[b].pb(a);
- }
- for (int i = 1; i <= n; i++) {
- int x;
- cin >> x;
- values.pb(x);
- }
- sort(rall(values));
- dfs();
- int ans = 0;
- for (int i = 2; i <= n; i++) {
- ans += c[i];
- }
- cout << ans << endl;
- for (int i = 1; i <= n; i++) {
- cout << c[i] << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement