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 double pi = acos(-1);
const int inf = 1000000;
struct edge {
  int x, y, k;
} e[3100];
int a[3100], n, tot, L[3100], R[3100], C[3100];
vector<int> E[3100];
struct Flow {
  int next[120000], e[120000], c[120000], head[13000], dis[13000], bfs[13000],
      len, s, t;
  Flow() { len = 1; }
  void reset(int source, int sink) {
    memset(head, 0, sizeof(head));
    s = source;
    t = sink;
    len = 1;
  }
  void addedge(int x, int y, int k) {
    next[++len] = head[x];
    head[x] = len;
    e[len] = y;
    c[len] = k;
    next[++len] = head[y];
    head[y] = len;
    e[len] = x;
    c[len] = 0;
  }
  bool build() {
    int l, r, i, x;
    memset(dis, 0, sizeof(dis));
    bfs[l = r = 1] = s;
    dis[s] = 1;
    while (l <= r) {
      x = bfs[l];
      for (i = head[x]; i; i = next[i])
        if (c[i] && !dis[e[i]]) {
          dis[e[i]] = dis[x] + 1;
          bfs[++r] = e[i];
        }
      l++;
    }
    return dis[t] != 0;
  }
  int dinic(int x, int v) {
    int flow = 0;
    if (x == t) return v;
    for (int i = head[x]; i; i = next[i])
      if (c[i] && dis[x] + 1 == dis[e[i]]) {
        int ff = dinic(e[i], min(v - flow, c[i]));
        c[i] -= ff;
        c[i ^ 1] += ff;
        flow += ff;
        if (flow == v) return flow;
      }
    dis[x] = -1;
    return flow;
  }
  int maxflow() {
    int ans = 0;
    while (build()) ans += dinic(s, inf);
    return ans;
  }
} flow;
void col(int x, int fa, int k) {
  int tmp = a[x];
  a[x] = k;
  for (int i : E[x])
    if (i != fa && a[i] == tmp) col(i, x, k);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    int x, y, k;
    scanf("%d%d%d", &x, &y, &k);
    e[i] = {x, y, k};
    E[x].push_back(y);
    E[y].push_back(x);
  }
  for (int i = 1; i <= n; ++i) scanf("%d", &C[i]);
  sort(e + 1, e + n, [](edge x, edge y) { return x.k > y.k; });
  tot = 1;
  for (int i = 1; i <= n; ++i) a[i] = 2;
  flow.reset(4 * n + 1, 4 * n + 2);
  for (int i = 1; i <= n - 1; ++i) {
    int fa = a[e[i].x] / 2, x = ++tot;
    assert(a[e[i].x] == a[e[i].y]);
    if (!L[fa])
      L[fa] = x;
    else
      R[fa] = x;
    flow.addedge(x, fa, inf);
    flow.addedge(fa + n, x + n, inf);
    col(e[i].x, e[i].y, x * 2);
    col(e[i].y, e[i].x, x * 2 + 1);
  }
  for (int i = 1; i <= n; ++i) {
    int fa = a[i] / 2;
    if (!L[fa])
      L[fa] = 2 * n + i;
    else
      R[fa] = 2 * n + i;
    flow.addedge(2 * n + i, fa, 1);
    flow.addedge(fa + n, 3 * n + i, inf);
    flow.addedge(4 * n + 1, 2 * n + i, 1);
    flow.addedge(3 * n + i, 4 * n + 2, C[i]);
  }
  int sum = 0;
  for (int i = 1; i <= n - 1; ++i) {
    int x = L[i + 1], y = R[i + 1];
    assert(x > 0);
    assert(y > 0);
    flow.addedge(x, y + n, inf);
    flow.addedge(y, x + n, inf);
    sum += flow.maxflow();
    if (sum == n) {
      printf("%d\n", e[i].k);
      return 0;
    }
  }
  printf("0\n");
  return 0;
}