CF1726A.Mainak and Array

传统题 时间 2000 ms 内存 256 MiB 4 尝试 1 已通过 1 标签

Mainak and Array

Mainak has an array a1,a2,,ana_1, a_2, \ldots, a_n of nn positive integers. He will do the following operation to this array exactly once:

  • Pick a subsegment of this array and cyclically rotate it by any amount.

Formally, he can do the following exactly once:

  • Pick two integers ll and rr, such that 1lrn1 \le l \le r \le n, and any positive integer kk.
  • Repeat this kk times: set $a_l=a_{l+1}, a_{l+1}=a_{l+2}, \ldots, a_{r-1}=a_r, a_r=a_l$ (all changes happen at the same time).

Mainak wants to maximize the value of (ana1)(a_n - a_1) after exactly one such operation. Determine the maximum value of (ana1)(a_n - a_1) that he can obtain.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1t501 \le t \le 50) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (1n20001 \le n \le 2000).

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai9991 \le a_i \le 999).

It is guaranteed that the sum of nn over all test cases does not exceed 20002000.

Output

For each test case, output a single integer — the maximum value of (ana1)(a_n - a_1) that Mainak can obtain by doing the operation exactly once.

Note

  • In the first test case, we can rotate the subarray from index 33 to index 66 by an amount of 22 (i.e. choose l=3l = 3, r=6r = 6 and k=2k = 2) to get the optimal array: $$[1, 3, \underline{9, 11, 5, 7}] \longrightarrow [1, 3, \underline{5, 7, 9, 11}]$$So the answer isa_na_1=111=10a\_n - a\_1 = 11 - 1 = 10.
  • In the second testcase, it is optimal to rotate the subarray starting and ending at index11and rotating it by an amount of22.
  • In the fourth testcase, it is optimal to rotate the subarray starting from index11to index44and rotating it by an amount of33. So the answer is81=78 - 1 = 7.

Samples

5
6
1 3 9 11 5 7
1
20
3
9 99 999
4
2 1 8 1
3
2 1 5
10
0
990
7
4

在线编程 IDE

建议全屏模式获得最佳体验