E - Tree and Hamilton Path 2 Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

AtCoder国には 11 から NN の番号がついた NN 個の街と 11 から N1N-1 の番号がついた N1N-1 本の道路があります。

道路 ii は街 AiA_i と街 BiB_i を双方向に結び、長さは CiC_i です。どの街同士も、いくつかの道路を通って互いに行き来することができます。

いずれかの街を出発し、道路による移動で全ての街を 11 度以上訪れるための移動距離の最小値を求めてください。

制約

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • 1Ci1091 \leq C_i \leq 10^9
  • 入力は全て整数である
  • どの街同士も、いくつかの道路を通って互いに行き来できる

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 B1B_1 C1C_1
\vdots
AN1A_{N-1} BN1B_{N-1} CN1C_{N-1}

出力

答えを出力せよ。


入力例 1Copy

Copy
4
1 2 2
1 3 3
1 4 4

出力例 1Copy

Copy
11

412134 \to 1 \to 2 \to 1 \to 3 と移動すると移動距離の合計は 1111 となり、これが最小値です。

最初の街に戻ってくる必要はないことに注意してください。


入力例 2Copy

Copy
10
10 9 1000000000
9 8 1000000000
8 7 1000000000
7 6 1000000000
6 5 1000000000
5 4 1000000000
4 3 1000000000
3 2 1000000000
2 1 1000000000

出力例 2Copy

Copy
9000000000

オーバーフローに注意してください。

Score : 500500 points

Problem Statement

In the nation of AtCoder, there are NN cities numbered 11 to NN and N1N-1 roads numbered 11 to N1N-1.

Road ii connects cities AiA_i and BiB_i bidirectionally, and its length is CiC_i. Any pair of cities can be reached from each other by traveling through some roads.

Find the minimum travel distance required to start from a city and visit all cities at least once using the roads.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • 1Ci1091 \leq C_i \leq 10^9
  • All input values are integers.
  • Any pair of cities can be reached from each other by traveling through some roads.

Input

The input is given from Standard Input in the following format:

NN
A1A_1 B1B_1 C1C_1
\vdots
AN1A_{N-1} BN1B_{N-1} CN1C_{N-1}

Output

Print the answer.


Sample Input 1Copy

Copy
4
1 2 2
1 3 3
1 4 4

Sample Output 1Copy

Copy
11

If you travel as 412134 \to 1 \to 2 \to 1 \to 3, the total travel distance is 1111, which is the minimum.

Note that you do not need to return to the starting city.


Sample Input 2Copy

Copy
10
10 9 1000000000
9 8 1000000000
8 7 1000000000
7 6 1000000000
6 5 1000000000
5 4 1000000000
4 3 1000000000
3 2 1000000000
2 1 1000000000

Sample Output 2Copy

Copy
9000000000

Beware overflow.



2025-04-27 (Sun)
22:26:13 +00:00