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;
int n;
struct st {
  int a;
  int b;
  int c;
};
vector<st> v;
bool cmp(st a, st b) { return a.c < b.c; }
vector<int> vv;
int belong[3002];
int siz[3002];
int sizz[3002];
inline int root(int b) {
  if (belong[b] == -1) return b;
  belong[b] = root(belong[b]);
  return belong[b];
}
void merge(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b) return;
  belong[a] = b;
  siz[b] += siz[a];
  sizz[b] += sizz[a];
}
int cap[3002];
bool ok(int val) {
  for (int i = 0; i < 3002; i++) belong[i] = -1, siz[i] = 1, sizz[i] = cap[i];
  for (int i = 0; i < v.size(); i++) {
    if (v[i].c >= val) {
      break;
    }
    merge(v[i].a, v[i].b);
  }
  vector<int> V;
  vector<int> S;
  int cap_sum = 0;
  for (int i = 0; i < n; i++) {
    if (i == root(i)) {
      V.push_back(siz[i]);
      S.push_back(sizz[i]);
      cap_sum += sizz[i];
    }
  }
  for (int i = 0; i < V.size(); i++) {
    int op = cap_sum - S[i];
    if (V[i] > op) {
      return false;
    }
  }
  return true;
}
int main() {
  cin >> n;
  if (n == 1) {
    puts("0");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    v.push_back({a, b, c});
    vv.push_back(c);
  }
  for (int i = 0; i < n; i++) scanf("%d", &cap[i]);
  sort(v.begin(), v.end(), cmp);
  sort(vv.begin(), vv.end());
  int mint = 0;
  int maxt = vv.size() - 1;
  while (mint + 1 < maxt) {
    int mid = (mint + maxt) >> 1;
    if (ok(vv[mid])) {
      mint = mid;
    } else {
      maxt = mid - 1;
    }
  }
  if (ok(vv[maxt])) {
    cout << vv[maxt] << endl;
  } else {
    cout << vv[mint] << endl;
  }
  return 0;
}