SP4669 CCROSSX - Cross Mountain Climb Extreme
Description
Somewhere in the neighborhood we have a very nice mountain that gives a splendid view over the surrounding area. There is one problem though: climbing this mountain is very difficult, because of rather large height differences. To make more people able to climb the mountain and enjoy the view, we would like to make the climb easier. To do so, we will model the mountain as follows: the mountain consists of n adjacent stacks of stones, and each of the stacks is h $ _{i} $ high. The successive height differences are therefore h $ _{i+1} $ − h $ _{i} $ (for 1
We can do this by increasing or decreasing the height of some of the stacks. The first stack (the starting point) and the last stack (the ending point) should remain at the same height as they are initially. Since adding and removing stones requires a lot of effort, we would like to minimize the total number of added stones plus the total number of removed stones. What is this minimum number?
Input Format
On the first line one positive number: the number of test cases, at most 50. After that, for each test case:
- One line with two integers n (2
- One line with n integers hi (0
Output Format
For each test case, output one line with the minimum number of stones that have to be added or removed, or “impossible” if it is impossible to achieve the goal.