Problem p03026 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define REP(i, e) for (int i = 0; i < (int)(e); i++)
#define FOR(i, b, e) for (int i = (int)(b); i < (int)(e); i++)
#define ALL(c) (c).begin(), (c).end()
#define EACH(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define RALL(c) (c).rbegin(), (c).rend()
#define ALLA(a, n) ((a) + 0), ((a) + n)

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef vector<string> vstring;
typedef vector<double> vdouble;

template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}
template <class T> void pp(T v) {
  EACH(it, v) cout << *it << ' ';
  cout << endl;
}
template <class T> T &ls(T &a, T b) {
  if (b < a)
    a = b;
  return a;
}
template <class T> T &gs(T &a, T b) {
  if (b > a)
    a = b;
  return a;
}
inline ll to_i(const string &s) {
  ll n;
  sscanf(s.c_str(), "%lld", &n);
  return n;
}
inline string to_s(ll n) {
  char buf[32];
  sprintf(buf, "%lld", n);
  return string(buf);
}

struct Edge {
  int src, dst;
};

int main() {
  int n;
  while (cin >> n) {
    vector<vector<Edge>> v(n);
    REP(i, n - 1) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      v[a].push_back((Edge){a, b});
      v[b].push_back((Edge){b, a});
    }

    vll c(n);
    REP(i, n) cin >> c[i];
    sort(RALL(c));

    ll best = 0;
    vint bestV;

    REP(root, n) {
      vint vc(n, -1);
      queue<int> qu;
      int o = 0;
      for (qu.push(root); qu.size(); qu.pop()) {
        int p = qu.front();
        if (vc[p] != -1)
          continue;
        vc[p] = c[o++];
        for (auto e : v[p])
          if (vc[e.dst] == -1)
            qu.push(e.dst);
      }

      ll result = 0;
      REP(i, n) for (auto e : v[i]) result += min(vc[e.src], vc[e.dst]);
      if (result > best) {
        best = result;
        bestV.swap(vc);
      }
    }

    cout << best / 2 << endl;
    REP(i, n) {
      if (i)
        cout << ' ';
      cout << bestV[i];
    }
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define REP(i, e) for (int i = 0; i < (int)(e); i++)
#define FOR(i, b, e) for (int i = (int)(b); i < (int)(e); i++)
#define ALL(c) (c).begin(), (c).end()
#define EACH(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define RALL(c) (c).rbegin(), (c).rend()
#define ALLA(a, n) ((a) + 0), ((a) + n)

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef vector<string> vstring;
typedef vector<double> vdouble;

template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}
template <class T> void pp(T v) {
  EACH(it, v) cout << *it << ' ';
  cout << endl;
}
template <class T> T &ls(T &a, T b) {
  if (b < a)
    a = b;
  return a;
}
template <class T> T &gs(T &a, T b) {
  if (b > a)
    a = b;
  return a;
}
inline ll to_i(const string &s) {
  ll n;
  sscanf(s.c_str(), "%lld", &n);
  return n;
}
inline string to_s(ll n) {
  char buf[32];
  sprintf(buf, "%lld", n);
  return string(buf);
}

struct Edge {
  int src, dst;
};

int main() {
  int n;
  while (cin >> n) {
    vector<vector<Edge>> v(n);
    REP(i, n - 1) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      v[a].push_back((Edge){a, b});
      v[b].push_back((Edge){b, a});
    }

    vll c(n);
    REP(i, n) cin >> c[i];
    sort(RALL(c));

    ll best = 0;
    vint bestV;

    REP(root, n) {
      if (root != 0)
        continue;
      vint vc(n, -1);
      queue<int> qu;
      int o = 0;
      for (qu.push(root); qu.size(); qu.pop()) {
        int p = qu.front();
        if (vc[p] != -1)
          continue;
        vc[p] = c[o++];
        for (auto e : v[p])
          if (vc[e.dst] == -1)
            qu.push(e.dst);
      }

      ll result = 0;
      REP(i, n) for (auto e : v[i]) result += min(vc[e.src], vc[e.dst]);
      if (result > best) {
        best = result;
        bestV.swap(vc);
      }
    }

    cout << best / 2 << endl;
    REP(i, n) {
      if (i)
        cout << ' ';
      cout << bestV[i];
    }
    cout << endl;
  }
}