var cnts: []int func main() var n: int :: cui@inputInt() var q: int :: cui@inputInt() var g: []list :: #[n]list for i(0, n - 1) do g[i] :: #list end for for(1, n - 1) var a: int :: cui@inputInt() - 1 var b: int :: cui@inputInt() - 1 do g[a].add(b) do g[b].add(a) end for do @cnts :: #[n]int do bfs(g, 0, -1) var ans: int :: 0 for(1, q) var p: int :: cui@inputInt() - 1 var x: int :: cui@inputInt() do ans :+ @cnts[p] * x do cui@print("\{ans}\n") end for func bfs(g: []list, current: int, parent: int): int var gc: list :: g[current] do gc.head() var cnt: int :: 1 for i(0, ^gc - 1) var neighbor: int :: gc.get() do gc.next() if(neighbor = parent) skip i end if do cnt :+ bfs(g, neighbor, current) end for do @cnts[current] :: cnt ret cnt end func end func