Woburn Challenge 2016-17 Round 3 - Senior Division
In the world of Pokémon Navy Green, there are towns, with routes running amongst them. At the beginning of the game, you find yourself in town with a starting Pokémon of your choice – a cute level Pyglion! Your objective is to reach town by following a sequence of routes.
The -th route connects distinct towns and , and can be walked along in either direction. No pair of towns are directly connected by multiple routes. However, the routes are also crawling with dangerous Pokémon. As such, you can only dare venture along the -th route if your Pyglion's level is currently no smaller than .
As mentioned, your Pyglion's level is initially , but it can be increased through intensive training. Each town has its own training gym for that purpose. However, some gyms are more efficient than others – in particular, in the -th town, it takes minutes of training to increase your Pyglion's level by . This –minute process can be repeated as many times as you'd like.
You'd hate to tucker out your poor Pyglion more than necessary, so you'd like to reach town after spending as little total time training in gyms as possible. Note that time spent walking along routes isn't relevant, and that you may revisit towns on your adventure as often as you'd like. Please determine the minimum number of minutes of training required to reach town , or determine that you can't complete your trip no matter how much training you put your Pyglion through.
In test cases worth of the points, , , and .
Input Specification
The first line of input consists of two space-separated integers and
.
lines follow, with the -th of these lines consisting of a single
integer, (for ).
lines follow, with the -th of these lines consisting of three
space-separated integers , , and , (for ).
Output Specification
Output one line consisting of a single integer – the minimum number of
minutes of training required to reach town , or -1
if it can't be
done.
Note that the answer may not necessarily fit within a -bit signed
integer (you may need the long long
type in C++, or long
in Java).
Sample Input
6 8
14
5
8
10
2
4
1 4 5
1 2 8
4 5 12
3 1 2
6 3 11
2 3 14
5 6 4
2 4 6
Sample Output
71
Sample Explanation
One optimal sequence of actions is as follows:
- Train in town for minutes (up to level ).
- Walk to town .
- Train in town for minutes (up to level ).
- Walk to town .
- Walk to town .
- Walk to town .
- Train in town for minutes (up to level ).
- Walk to town .
- Walk to town .
- Walk to town .
Comments