Funny Salesman

시간 제한1초메모리 제한512 MB

요약
가중치가 30 이하인 간선을 가진 트리에서 모든 정점을 한 번씩 나열해 연속한 두 정점 사이 경로의 최대 간선 가중치에 대한 2의 거듭제곱 합을 최대로 만든다.
난이도

보통10점 중 7점

유형
그리디, 트리, DFS, 정렬
정답자
아직 제출이 없습니다

문제

You are given a tree, and each edge has a non-negative integer weight.

Let d(u,v)d(u, v) --- The maximum of the edge weights on the unique simple path between vertices uu and vv.

Find the largest ∑_i=2n2d(p_i−1,p_i)\sum\_{i=2}^n{2^{d(p\_{i - 1}, p\_i)}} among all permutations of vertices p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n.

입력

The first line contains one integer nn (2≤n≤100,0002 \leq n \leq 100\\,000): the number of vertices in the tree.

Each of the next n−1n-1 lines contains three integers u,v,wu,v,w (1≤u,v≤n,0≤w≤301 \leq u, v \leq n, 0 \leq w \leq 30), an edge in the tree with endpoints u,vu,v having weight ww.

출력

Print one integer: the largest ∑_i=2n2d(p_i−1,p_i)\sum\_{i=2}^n{2^{d(p\_{i - 1}, p\_i)}}.

힌트

In the first example, one of the optimal permutations is 4,5,3,2,1\\{4, 5, 3, 2, 1\\}.

예제2

  1. 예제 1

    입력
    5
    1 2 0
    2 3 0
    3 4 0
    4 5 1
    
    예상 출력
    6
    
  2. 예제 2

    입력
    10
    2 1 1
    3 1 1
    1 4 0
    5 1 2
    6 4 1
    2 7 2
    8 4 2
    8 9 3
    6 10 0
    
    예상 출력
    42