Non-redundant Drive
Time limit2sMemory limit512 MB
In a tree where each node gives g fuel and each edge costs d, find the longest simple path from any start such that the running fuel never drops below zero, refueling once per node.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
The people of the JAG kingdom hate redundancy. For example, the cities of the kingdom are connected by exactly bidirectional roads, and every city is reachable from every other city by some roads. Under this condition, the number of paths between any two cities is exactly one. This is a non-redundant road network.
One day you, a citizen of the JAG kingdom, decide to travel through as many cities of the kingdom as possible by car. The car has an infinitely large tank, but the tank is empty at first. The car consumes 1 liter of gasoline per 1 km.
Each city has exactly one gas station, and the gas station of city supplies liters of gasoline to your car. You may choose not to visit some gas stations during the travel. But you never refuel twice or more at the same gas station, because that is redundant. Each road of the kingdom has a distance between its two cities, and the distance of the -th road is km. You never pass through the same city or the same road twice or more either, because that is redundant.
If the amount of stored gasoline becomes zero, the car cannot move, and the travel ends there. The initially empty tank is not a problem: you can start at the gas station of any city in the kingdom. Also, each road directly connects the gas stations at its two ends (the spirit of non-redundancy avoids redundant moves inside a city), so you can refuel at a city even if the tank becomes empty exactly when you arrive there.
Write a program that computes the maximum number of cities you can travel through under this non-redundancy policy.
Input
The input consists of a single test case.
N
g1 ... gN
a1 b1 d1
...
aN-1 bN-1 dN-1
The first line contains an integer (), the number of cities in the kingdom. The second line contains integers; the -th of them is (), the amount of gasoline the gas station of city supplies. The following lines describe the roads: the -th of them contains , , and , meaning that the -th road bidirectionally connects city and city (, ) with distance (). All cities of the kingdom are connected by the roads.
Output
Print the maximum number of cities you can travel through, starting from any city, under the constraint that you refuel at most once per gas station.