実行時間制限: 2 sec / メモリ制限: 1024 MB
配点 : 点
問題文
頂点からなる木が与えられます。頂点は から までの番号がついており、 番目の辺は頂点 を結んでいます。
長さ の正整数列 が与えられます。 を頂点 の間にある辺の数とし、 に対して とします。 を求めてください。
制約
- 与えられるグラフは木である。
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを一行に出力せよ。
入力例 1Copy
4 1 2 1 3 2 4 1 1 1 2
出力例 1Copy
5
例として、 を求めることを考えます。 です。
よって、 となります。
同様に、 です。 が最小なので 5
を出力します。
入力例 2Copy
2 2 1 1 1000000000
出力例 2Copy
1
で、これが最小です。
入力例 3Copy
7 7 3 2 5 2 4 3 1 3 6 2 1 2 7 6 9 3 4 6
出力例 3Copy
56
Score: points
Problem Statement
You are given a tree with vertices. The vertices are numbered to , and the -th edge connects vertices and .
You are also given a sequence of positive integers of length . Let be the number of edges between vertices and , and for , let . Find .
Constraints
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
Output
Print the answer in one line.
Sample Input 1Copy
4 1 2 1 3 2 4 1 1 1 2
Sample Output 1Copy
5
For example, consider calculating . We have .
Thus, .
Similarly, . Since is the minimum, print 5
.
Sample Input 2Copy
2 2 1 1 1000000000
Sample Output 2Copy
1
, which is the minimum.
Sample Input 3Copy
7 7 3 2 5 2 4 3 1 3 6 2 1 2 7 6 9 3 4 6
Sample Output 3Copy
56