Problem p03026 (C++)

Buggy submission (status: Time Limit Exceeded):
/***********************************************************
 *This code By @1353055672(Ligen)                          *
 *[Warning]You're not excepted to understand this code!    *
 *STOI GDOI APIO CTS NOIP 2019 RP++                        *
 ***********************************************************/
// #pragma GCC optimize(3)
// #pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <vector>
#define ll long long
#define ull unsigned long long
#define mn 20020
#define Max(x, y) ((x > y) ? x : y)
#define Min(x, y) ((x < y) ? x : y)
#define Abs(x) (((x) < (0)) ? (-(x)) : (x))
#define infll (ll)(1e18)
#define infint (int)(1 << 30)
#define mod (int)(1e9 + 7)
#define FOR(a, b, c) for (register ll a = b; a <= c; a++)
#define FORD(a, b, c) for (register ll a = b; a >= c; a--)
using namespace std;
inline ll read() {
  ll x = 0, f = 1;
  char c;
  for (c = getchar(); c < '0' || c > '9';
       f = ((c == '-') ? -1 : f), c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
    ;
  return x * f;
}

template <typename T> inline void write(T a) {
  if (a == 0) {
    putchar('0');
    return;
  }
  if (a < 0)
    putchar('-'), a = -a;
  char c1[120];
  int h = 0;
  while (a)
    c1[++h] = a % 10 + '0', a /= 10;
  FORD(i, h, 1) putchar(c1[i]);
}
inline void write_() { return; }
template <typename T, typename... Args> inline void write_(T a, Args... b) {
  write(a);
  putchar(' ');
  write_(b...);
}
inline void writeln() {
  putchar('\n');
  return;
}
template <typename T, typename... Args> inline void writeln(T a, Args... b) {
  write(a);
  putchar(' ');
  writeln(b...);
}
// need c++11

// inline void write(ll a){
//     if(a==0){putchar('0');return;}if(a<0)putchar('-'),a=-a;char c1[120];int
//     h=0; while(a)c1[++h]=a%10+'0',a/=10;FORD(i,h,1)putchar(c1[i]);
// }
// inline void write_(ll a){write(a);putchar(' ');}
// inline void writeln(ll a){write(a);putchar('\n');}

inline ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b % a, a); }
inline ll lcm(ll a, ll b) { return 1ll * a / gcd(a, b) * b; }
inline ll Pow(ll n, ll a) {
  ll b = 1;
  while (a) {
    if (a & 1)
      b = 1ll * b * n % mod;
    n = 1ll * n * n % mod;
    a >>= 1;
  }
  return b;
}
//---------------------Head Files--------------------------//
int n, fir[mn], nxt[mn], y[mn], a[mn], x, yy, tot, In[mn], ans, d[mn], now, mx,
    num, an[mn];
inline void adde(int x, int yy) {
  nxt[++tot] = fir[x];
  fir[x] = tot;
  y[tot] = yy;
  ++In[x];
}
struct node {
  int x, c;
};
inline bool operator<(const node &a, const node &b) { return a.c < b.c; }
priority_queue<node> q[mn];
inline void dfs(int x, int fa) {
  d[x] = a[now--];
  for (int i = fir[x]; i; i = nxt[i])
    if (y[i] != fa)
      q[x].push((node){y[i], In[y[i]]});
  int yy;
  while (!q[x].empty()) {
    yy = q[x].top().x;
    q[x].pop();
    dfs(yy, x);
    num += Min(d[x], d[yy]);
  }
}
signed main() {
  n = read();
  FOR(i, 2, n) x = read(), yy = read(), adde(x, yy), adde(yy, x);
  FOR(i, 1, n) a[i] = read();
  sort(a + 1, a + 1 + n);
  FOR(i, 1, n) mx = Max(mx, In[i]);
  FOR(i, 1, n) if (In[i] == mx) {
    now = n;
    num = 0;
    dfs(i, 0);
    if (num > ans) {
      ans = num;
      FOR(j, 1, n) an[j] = d[j];
    }
  }
  writeln(ans);
  FOR(i, 1, n) write_(an[i]);
  puts("");
  return 0;
}


Fixed submission:
/***********************************************************
 *This code By @1353055672(Ligen)                          *
 *[Warning]You're not excepted to understand this code!    *
 *STOI GDOI APIO CTS NOIP 2019 RP++                        *
 ***********************************************************/
// #pragma GCC optimize(3)
// #pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <vector>
#define ll long long
#define ull unsigned long long
#define mn 20020
#define Max(x, y) ((x > y) ? x : y)
#define Min(x, y) ((x < y) ? x : y)
#define Abs(x) (((x) < (0)) ? (-(x)) : (x))
#define infll (ll)(1e18)
#define infint (int)(1 << 30)
#define mod (int)(1e9 + 7)
#define FOR(a, b, c) for (register ll a = b; a <= c; a++)
#define FORD(a, b, c) for (register ll a = b; a >= c; a--)
using namespace std;
inline ll read() {
  ll x = 0, f = 1;
  char c;
  for (c = getchar(); c < '0' || c > '9';
       f = ((c == '-') ? -1 : f), c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
    ;
  return x * f;
}

template <typename T> inline void write(T a) {
  if (a == 0) {
    putchar('0');
    return;
  }
  if (a < 0)
    putchar('-'), a = -a;
  char c1[120];
  int h = 0;
  while (a)
    c1[++h] = a % 10 + '0', a /= 10;
  FORD(i, h, 1) putchar(c1[i]);
}
inline void write_() { return; }
template <typename T, typename... Args> inline void write_(T a, Args... b) {
  write(a);
  putchar(' ');
  write_(b...);
}
inline void writeln() {
  putchar('\n');
  return;
}
template <typename T, typename... Args> inline void writeln(T a, Args... b) {
  write(a);
  putchar(' ');
  writeln(b...);
}
// need c++11

// inline void write(ll a){
//     if(a==0){putchar('0');return;}if(a<0)putchar('-'),a=-a;char c1[120];int
//     h=0; while(a)c1[++h]=a%10+'0',a/=10;FORD(i,h,1)putchar(c1[i]);
// }
// inline void write_(ll a){write(a);putchar(' ');}
// inline void writeln(ll a){write(a);putchar('\n');}

inline ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b % a, a); }
inline ll lcm(ll a, ll b) { return 1ll * a / gcd(a, b) * b; }
inline ll Pow(ll n, ll a) {
  ll b = 1;
  while (a) {
    if (a & 1)
      b = 1ll * b * n % mod;
    n = 1ll * n * n % mod;
    a >>= 1;
  }
  return b;
}
//---------------------Head Files--------------------------//
int n, fir[mn], nxt[mn], y[mn], a[mn], x, yy, tot, In[mn], ans, d[mn], now, mx,
    num, an[mn];
inline void adde(int x, int yy) {
  nxt[++tot] = fir[x];
  fir[x] = tot;
  y[tot] = yy;
  ++In[x];
}
struct node {
  int x, c;
};
inline bool operator<(const node &a, const node &b) { return a.c < b.c; }
priority_queue<node> q[mn];
inline void dfs(int x, int fa) {
  d[x] = a[now--];
  for (int i = fir[x]; i; i = nxt[i])
    if (y[i] != fa)
      q[x].push((node){y[i], In[y[i]]});
  int yy;
  while (!q[x].empty()) {
    yy = q[x].top().x;
    q[x].pop();
    dfs(yy, x);
    num += Min(d[x], d[yy]);
  }
}
signed main() {
  n = read();
  FOR(i, 2, n) x = read(), yy = read(), adde(x, yy), adde(yy, x);
  FOR(i, 1, n) a[i] = read();
  sort(a + 1, a + 1 + n);
  FOR(i, 1, n) mx = Max(mx, In[i]);
  FOR(i, 1, n) if (In[i] == mx) {
    now = n;
    num = 0;
    dfs(i, 0);
    if (num > ans) {
      ans = num;
      FOR(j, 1, n) an[j] = d[j];
    }
    break;
  }
  writeln(ans);
  FOR(i, 1, n) write_(an[i]);
  puts("");
  return 0;
}