#C748. Mountain Hopping: Minimizing Coins

    ID: 51355 Type: Default 1000ms 256MiB

Mountain Hopping: Minimizing Coins

Mountain Hopping: Minimizing Coins

You are given a sequence of mountains represented by their heights. You start at the first mountain and want to reach the last mountain by making a series of jumps. However, you are only allowed to jump from the current mountain to a mountain with a height greater than or equal to the current mountain.

For each valid jump from mountain i to mountain j (with heights \(h_i\) and \(h_j\) respectively and \(h_j \ge h_i\)), you pay a cost of \(h_j - h_i\) coins. At each step, you must choose the jump that minimizes this cost. If there is no valid jump (i.e. no mountain ahead with a height at least the current mountain), then you cannot make any jump and the cost remains unchanged (effectively, the journey ends without further expense).

Your task is to compute the minimum total number of coins needed to jump from the first mountain to the last mountain for each test case.

Note: If the journey cannot continue because no valid jump exists, assume the cost does not increase further.

inputFormat

The first line contains an integer \(T\) denoting the number of test cases. For each test case, the first line contains an integer \(n\) denoting the number of mountains. The second line contains \(n\) space-separated integers \(h_1, h_2, ..., h_n\) representing the heights of the mountains in order.

Input Format:

T
n
h_1 h_2 ... h_n
... (repeat for T test cases)

outputFormat

For each test case, output a single line containing the minimum number of coins required to reach the last mountain. If it is not possible to proceed, output the coins accumulated thus far (which will be 0 if no jump is possible).

## sample
1
5
1 3 5 2 4
3

</p>