Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Non-redundant Drive

The people of JAG kingdom hate redundancy. For example, the N cities in JAG kingdom are connected with just $N - 1$ bidirectional roads such that any city is reachable from any city through some roads. Under the condition, the number of paths from a city to another city is exactly one for all pairs of the cities. This is a non-redundant road network :)

One day, you, a citizen of JAG kingdom, decided to travel as many cities in the kingdom as possible with a car. The car that you will use has an infinitely large tank, but initially the tank is empty. The fuel consumption of your car is 1 liter per 1 km, i.e. it consumes 1 liter of gasoline to move 1 km.

Each city has exactly one gas station, and you can supply $g_x$ liters of gasoline to your car at the gas station of the city $x$. Of course, you have a choice not to visit some of the gas stations in your travel. But you will not supply gasoline twice or more at the same gas station, because it is redundant. Each road in the kingdom has a distance between two cities: the distance of $i$-th road is $d_i$ km. You will not pass the same city or the same road twice or more, of course, because it is redundant.

If a quantity of stored gasoline becomes zero, the car cannot move, and hence your travel will end there. But then, you may concern about an initially empty tank. Don't worry. You can start at any gas station of the cities in the kingdom. Furthermore, each road directly connects the gas stations of the its two ends (because the spirit of non-redundancy avoids redundant moves in a city), you therefore can supply gasoline to your car even if your car tank becomes empty just when you arrive the city.

Your task is to write a program computing the maximum number of cities so that you can travel under your non-redundancy policy.

Input

The input consists of a single test case.

$N$

$g_1$ $g_2$ ... $g_N$

$a_1$ $b_1$ $d_1$

$a_2$ $b_2$ $d_2$

...

$a_{N-1}$ $b_{N-1}$ $d_{N-1}$

The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of cities in JAG kingdom. The second line contains $N$ integers: the $i$-th of them is $g_i$ ($1 \leq g_i \leq 10,000$), the amount of gasoline can be supplied at the gas station of the city $i$. The following $N - 1$ lines give information of roads: the $j$-th line of them contains $a_j$ and $b_j$ , which indicates that the $j$-th road bidirectionally connects the cities $a_j$ and $b_j$ ($1 \leq a_j, b_j \leq N, a_j \ne b_j$) with distance $d_j$ ($1 \leq d_j \leq 10,000$). You can assume that all cities in the kingdom are connected by the roads.

Output

Print the maximum number of cities you can travel from any city under the constraint such that you can supply gasoline at most once per a gas station.

Sample Input 1

5
5 8 1 3 5
1 2 4
2 3 3
2 4 3
1 5 7

Output for the Sample Input 1

4

Sample Input 2

2
10 1
1 2 10

Output for the Sample Input 2

2

Sample Input 3

5
1 3 5 1 1
1 2 5
2 3 3
2 4 3
1 5 5

Output for the Sample Input 3

3

Solution in C++11 (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

struct edge
{
  int to, cost;
};

int N, G[100000];
vector< edge > g[100000];
int sub[100000];
bool v[100000];
int ans;


int build_dfs(int idx, int par)
{
  sub[idx] = 1;
  for(auto &e : g[idx]) {
    if(e.to == par || v[e.to]) continue;
    sub[idx] += build_dfs(e.to, idx);
  }
  return (sub[idx]);
}

pair< int, int > search_centroid(int idx, int par, const int all)
{
  pair< int, int > ret(INF, -1);
  int connect = all - sub[idx];
  for(auto &e : g[idx]) {
    if(e.to == par || v[e.to]) continue;
    ret = min(ret, search_centroid(e.to, idx, all));
    connect = max(connect, sub[e.to]);
  }
  return (min(ret, make_pair(connect, idx)));
}

map< int, int > down;
queue< pair< int, int > > downupdate;

void rec(int idx, int par, int downsmall, int downsum, int upsmall, int upsum, int length)
{
  upsum -= G[idx];
  upsmall = min(upsmall, upsum);
  downsmall = min(downsmall, downsum);
  downupdate.emplace(downsmall, length);


  if(downsmall >= 0) {
    ans = max(ans, length + 1);
  }
  if(upsmall <= upsum) {
    auto p = down.lower_bound(upsum);
    if(p != down.end()) ans = max(ans, p->second + length + 1);
  }

  downsum += G[idx];
  for(auto &e : g[idx]) {
    if(e.to == par || v[e.to]) continue;
    rec(e.to, idx, downsmall, downsum - e.cost, upsmall, upsum + e.cost, length + 1);
  }
}

void beet(int idx)
{
  for(int i = 0; i < 2; reverse(begin(g[idx]), end(g[idx])), i++) {
    down.clear();
    for(auto &e : g[idx]) {
      if(e.to == idx || v[e.to]) continue;
      rec(e.to, idx, INF, G[idx] - e.cost, INF, e.cost, 1);
      while(!downupdate.empty()) {
        int p, q;
        tie(p, q) = downupdate.front();
        downupdate.pop();

        if(down.empty()) {
          down[p] = q;
          continue;
        }

        auto it = down.upper_bound(p);
        if(it != down.begin()) {
          --it;
          if(it->second >= q) continue;
        }
        down[p] = q;
        it = down.lower_bound(p);
        ++it;
        while(it != down.end() && it->second <= q) {
          it = down.erase(it);
        }
      }
    }
  }
}

int centroid_decomp(int idx)
{
  int centroid = search_centroid(idx, -1, build_dfs(idx, -1)).second;
  beet(centroid);
  v[centroid] = true;
  for(auto &e : g[centroid]) {
    if(v[e.to]) continue;
    centroid_decomp(e.to);
  }
}

int main()
{
  scanf("%d", &N);
  for(int i = 0; i < N; i++) {
    scanf("%d", &G[i]);
  }
  for(int i = 1; i < N; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    --a, --b;
    g[a].emplace_back((edge) {b, c});
    g[b].emplace_back((edge) {a, c});
  }
  centroid_decomp(0);
  printf("%d\n", max(1, ans));
}