Pony-less Express
문제
Howdy, pardner! You've been put in charge of mail delivery in these here parts. Your headquarters is in Capital City, and Miss Penelope has asked us to tell all of the local farmsteads about her fashionable doin's. The roads hereabouts were built so that each farmstead connects to Capital City by only one set of roads, which may connect through multiple other farmsteads. Each road takes exactly one day to travel on a horse.
Each farmstead wants to receive news on their own specific timetable (so as not to distract the hired hands from their work), and gets peeved if the delivery misses the desired date, either early or late. Specifically, each farmstead has a perfect date for receiving news. If the news arrive on day , the anger level at the farmstead is . Obviously, we want to minimize the total anger level over all the farmsteads, so we need to get on our horses and send this important information to each farmstead as close to their desired date as possible! The only problem is ... well ... we actually don't HAVE any horses. Instead, we can requisition exactly one horse from each location (farmstead or Capital City) each day, ride it for day, and let it return to its starting location at the end of the day (don't worry, these horses know how to find their way back). The next day, another rider can take that horse and travel to a different location (if necessary). This process repeats in all of the farmsteads as well as Capital City.
Oh, and one more thing: we ain't payin' riders to sit idle at any farmstead, gittin' into who knows what kind of trouble. So if news arrives at farmstead on day and there are roads leading to other farmsteads that have not received any news yet, a horse will leave farmstead on each day until every neighboring city has been visited, even if waiting extra days might lead to lower anger levels for some of the farmsteads.
For example, consider the layout of farmsteads shown in Figure K.1 (corresponding to Sample Input ), where the values are shown to the left of each farmstead. The optimal way to deliver the news is as follows:
- Day : send a horse from Capital City to farmstead
- Day : send a horse from Capital City to farmstead and a horse from farmstead to farmstead
- Day : send a horse from Capital City to farmstead , a horse from farmstead to farmstead and a horse from farmstead to farmstead
- Day : send a horse from farmstead to farmstead
The total anger level, adding up from farmstead to , is .
Figure K.1: Example layout.
Can you help us figure out just how angry people will be in total, so we know what we're in fer?
출력
The first line of input contains a positive integer indicating the number of farmsteads, numbered to , with Capital City labeled . Following this are lines each containing three integers , where the of these lines specify the and values for the farmstead and indicates that there is a road from farmstead to farmstead (or Capital City if ). The roads are set up so that there is a unique path between any two farmsteads, and between each farmstead and Capital City.
예제
예제 1
7 3 1 0 3 2 0 2 2 0 2 4 1 1 6 1 3 3 3 4 1 3
18
예제 2
3 5 2 2 4 1 0 6 3 1
0