Company: D. E. Shaw
Difficulty: medium
Collect Values at the Root of a Tree An undirected tree has vertices numbered from `1` to `n`; every vertex initially has value `1`. In one operation, move an amount from `1` through `k` inclusive from a vertex to a directly adjacent vertex. Find the minimum operations needed to make every vertex except vertex `1` equal to `0`, and vertex `1` equal to `n`. Input Format The first line contains `k`. The second line contains `n` and `n-1`. The following `n-1` lines each contain an undirected edge. Output Format Print the minimum number of operations. Example For `k = 3` and edges `(2,1),(2,6),(2,3),(3,5),(4,3)`, the answer is `6`. Notes Root the tree at vertex `1`. Every unit in a child subtree must cross its edge to the parent; one operation can carry at most `k` such units.