Language: C++

#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; }
template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; }
 
 
int main() {
    int N;
    cin >> N;
    map<int, vector<int>> graph;
    map<int, int> edge_num_list;
    rep(i, N - 1) {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
        edge_num_list[a]++;
        edge_num_list[b]++;
    }
 
    vector<pair<int, int>> sort_edge_num(N);
    int i = 0;
    for (auto& a : edge_num_list) {
        sort_edge_num[i] = make_pair(a.second, a.first);
        i++;
    }
    sort(sort_edge_num.begin(), sort_edge_num.end(), greater<pair<int, int>>());
    vint C(N);
    map<int, int, std::greater<int>> score_num;
    vint unique;
    rep(i, N) {
        cin >> C[i];
        score_num[C[i]] ++;
    }
    
 
    sort(C.begin(), C.end(), greater<int>());
 
 
    vint V_score(N + 1, -1);
    //sort(score_num.begin(), score_num.end(), greater<>());
   //score_num.
    for (auto& sn : score_num) {
        
        int start;
        rep(i, N) {
            start = sort_edge_num[i].second;
            if (V_score[start] == -1)
                break;
        }
        deque<int> q;
        q.push_back(start);
        V_score[start] = sn.first;
        int max_num = sn.second - 1;
        while (1) {
            if (q.size() <= 0 ) {
                if (max_num > 0) {
                    rep(i, N) {
                        start = sort_edge_num[i].second;
                        if (V_score[start] == -1)
                            break;
                    }
                    q.push_back(start);
                    V_score[start] = sn.first;
                    max_num--;
                }
                else {
                    break;
                }
            }
                
            auto nd = q.front();
            q.pop_front();
 
            for (auto adj : graph[nd]) {
                if (V_score[adj] == -1 && max_num > 0) {
                    V_score[adj] = sn.first;
                    max_num--;
                    q.push_back(adj);
                }
            }
        }
    }
 
//    REP(i, 1, N) {
//        if (V_score[sort_edge_num[i].second] == -1)
//            V_score[sort_edge_num[i].second] = C[i];
//    }
    ll score = 0;
    for (auto& v : graph) {
        for (auto& adj : graph[v.first]) {
            score += min(V_score[v.first], V_score[adj]);
        }
    }
 
    score = 0;
    REP(i, 1, N) {
        score += C[i];
    }
    cout << score << endl;
 
    REP(i, 1, N + 1) {
        cout << V_score[i] << " ";
    }
    cout << endl;
 
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:48:5: error: 'map' was not declared in this scope
   48 |     map<int, vector<int>> graph;
      |     ^~~
a.cc:26:1: note: 'std::map' is defined in header '<map>'; this is probably fixable by adding '#include <map>'
   25 | #include <functional>
  +++ |+#include <map>
   26 | using namespace std;
a.cc:48:9: error: expected primary-expression before 'int'
   48 |     map<int, vector<int>> graph;
      |         ^~~
a.cc:49:9: error: expected primary-expression before 'int'
   49 |     map<int, int> edge_num_list;
      |         ^~~
a.cc:53:9: error: 'graph' was not declared in this scope; did you mean 'isgraph'?
   53 |         graph[a].push_back(b);
      |         ^~~~~
      |         isgraph
a.cc:55:9: error: 'edge_num_list' was not declared in this scope
   55 |         edge_num_list[a]++;
      |         ^~~~~~~~~~~~~
a.cc:61:20: error: 'edge_num_list' was not declared in this scope
   61 |     for (auto& a : edge_num_list) {
      |                    ^~~~~~~~~~~~~
a.cc:67:9: error: expected primary-expression before 'int'
   67 |     map<int, int, std::greater<int>> score_num;
      |         ^~~
a.cc:71:9: error: 'score_num' was not declared in this scope
   71 |         score_num[C[i]] ++;
      |         ^~~~~~~~~
a.cc:81:21: error: 'score_num' was not declared in this scope
   81 |     for (auto& sn : score_num) {
      |                     ^~~~~~~~~
a.cc:113:29: error: 'graph' was not declared in this scope; did you mean 'isgraph'?
  113 |             for (auto adj : graph[nd]) {
      |                             ^~~~~
      |                             isgraph
a.cc:128:20: error: 'graph' was not declared in this scope; did you mean 'isgraph'?
  128 |     for (auto& v : graph) {
      |                    ^~~~~
      |                    isgraph