You are given an array of integers. Your goal is to split the array into one or more non-empty contiguous subarrays.
The value of a subarray is defined as the difference between its maximum and minimum elements.
Your task is to determine the best way to split the array to maximize the sum of these values.
Write a program which computes these optimal sums!
Input data
The first line of the input file contains a single integer , the number of test cases.
test cases follow.
Each test case consists of:
- a line containing a single integer .
- a line containing the array : .
Output data
The output file must contain lines corresponding to the test cases, each consisting of integer ,
the maximum sum of subarrays with an optimal construction.
Constraints and clarifications
- .
- for each .
- The sum of across all testcases does not exceed 200 \ 000$.
# | Points | Constraints |
---|---|---|
1 | 0 | Examples |
2 | 30 | The sum of across all testcases does not exceed 5 \ 000$. |
3 | 30 | . |
4 | 40 | No additional constraints. |
Example
stdin
7
4
2 1 4 3
5
1 2 2 1 2
6
1 3 6 2 4 5
6
1 4 6 2 5 3
10
7 1 10 9 4 2 8 5 3 6
10
3 1 4 1 5 9 2 6 5 3
6
1000000000 1 1000000000 1 1000000000 1
stdout
3
2
8
9
23
17
2999999997
Explanation
In the first testcase of the example, the value of the whole array is .
In the second testcase, splitting into and gives the total value .
In the third testcase, splitting into , and gives the total value .
It can be proven that the total values above are optimal.