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

18638번 - The Older We Are, The Worse It Hurts 다국어

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

문제

You are given a tree with n vertices. Each vertex i has a weight ai.

You traverse the whole tree starting in an arbitrary vertex and moving along the edges so that each edge is traversed exactly once in each direction (in other words, you perform a depth-first search traversal choosing the initial vertex and the order of outgoing edges arbitrarily). Write down the list of all vertices, (v1, v2, . . . , vn), sorted by the time when you first arrive at them. You get a penalty of ∑i · avi.

Your goal is to minimize the penalty. Note that (v1, v2, . . . , vn) is a permutation of (1, 2, . . . , n), and v1 is the vertex you start from.

입력

The first line contains the only integer n (1 ≤ n ≤ 200 000) denoting the number of vertices. The next n−1 lines contain edges descriptions: i-th of them contains two integers ui and vi (1 ≤ ui, vi ≤ n) denoting the edge between ui and vi. The third line contains n space-separated integers ai (1 ≤ ai ≤ 200 000).

It is guaranteed that the given edges represent a tree.

출력

Print a single line with a single integer on it: the minimum possible penalty.

제한

예제 입력 1

3
1 2
1 3
1 2 3

예제 출력 1

11

예제 입력 2

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

예제 출력 2

35

힌트

출처

Camp > Petrozavodsk Programming Camp > Summer 2019 > Day 6: MIPT Contest I번

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

출처

대학교 대회

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

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