Codeforces Round 819 (Div. 1 + Div. 2) and Grimoire of Code Annual Contest 2022
A
Mainak and Array
Codeforces
900
原题
←B →
翻译中…

Mainak has an array $a_1, a_2, \ldots, a_n$ of $n$ 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 $l$ and $r$, such that $1 \le l \le r \le n$, and any positive integer $k$.
  • Repeat this $k$ 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 $(a_n - a_1)$ after exactly one such operation. Determine the maximum value of $(a_n - a_1)$ that he can obtain.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \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 $n$ ($1 \le n \le 2000$).

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 999$).

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the maximum value of $(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 $3$ to index $6$ by an amount of $2$ (i.e. choose $l = 3$, $r = 6$ and $k = 2$) to get the optimal array: $$[1, 3, \underline{9, 11, 5, 7}] \longrightarrow [1, 3, \underline{5, 7, 9, 11}]$$ So the answer is $a_n - a_1 = 11 - 1 = 10$.
  • In the second testcase, it is optimal to rotate the subarray starting and ending at index $1$ and rotating it by an amount of $2$.
  • In the fourth testcase, it is optimal to rotate the subarray starting from index $1$ to index $4$ and rotating it by an amount of $3$. So the answer is $8 - 1 = 7$.
翻译中…

样例

Input #1
5
6
1 3 9 11 5 7
1
20
3
9 99 999
4
2 1 8 1
3
2 1 5
Output #1
10
0
990
7
4
InputCopy OutputCopy
题目信息
题号
1726A
时间限制
1 s
内存限制
256 MB
标签
贪心
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。