ICM Technex 2018 and Codeforces Round 463 (Div. 1 + Div. 2, combined)
F
Escape Through Leaf
Codeforces
2700
原题
← EG →
翻译中…

You are given a tree with $*n*$ nodes (numbered from $1$ to $*n*$) rooted at node $1$. Also, each node has two values associated with it. The values for $*i*$-th node are $*a*_{*i*}$ and $*b*_{*i*}$.

翻译中…

You can jump from a node to any node in its subtree. The cost of one jump from node $*x*$ to node $*y*$ is the product of $*a*_{*x*}$ and $*b*_{*y*}$. The total cost of a path formed by one or more jumps is sum of costs of individual jumps. For every node, calculate the minimum total cost to reach any leaf from that node. Pay attention, that root can never be leaf, even if it has degree $1$.

翻译中…

Note that you cannot jump from a node to itself.

翻译中…

Input

翻译中…

The first line of input contains an integer $*n*$ ($2 ≤ *n* ≤ 10^{5}$) — the number of nodes in the tree.

翻译中…

The second line contains $*n*$ space-separated integers $*a*_{1},  *a*_{2},  ...,  *a*_{*n*}( - 10^{5}  ≤  *a*_{*i*}  ≤  10^{5})$.

翻译中…

The third line contains $*n*$ space-separated integers $*b*_{1},  *b*_{2},  ...,  *b*_{*n*}( - 10^{5}  ≤  *b*_{*i*}  ≤  10^{5})$.

翻译中…

Next $*n*  -  1$ lines contains two space-separated integers $*u*_{*i*}$ and $*v*_{*i*}$ ($1 ≤ *u*_{*i*},  *v*_{*i*} ≤  *n*$) describing edge between nodes $*u*_{*i*}$ and $*v*_{*i*}$ in the tree.

翻译中…

Output

翻译中…

Output $*n*$ space-separated integers, $*i*$-th of which denotes the minimum cost of a path from node $*i*$ to reach any leaf.

翻译中…

Note

翻译中…

In the first example, node $3$ is already a leaf, so the cost is $0$. For node $2$, jump to node $3$ with cost $*a*_{2} × *b*_{3} = 50$. For node $1$, jump directly to node $3$ with cost $*a*_{1} × *b*_{3} = 10$.

翻译中…

In the second example, node $3$ and node $4$ are leaves, so the cost is $0$. For node $2$, jump to node $4$ with cost $*a*_{2} × *b*_{4} = 100$. For node $1$, jump to node $2$ with cost $*a*_{1} × *b*_{2} =  - 400$ followed by a jump from $2$ to $4$ with cost $*a*_{2} × *b*_{4} = 100$.

翻译中…

样例

Input #1
3
2 10 -1
7 -7 5
2 3
2 1
Output #1
10 50 0
InputCopy OutputCopy
Input #2
4
5 -10 5 7
-8 -80 -3 -10
2 1
2 4
1 3
Output #2
-300 100 0 0
InputCopy OutputCopy
题目信息
题号
932F
时间限制
3 s
内存限制
256 MB
标签
数据结构
动态规划
计算几何

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