Way Home
Time limit1sMemory limit1024 MB
Find the minimum number of performances needed to fly from city 1 to city n with starting money p, where each city pays a fixed amount per performance.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Greedy
- Solved
- No attempts yet
Statement
The famous magician Borya Budini was traveling through a country X made up of cities. Unfortunately, he was robbed in city 1. Now he has a long road ahead to get home to city .
He travels by plane. The country has flights. The -th flight goes from to and costs . To take a flight, Borya must be in city and have at least money on hand, which he spends on the flight.
After the robbery he has only rubles left. He does not give up, though. While in city , he can give a performance every day, and each performance earns him rubles.
Help the magician find out whether he can get home, and if so, the minimum number of performances he must give to do it.
Input
The first line contains four integers , , , and (, , , ): the number of cities, the number of flights, the initial money, and the test group number.
The second line contains integers (), the profit from one performance in each city.
Each of the next lines contains three integers , , and (, ): the departure city, the destination city, and the price of the -th flight.
Output
Print a single integer: the minimum number of performances Borya must give to get home, or if it is impossible.
Hint
In the first example, the best plan is to give 4 performances in city 1, which leaves Borya with rubles. He then flies the route , spending rubles.
In the second example, the best plan is to give 15 performances in city 1, fly to city 3, give 9 performances there, and then fly on to city 4.