Maximum Difference

Time limit: 0.3s Memory limit: 256MB Input: Output:


You are given an array AA of NN 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 TT, the number of test cases.
TT test cases follow.

Each test case consists of:

  • a line containing a single integer NN.
  • a line containing the array AA: A0,A1,AN1A_{0}, A_{1}, \ldots A_{N-1}.

Output data

The output file must contain TT lines corresponding to the test cases, each consisting of integer PP,
the maximum sum of subarrays with an optimal construction.

Constraints and clarifications

  • 1N200 0001 \le N \le 200 \ 000.
  • 1Ai1 000 000 0001 \le A_i \le 1 \ 000 \ 000 \ 000 for each i=0N1i=0\ldots N-1.
  • The sum of NN across all testcases does not exceed 200 \ 000$.
# Points Constraints
1 0 Examples
2 30 The sum of NN across all testcases does not exceed 5 \ 000$.
3 30 1Ai21 \le A_i \le 2.
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 41=34-1=3.

In the second testcase, splitting AA into [1,2][1, 2] and [2,1,2][2, 1, 2] gives the total value 1+1=21+1=2.

In the third testcase, splitting AA into [1,3,6][1, 3, 6], and [2,4,5][2, 4, 5] gives the total value 3+5=83+5=8.

It can be proven that the total values above are optimal.

Log in or sign up to be able to send submissions!