Codeforces Round 202 (Div. 1)
B
Apple Tree
Codeforces
2100
原题
← AC →
翻译中…

You are given a rooted tree with $*n*$ vertices. In each leaf vertex there's a single integer — the number of apples in this vertex.

翻译中…

The weight of a subtree is the sum of all numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf is the number written in the leaf.

翻译中…

A tree is balanced if for every vertex $*v*$ of the tree all its subtrees, corresponding to the children of vertex $*v*$, are of equal weight.

翻译中…

Count the minimum number of apples that you need to remove from the tree (specifically, from some of its leaves) in order to make the tree balanced. Notice that you can always achieve the goal by just removing all apples.

翻译中…

Input

翻译中…

The first line contains integer $*n*$ $(2 ≤ *n* ≤ 10^{5})$, showing the number of vertices in the tree. The next line contains $*n*$ integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ $(0 ≤ *a*_{*i*} ≤ 10^{8})$, $*a*_{*i*}$ is the number of apples in the vertex number $*i*$. The number of apples in non-leaf vertices is guaranteed to be zero.

翻译中…

Then follow $*n* - 1$ lines, describing the tree edges. Each line contains a pair of integers $*x*_{*i*}, *y*_{*i*}$ ($1 ≤ *x*_{*i*}, *y*_{*i*} ≤ *n*, *x*_{*i*} ≠ *y*_{*i*}$) — the vertices connected by an edge.

翻译中…

The vertices are indexed from 1 to $*n*$. Vertex 1 is the root.

翻译中…

Output

翻译中…

Print a single integer — the minimum number of apples to remove in order to make the tree balanced.

翻译中…

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the sin, cout streams cin, cout or the %I64d specifier.

翻译中…

样例

Input #1
6
0 0 12 13 5 6
1 2
1 3
1 4
2 5
2 6
Output #1
6
InputCopy OutputCopy
题目信息
题号
348B
时间限制
2 s
内存限制
256 MB
标签
DFS 及类似
数论
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。