Mid-dura
Rory is travelling from Adelaide to Mildura for the holidays, but he doesn't particularly like Mildura.
As such, he would like to travel as slowly as possible, making stops along the way. To do so, he will find places where he can plausibly stall his journey, such as stopping in a town with a tyre shop along to the way because his car "broke down", or a popular tourist destination because "it was on the way and he had to go see it". At different destinations, he will be able to stall for different amounts of time.
Unfortunately, Rory knows that if he stops more than three times, his family will get suspicious.
Given all of the stall destinations and all of the roads, can you find the longest amount of time he can stall for, making sure his family doesn't get suspicious?
Input
The first line consists of a single integer
, the number of towns that may be passed through.
The next lines each consist of a string
, a given destination's name, and an integer
, how long he can stall at for this particular location.
All inputs will contain the following:
adelaide 0
mildura 0
The next line consists of a single integer
, the number of roads between these towns.
The next lines are of the format
, the names of two towns. This indicates that there is a directed road from town
to
.
There is guaranteed to be no cycles in the road network, where all towns are reachable starting from adelaide, and all roads eventually lead to mildura.
Output
Output a single integer, the maximum amount of time Rory can stall for by stopping at no more than three towns on the way from adelaide to mildura.
Clarifications
- All town names only consist of uppercase letters, lowercase letters, and numbers, and are between
and
characters long, inclusive.
- There will be no duplicated roads between any two towns, cycles, or unconnected components.
- Rory can stop less than
times on the way from Adelaide to Mildura if he likes.
Examples
Input 1
7
adelaide 0
mildura 0
hanhdorf 12
berri 6
loxton 10
rorytown 3
roryville 2
9
adelaide hanhdorf
adelaide berri
berri loxton
berri roryville
hanhdorf roryville
loxton rorytown
loxton mildura
rorytown mildura
roryville mildura
Output 1
19
Explanation
Rory can stall for the longest amount of time by going from adelaide -> berri -> loxton -> rorytown -> mildura, stalling in berri, loxton, and rorytown for a total time of .

Comments