#K61952. Vegeta's Obstacle Energy Challenge

    ID: 31423 Type: Default 1000ms 256MiB

Vegeta's Obstacle Energy Challenge

Vegeta's Obstacle Energy Challenge

Vegeta is faced with a series of obstacles and must calculate the total energy required to jump over them. The energy consumption depends on the change in height between consecutive obstacles. If the next obstacle is higher than the current one, the energy cost is the square of the height difference. Otherwise, the energy cost is simply the difference in height.

Formally, given a sequence of obstacle heights \(h_1, h_2, \dots, h_N\), the total energy required is computed as:

[ E = \sum_{i=1}^{N-1} \begin{cases} (h_{i+1} - h_i)^2 & \text{if } h_{i+1} > h_i, \ h_i - h_{i+1} & \text{otherwise.} \end{cases} ]

You will be given \(T\) test cases. For each test case, the first number is \(N\), the number of obstacles, followed by \(N\) integers representing the obstacle heights in order. Your task is to compute and output the energy required for each test case.

inputFormat

The first line of input contains a single integer \(T\) indicating the number of test cases. Each test case is described as follows:

  • The first line contains an integer \(N\) representing the number of obstacles.
  • The second line contains \(N\) space-separated integers denoting the heights of the obstacles.

Input should be read from standard input (stdin).

outputFormat

For each test case, output the total energy required on a separate line. The answer for each test case should be written to standard output (stdout).

## sample
2
4
5 2 7 3
3
4 1 4
32

12

</p>