Leon is speedrunning a video game, intending to reach all of the possible endings.
The entire game can be mapped-out as a rooted tree of checkpoints connected by one-way routes. Route connects checkpoint to , and playing through this route takes exactly seconds. Let checkpoint represent the beginning of the game; all other checkpoints are reachable through some set of particular routes.
Note that a checkpoint that branches-off into multiple other checkpoints represents a point in the game where Leon must make a key decision that will alter the course of the game. Furthermore, a checkpoint that leads to nowhere else (a dead-end) represents an ending.
The game also features a built-in save/load mechanic. Leon is allowed to save the game only at checkpoints, which keeps track of his current point in the game. He may choose to load the save file at any time, immediately bringing him back to the saved checkpoint. Additionally, he also has the power to reset the game at any time, which instantly takes him all the way back to the beginning (checkpoint ).
This save/load mechanic would have made his play-through a lot easier if it weren't for the game's inconsiderate developers - only providing one save slot. If Leon plays the game optimally, what would the minimum time taken to complete the game (i.e. visit all the checkpoints) be?
The first line contains integer : the number of checkpoints in the game.
The following lines each contain three integers: , , and . This represents a one-way route taking seconds to play from checkpoint to .
A single integer: the minimum amount of time required to complete the entire game.
Sample Input 1
7 1 2 1 1 3 1 2 4 1 2 5 1 3 6 1 3 7 1
Sample Output 1
Sample Input 2
15 1 2 2 1 3 1 2 4 1 2 5 1 3 6 2 3 7 2 4 8 2 4 9 2 5 10 2 5 11 2 6 12 1 6 13 1 7 14 1 7 15 1
Sample Output 2