{"problem":{"name":"Minimize Sum of Distances","description":{"content":"You are given a tree with $N$ vertices. The vertices are numbered $1$ to $N$, and the $i$\\-th edge connects vertices $A_i$ and $B_i$. You are also given a sequence of positive integers $C = (C_1, C_2,","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc348_e"},"statements":[{"statement_type":"Markdown","content":"You are given a tree with $N$ vertices. The vertices are numbered $1$ to $N$, and the $i$\\-th edge connects vertices $A_i$ and $B_i$.\nYou are also given a sequence of positive integers $C = (C_1, C_2, \\ldots ,C_N)$ of length $N$. Let $d(a, b)$ be the number of edges between vertices $a$ and $b$, and for $x = 1, 2, \\ldots, N$, let $\\displaystyle f(x) = \\sum_{i=1}^{N} (C_i \\times d(x, i))$. Find $\\displaystyle \\min_{1 \\leq v \\leq N} f(v)$.\n\n## Constraints\n\n*   $1 \\leq N \\leq 10^5$\n*   $1 \\leq A_i, B_i \\leq N$\n*   The given graph is a tree.\n*   $1 \\leq C_i \\leq 10^9$\n\n## Input\n\nThe input is given from Standard Input in the following format:\n\n$N$\n$A_1$ $B_1$\n$A_2$ $B_2$\n$\\vdots$\n$A_{N - 1}$ $B_{N - 1}$\n$C_1$ $C_2$ $\\cdots$ $C_N$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc348_e","tags":[],"sample_group":[["4\n1 2\n1 3\n2 4\n1 1 1 2","5\n\nFor example, consider calculating $f(1)$. We have $d(1, 1) = 0, d(1, 2) = 1, d(1, 3) = 1, d(1, 4) = 2$.  \nThus, $f(1) = 0 \\times 1 + 1 \\times 1 + 1 \\times 1 + 2 \\times 2 = 6$.\nSimilarly, $f(2) = 5, f(3) = 9, f(4) = 6$. Since $f(2)$ is the minimum, print `5`."],["2\n2 1\n1 1000000000","1\n\n$f(2) = 1$, which is the minimum."],["7\n7 3\n2 5\n2 4\n3 1\n3 6\n2 1\n2 7 6 9 3 4 6","56"]],"created_at":"2026-03-03 11:01:14"}}