#P5306. [COCI 2018/2019 #5] Transport

[COCI 2018/2019 #5] Transport

Description

A country has nn cities. Each city has a gas station with fuel supply aia_i.
There are n1n-1 roads connecting these cities into a tree structure.

For a truck to travel from city uu to city vv, the amount of fuel in the truck must be at least the distance between uu and vv.
Each time the truck arrives at a city, it can refuel by an amount not exceeding the fuel supply of that city's gas station.

Assume the truck's fuel tank has infinite capacity, and it consumes one unit of fuel for each kilometer traveled. Please calculate how many ordered pairs (u,v)(u,v) satisfy:

A truck whose initial fuel is 00 can start from city uu, reach city vv, and uvu \neq v.

Note that the truck can only travel along a simple path, meaning it cannot go back.

Input Format

The first line contains a positive integer nn.
The second line contains nn positive integers aia_i, representing the fuel supply of each gas station.
The next n1n-1 lines each contain three positive integers u,v,wu,v,w, indicating that there is a road of length ww between cities uu and vv.

Output Format

Output one integer in a single line, representing the answer.

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

Hint

Explanation for Sample 1:

The only feasible pair is (1,2)(1,2), meaning only 121\rightarrow 2 is feasible.

Constraints:

For 20%20\% of the testdata:
1n50001\le n \le 5000
For 40%40\% of the testdata:
The tree is a chain.
For 100%100\% of the testdata:
1n1051\le n \le 10^5
1u,vn1\le u,v \le n
1w,ai1091\le w,a_i \le 10^9

Translated by ChatGPT 5