Problem: DZY loves planting, and he enjoys solving tree problems.

DZY has a weighted tree (connected undirected graph without cycles) containing n nodes (they are numbered from 1 to n). He defines the function g(x, y) (1 ≤ x, y ≤ n) as the longest edge in the shortest path between nodes x and y. Specially g(z, z) = 0 for every z.

For every integer sequence p1, p2, ..., pn (1 ≤ pi ≤ n), DZY defines f(p) as <image>. 

DZY wants to find such a sequence p that f(p) has maximum possible value. But there is one more restriction: the element j can appear in p at most xj times.

Please, find the maximum possible f(p) under the described restrictions.

Input

The first line contains an integer n (1 ≤ n ≤ 3000).

Each of the next n - 1 lines contains three integers ai, bi, ci (1 ≤ ai, bi ≤ n; 1 ≤ ci ≤ 10000), denoting an edge between ai and bi with length ci. It is guaranteed that these edges form a tree.

Each of the next n lines describes an element of sequence x. The j-th line contains an integer xj (1 ≤ xj ≤ n).

Output

Print a single integer representing the answer.

Examples

Input

4
1 2 1
2 3 2
3 4 3
1
1
1
1


Output

2


Input

4
1 2 1
2 3 2
3 4 3
4
4
4
4


Output

3

Note

In the first sample, one of the optimal p is [4, 3, 2, 1].

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const long long MXN = 3e3 + 10;
const long long MXM = 5e2 + 10;
const long long MX5 = 1e5 + 10;
const long long MX6 = 1e6 + 10;
const long long LOG = 20;
const long long INF = 8e18;
const double eps = 1e-9;
const long long MOD = 1e9 + 7;
long long power(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * power(a * a % md, b / 2, md) % md
                      : power(a * a % md, b / 2, md) % md));
}
long long bmm(long long a, long long b) {
  return (a % b == 0 ? b : bmm(b, a % b));
}
string base2(long long n) {
  string a = "";
  while (n >= 2) {
    a += (char)(n % 2 + '0');
    n /= 2;
  }
  a += (char)(n + '0');
  reverse((a).begin(), (a).end());
  return a;
}
long long n, m, Sum, X[MXN];
long long par[MXN], SZ[MXN], dp[MXN];
vector<long long> G[MXN], vec;
vector<pair<long long, pair<long long, long long> > > E;
bool vis[MXN], mark[MXN];
set<long long> M;
void init() {
  vec.clear();
  M.clear();
  for (int i = 1; i <= n; i++) {
    SZ[i] = 1;
    par[i] = i;
    vis[i] = 0;
    mark[i] = 0;
    dp[i] = X[i];
    G[i].clear();
    M.insert(i);
  }
  return;
}
long long Find(long long u) {
  if (par[u] == u) {
    return u;
  }
  par[u] = Find(par[u]);
  return par[u];
}
void Union(long long u, long long v) {
  long long p1 = Find(u), p2 = Find(v);
  if (p1 == p2) {
    return;
  }
  if (SZ[p1] > SZ[p2]) {
    par[p2] = p1;
    SZ[p1] += SZ[p2];
    dp[p1] += dp[p2];
    M.erase(p2);
  } else {
    par[p1] = p2;
    SZ[p2] += SZ[p1];
    dp[p2] += dp[p1];
    M.erase(p1);
  }
  return;
}
bool DFS1(long long u) {
  bool f = 1;
  mark[u] = 1;
  if (Sum - dp[u] < SZ[u]) return 0;
  for (auto v : G[u]) {
    if (!mark[v]) {
      if (DFS1(v) == 0) return 0;
    }
  }
  return f;
}
void DFS(long long u) {
  vis[u] = 1;
  Sum += dp[u];
  for (auto v : G[u]) {
    if (!vis[v]) {
      DFS(v);
    }
  }
  return;
}
bool check(long long x) {
  init();
  long long i = 0;
  while (i < m && E[i].first < x) {
    long long u = E[i].second.first, v = E[i].second.second;
    Union(u, v);
    i++;
  }
  while (i < m) {
    long long u = E[i].second.first, v = E[i].second.second;
    long long p1 = Find(u), p2 = Find(v);
    G[p1].push_back(p2);
    G[p2].push_back(p1);
    i++;
  }
  auto itr = M.begin();
  while (itr != M.end()) {
    long long now = *itr;
    if (G[now].empty()) return 0;
    vec.push_back(now);
    itr++;
  }
  for (auto u : vec) {
    if (!vis[u]) {
      Sum = 0;
      DFS(u);
      bool f = DFS1(u);
      if (!f) return 0;
    }
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  m = n - 1;
  for (int i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    E.push_back({w, {u, v}});
  }
  for (int i = 1; i <= n; i++) cin >> X[i];
  sort(((E)).begin(), ((E)).end());
  long long l = 0, r = 1e5;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  cout << l;
  return 0;
}