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;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  write(x);
  puts("");
}
struct Edge {
  long long v, w, to;
} e[100005 * 2];
struct Node {
  long long l, r, w;
  inline bool operator<(const Node& x) const { return w > x.w; }
} a[100005];
long long n, l, r, w, x, y, xx, yy, xxx, yyy, cnt, size, root, b[100005],
    c[100005], f[100005], g[100005], id[100005], fa[100005], dep[100005],
    num[100005], sum[100005], head[100005];
inline void add(long long u, long long v, long long w) {
  e[++size].v = v;
  e[size].to = head[u];
  e[size].w = w;
  head[u] = size;
}
inline void inc1(long long x, long long k) {
  while (x <= n) {
    f[x] += k;
    x += x & (-x);
  }
  return;
}
inline void inc2(long long x, long long k) {
  while (x <= n) {
    g[x] += k;
    x += x & (-x);
  }
  return;
}
inline long long ask1(long long x) {
  long long ans = 0;
  while (x) {
    ans += f[x];
    x -= x & (-x);
  }
  return ans;
}
inline long long ask2(long long x) {
  long long ans = 0;
  while (x) {
    ans += g[x];
    x -= x & (-x);
  }
  return ans;
}
inline void dfs(long long u, long long last) {
  id[u] = ++cnt;
  num[cnt] = u;
  fa[u] = last;
  dep[u] = dep[last] + 1;
  sum[u] = 1;
  for (long long i = head[u]; i; i = e[i].to) {
    long long v = e[i].v;
    if (v == last) continue;
    dfs(v, u);
    sum[u] += sum[v];
  }
  return;
}
inline void dfs2(long long u, long long last) {
  inc1(id[u], -b[u]);
  inc2(id[u], -c[u]);
  b[u] = c[u] = 0;
  for (long long i = head[u]; i; i = e[i].to) {
    long long v = e[i].v;
    if (v == last) continue;
    dfs2(v, u);
  }
  return;
}
int main() {
  n = read();
  for (long long i = 1; i < n; i++) {
    l = read();
    r = read();
    w = read();
    add(l, r, w);
    add(r, l, w);
    a[i].l = l;
    a[i].r = r;
    a[i].w = w;
  }
  dfs(1, 1);
  for (long long i = 1; i <= n; i++) {
    b[i] = 1;
    c[i] = read();
    inc1(id[i], b[i]);
    inc2(id[i], c[i]);
  }
  sort(a + 1, a + n);
  root = 1;
  x = ask1(cnt);
  y = ask2(cnt);
  root = 1;
  for (long long i = 1; i < n; i++) {
    l = a[i].l;
    r = a[i].r;
    if (!b[l] || !b[r]) continue;
    if (dep[l] > dep[r]) swap(l, r);
    xx = ask1(id[r] + sum[r] - 1) - ask1(id[r] - 1);
    yy = ask2(id[r] + sum[r] - 1) - ask2(id[r] - 1);
    xxx = ask1(id[root] + sum[root] - 1);
    yyy = ask2(id[root] + sum[root] - 1);
    if (xx <= yyy - yy) {
      x -= xx;
      x -= min(x, yy);
      y -= xx;
      y -= min(y, xx);
      dfs2(r, fa[r]);
    } else {
      for (int j = 1; j < id[r]; j++) {
        inc1(id[i], -b[i]);
        inc2(id[i], -c[i]);
        b[i] = c[i] = 0;
      }
      for (int j = id[r] + sum[r]; j <= n; j++) {
        inc1(id[i], -b[i]);
        inc2(id[i], -c[i]);
        b[i] = c[i] = 0;
      }
      x -= xxx - xx;
      x -= min(x, yyy - yy);
      y -= yyy - yy;
      y -= min(y, xxx - xx);
    }
    if (!x) {
      writeln(a[i].w);
      return 0;
    }
  }
  puts("0");
  return 0;
}