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>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& a) {
  return out << "(" << a.first << ", " << a.second << ")";
}
template <class A>
ostream& operator<<(ostream& out, const vector<A>& a) {
  out << "[";
  for (auto it = a.begin(); it != a.end(); ++it) {
    if (it != a.begin()) out << ", ";
    out << *it;
  }
  return out << "]";
}
mt19937 rnd(time(NULL));
mt19937_64 rnd64(time(NULL));
const int INF = 1e9;
const long long INF64 = 1e18;
const int MOD = 1e9 + 7;
const long double PI = acosl(-1.0);
const long double EPS = 1e-9;
const int N = 6000 + 7;
int n;
vector<pair<int, int> > g[N];
int a[N];
bool read() {
  if (scanf("%d", &n) != 1) return false;
  for (int i = 0; i < int(n); ++i) g[i].clear();
  for (int i = 0; i < int(n - 1); ++i) {
    int v, u, w;
    scanf("%d%d%d", &v, &u, &w);
    --v, --u;
    g[v].push_back(make_pair(u, w));
    g[u].push_back(make_pair(v, w));
  }
  for (int i = 0; i < int(n); ++i) {
    scanf("%d", &a[i]);
  }
  return true;
}
int cnt;
int s, t;
short dist[N][N];
short cap[N][N], flow[N][N];
void init(int v, int p, int mx, short dist[N]) {
  dist[v] = mx;
  for (auto it : g[v]) {
    int u = it.first;
    int w = it.second;
    if (u == p) continue;
    init(u, v, max(mx, w), dist);
  }
}
void build() {
  s = 2 * n;
  t = s + 1;
  cnt = t + 1;
  for (int i = 0; i < int(n); ++i) {
    dist[i][i] = 0;
    init(i, -1, 0, dist[i]);
  }
  for (int i = 0; i < int(n); ++i) {
    cap[s][i] = 1;
    cap[i + n][t] = a[i];
  }
}
inline int rem(int v, int u) { return cap[v][u] - flow[v][u]; }
int d[N];
int lst[N];
int bfs() {
  for (int i = 0; i < int(cnt); ++i) d[i] = INF;
  d[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int u = 0; u < int(cnt); ++u) {
      if (rem(v, u) && d[u] == INF) {
        d[u] = d[v] + 1;
        if (u == t) return true;
        q.push(u);
      }
    }
  }
  return d[t] != INF;
}
int dfs(int v, int f) {
  if (v == t) return f;
  if (f == 0) return 0;
  int result = 0;
  for (; lst[v] < cnt; ++lst[v]) {
    int u = lst[v];
    if (v >= n && v != t && u == n) lst[v] = u = t;
    if (v == s && u == n) break;
    if (v == u) continue;
    if (d[u] != d[v] + 1) continue;
    int add = dfs(u, min(f, rem(v, u)));
    result += add;
    f -= add;
    flow[v][u] += add;
    flow[u][v] -= add;
    if (f == 0) break;
  }
  return result;
}
int dinic() {
  int flow = 0;
  while (true) {
    if (!bfs()) break;
    for (int v = 0; v < int(n); ++v) lst[v] = n;
    for (int v = 0; v < int(n); ++v) lst[v + n] = 0;
    lst[s] = 0;
    lst[t] = n;
    int add;
    while ((add = dfs(s, INF)) > 0) flow += add;
  }
  return flow;
}
void solve() {
  build();
  vector<int> vals;
  for (int i = 0; i < int(n); ++i)
    for (auto it : g[i]) vals.push_back(it.second);
  sort((vals).begin(), (vals).end());
  vals.resize(unique((vals).begin(), (vals).end()) - vals.begin());
  int l = 0, r = int((vals).size()) - 1;
  int res = 0;
  while (l <= r) {
    int m = (l + r) / 2;
    memset(flow, 0, sizeof(flow));
    for (int i = 0; i < int(n); ++i)
      for (int j = 0; j < int(i); ++j)
        cap[i][n + j] = cap[j][n + i] = (dist[i][j] >= vals[m]);
    int f = 0;
    while (true) {
      int add = dinic();
      if (add == 0) break;
      f += add;
    }
    if (f == n) {
      res = vals[m];
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  printf("%d\n", res);
}
int main() {
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  if (read()) {
    solve();
  }
}