Problem p03026 (C++)

Buggy submission (status: Time Limit Exceeded):
// Copyright lzt
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef std::pair<long long, long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, j, k) for (register int i = (int)(j); i <= (int)(k); i++)
#define rrep(i, j, k) for (register int i = (int)(j); i >= (int)(k); i--)
#define Debug(...) fprintf(stderr, __VA_ARGS__)

inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int maxn = 100100;
int n, cnt;
vector<int> G[maxn];
int num[maxn], a[maxn];
ll ans;
int an[maxn];

void dfs(int u) {
  vector<int> vec;
  rep(i, 0, G[u].size() - 1) if (!a[G[u][i]]) {
    a[G[u][i]] = num[++cnt];
    vec.pb(G[u][i]);
  }
  rep(i, 0, vec.size() - 1) { dfs(vec[i]); }
}

void work() {
  n = read();
  rep(i, 1, n - 1) {
    int x = read(), y = read();
    G[x].pb(y);
    G[y].pb(x);
  }
  rep(i, 1, n) num[i] = read();
  sort(num + 1, num + n + 1);
  reverse(num + 1, num + n + 1);
  rep(i, 1, n) {
    memset(a, 0, sizeof(a));
    cnt = 1;
    a[i] = num[1];
    dfs(i);
    ll nw = 0;
    rep(j, 1, n) rep(k, 0, G[j].size() - 1) {
      if (j < G[j][k])
        nw += min(a[j], a[G[j][k]]);
    }
    if (nw > ans) {
      ans = nw;
      rep(i, 1, n) an[i] = a[i];
    }
  }
  printf("%lld\n", ans);
  rep(i, 1, n) printf("%d ", an[i]);
}

int main() {
#ifdef LZT
  freopen("in", "r", stdin);
#endif

  work();

#ifdef LZT
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
}


Fixed submission:
// Copyright lzt
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef long double ld;
typedef unsigned long long ull;
typedef std::pair<long long, long long> pll;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i, j, k) for (register int i = (int)(j); i <= (int)(k); i++)
#define rrep(i, j, k) for (register int i = (int)(j); i >= (int)(k); i--)
#define Debug(...) fprintf(stderr, __VA_ARGS__)

inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * f;
}

const int maxn = 100100;
int n, cnt;
vector<int> G[maxn];
int num[maxn], a[maxn];
ll ans;
int an[maxn];

void dfs(int u) {
  vector<int> vec;
  rep(i, 0, G[u].size() - 1) if (!a[G[u][i]]) {
    a[G[u][i]] = num[++cnt];
    vec.pb(G[u][i]);
  }
  rep(i, 0, vec.size() - 1) { dfs(vec[i]); }
}

void work() {
  n = read();
  rep(i, 1, n - 1) {
    int x = read(), y = read();
    G[x].pb(y);
    G[y].pb(x);
  }
  rep(i, 1, n) num[i] = read();
  sort(num + 1, num + n + 1);
  reverse(num + 1, num + n + 1);
  rep(i, 1, 1) {
    memset(a, 0, sizeof(a));
    cnt = 1;
    a[i] = num[1];
    dfs(i);
    ll nw = 0;
    rep(j, 1, n) rep(k, 0, G[j].size() - 1) {
      if (j < G[j][k])
        nw += min(a[j], a[G[j][k]]);
    }
    if (nw > ans) {
      ans = nw;
      rep(i, 1, n) an[i] = a[i];
    }
  }
  printf("%lld\n", ans);
  rep(i, 1, n) printf("%d ", an[i]);
}

int main() {
#ifdef LZT
  freopen("in", "r", stdin);
#endif

  work();

#ifdef LZT
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
}