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 comment(linker, "/STACK:100000000000000")
using namespace std;
const long long int INF = 2e9 + 1;
vector<vector<pair<int, int> > > g;
vector<int> x;
vector<vector<int> > comp;
int ptr = 0;
void dfs(int id, int isc, int need, int p) {
  comp[isc].push_back(id);
  for (int i = 0; i < ((int)(g[id]).size()); i++) {
    int to = g[id][i].first;
    int d = g[id][i].second;
    if (p == to) continue;
    int togo = isc;
    if (d >= need) {
      togo = ++ptr;
      vector<int> cl;
      comp.push_back(cl);
    }
    dfs(to, togo, need, id);
  }
}
bool tryans(int ans) {
  comp.clear();
  comp.resize(1);
  ptr = 0;
  dfs(0, 0, ans, -1);
  vector<int> sum((int)(comp).size());
  vector<int> used((int)(g).size());
  vector<int> val((int)(g).size());
  int allsum = 0;
  for (int i = 0; i < ((int)(comp).size()); i++) {
    for (int j = 0; j < ((int)(comp[i]).size()); j++) {
      int id = comp[i][j];
      sum[i] += x[id];
    }
    allsum += sum[i];
  }
  set<pair<int, int> > s;
  for (int i = 0; i < ((int)(comp).size()); i++) {
    for (int j = 0; j < ((int)(comp[i]).size()); j++) {
      int id = comp[i][j];
      val[id] = allsum - sum[i];
      s.insert(make_pair(val[id], id));
    }
  }
  int taked = 0;
  for (int i = 0; i < ((int)(comp).size()); i++) {
    int minus = 0;
    for (int j = 0; j < ((int)(comp[i]).size()); j++) {
      int id = comp[i][j];
      if (!used[id]) s.erase(s.find(make_pair(val[id], id)));
    }
    for (int j = 0; j < ((int)(comp[i]).size()); j++) {
      int id = comp[i][j];
      for (int k = 0; k < (x[id]); k++) {
        if (s.empty()) break;
        int to = s.begin()->second;
        s.erase(s.begin());
        used[to]++;
        taked++;
        minus++;
      }
    }
    for (int j = 0; j < ((int)(comp[i]).size()); j++) {
      int id = comp[i][j];
      if (!used[id]) {
        if (!s.empty()) val[id] += minus;
        s.insert(make_pair(val[id], id));
      }
    }
  }
  return taked == (int)(g).size();
}
int main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << 0;
    return 0;
  }
  g.resize(n);
  x.resize(n);
  for (int i = 0; i < (n - 1); i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }
  for (int i = 0; i < (n); i++) scanf("%d", &x[i]);
  int l = 1, r = 10001;
  while (l != r) {
    int mid = (l + r + 1) / 2;
    if (tryans(mid))
      l = mid;
    else
      r = mid - 1;
  }
  cout << l;
}