시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
1 초 | 512 MB | 22 | 17 | 15 | 75.000% |
Treantis is a wonderful town with N junctions (numbered from 1 to N) that are connected by exactly N − 1 roads such that one can go from one junction to any other junction using a sequence of roads. Such a special structure causes Treantis to have some junctions in which each of them is only connected to exactly one other junction; such a junction is known as a cul-de-sac (or a dead-end junction) in Treantis.
To live up the town, the mayor of Treantis decides to carry out a festival in Treantis with glamorous parades. Due to some superstition in Treantis, there are two constraints that should be fulfilled when designing the routes for the parades.
The mayor has estimated that if a parade passes a road connecting junction ui and junction vi, then the citizen happiness will be increased by wi.
Your task in this problem is to help the mayor to calculate the maximum happiness that can be obtained by carefully designing the parades while satisfying the two constraints mentioned above.
For example, let N = 10 and the town structure is as shown in the following figure.
As we can see, there are 7 cul-de-sacs and their numbers are {1, 2, 3, 4, 8, 9, 10}. In this example, the maximum happiness can be obtained by running 3 parades of the following routes.
Observe that all those parades start and end at a cul-de-sac and no two parades share the same road. The total happiness of those parades is 40 + 90 + 80 = 210. There are no parades that yield a happiness of more than 210 in this example.
Input begins with a line containing an integer: N (2 ≤ N ≤ 100 000) representing the number of junctions in Treantis. The next N − 1 lines each contains three integers: ui vi wi (1 ≤ ui < vi ≤ N; 1 ≤ wi ≤ 106) representing a road and its increment in happiness if this road is passed by a parade, respectively. It is guaranteed that one can go from one junction to any other junction using a sequence of roads.
Output in a line an integer representing the maximum happiness that can be obtained by carefully designing the parades while satisfying all the constraints.
10 1 5 10 2 5 20 3 6 20 4 7 40 5 6 50 5 8 30 6 7 10 6 9 20 7 10 40
210
7 1 2 10 1 3 5 2 4 15 2 5 20 3 6 12 3 7 13
60