Logo
(追記) (追記ここまで)

17325번 - Road Construction 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB31211071.429%

문제

On planet W, there exists n countries. To promote the economic growth of each country, the kings of the countries have decided to construct bidirectional roads to ensure that all countries are connected. However, since they're all incredibly stingy, they wish to construct exactly n − 1 roads.

Constructing each road will require a cost. This cost is equal to the length of the road multiplied by the absolute difference of the number of countries on each side of the road. For example, the road represented by a dashed line in the figure below has, respectively, 2 and 4 countries on each of its sides. If this road has a length of 1, then the cost will be 1×|2 − 4| = 2. The circled numbers represent the numbers of the countries.


Since both the number of countries and the number of ways to construct the roads are extremely large, as well the construction costs for each way is hard to calculate by humans, the kings have decided to hire a person to design a software to do this. This piece of software should be able calculate the total cost of constructing all the roads, given a way to construct them. Please help the kings to write such a program.

입력

The first line contains an integer n, representing the number countries on planet W. Countries are numbered from 1 to n.

For the following n − 1 lines, each line will describe the construction of a single road. The i-th of these lines will contain three integers ai, bi, and ci, indicating that the i-th bidirectional road connects countries ai and bi, and has a length of ci.

출력

Output a single integer, the total cost of constructing all the roads.

제한

  • 1 ≤ ai, bin
  • 2 ≤ n ≤ 106
  • 0 ≤ ci ≤ 106

예제 입력 1

6
1 2 1
1 3 1
1 4 2
6 3 1
5 2 1

예제 출력 1

20

힌트

출처

Olympiad > National Olympiad in Informatics (China) > NOI 2011 4번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /