Cat Exercise
Time limit2sMemory limit1024 MB
Place obstacles on a tree of towers to maximize the total number of moves a cat makes while jumping to the highest reachable tower each time.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
There are cat towers, numbered from to . The height of Tower () is . The heights are distinct integers between and , inclusive. There are adjacent pairs of towers. For each (), Tower and Tower are adjacent. At the start, you can travel from any tower to any other tower by moving between adjacent towers.
At the start, a cat stays in the tower of height .
Then we perform cat exercises. In each exercise, we choose a tower and put an obstacle on it. A tower cannot receive a second obstacle. The following happens.
- If the cat is not in the chosen tower, nothing happens.
- If the cat is in the chosen tower and every tower adjacent to it has an obstacle, the cat exercises end.
- Otherwise, the cat moves to the highest tower, excluding its current tower, among the towers it can reach from its current tower without passing through obstacles. It takes the route with the fewest moves between adjacent towers.
Given the tower heights and the adjacency pairs, write a program that finds the maximum possible sum of the cat's moves between adjacent towers when obstacles are placed optimally.
Input
Output
Print one line containing the maximum possible sum of the cat's moves between adjacent towers.
Constraints
- .
- ().
- ().
- ().
- At the start, you can travel from any tower to any other tower through adjacent towers.
- All given values are integers.