Codeforces Round 1085 (Div. 1 + Div. 2)
C
Where's My Water?
Codeforces
1600
原题
← BD →
翻译中…

Let $n, h$ be positive integers. Swampy the Alligator needs water for his bathtub, and you are tasked with getting it to him. Above his house, there is an $h \times n$ grid of dirt and water tiles with $n$ columns numbered $1, \ldots, n$. In column $i$, the bottom $a_i$ tiles are dirt, and the rest on top are water tiles.

翻译中…

To get water to Swampy, you can place a drain on any tile that isn't a dirt tile. Placing a drain takes away all water tiles that can access the drain by moving only down, left, or right on the grid without crossing any dirt tiles.

翻译中…

For example, placing the drain on the $\times$ in the grid below will drain both the top left and top right water tiles and result in $10$ tiles being drained:

翻译中…

image

翻译中…

What is the maximum amount of water you can get to Swampy after placing at most two drains? Note that water tiles that could have been taken by either drain are only counted once.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.

翻译中…

The first line contains two integers $n$ and $h$ ($1 \leq n \leq 2000, 1 \leq h \leq 10^9$) — the number of columns and height of the grid.

翻译中…

The next line contains $n$ integers $a_i$ ($1 \leq a_i \leq h$) — the elevation of the dirt in column $i$.

翻译中…

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 number of water tiles that can be obtained with at most two drains.

翻译中…

Note

翻译中…

In the first test case, the grid is the example shown in the statement. It is possible to drain $14$ tiles of water by placing the drains as shown:

翻译中…

image

翻译中…

In the second test case, the grid is shown below:

翻译中…

image

翻译中…

It is possible to drain all $43$ tiles of water by placing the drains as shown:

翻译中…

image

翻译中…

In the third test case, the grid is a single dirt tile, and no drains may be placed. So, the answer is $0$.

翻译中…

样例

Input #1
6
7 4
1 3 1 2 3 1 1
8 10
7 5 1 3 2 5 6 8
1 1
1
10 20
5 2 1 2 1 3 6 7 1 1
5 1000000000
1 420420420 1 420420420 1
1 1000000000
1
Output #1
14
43
0
170
3738738738
999999999
InputCopy OutputCopy
题目信息
题号
2207C
时间限制
2 s
内存限制
256 MB
标签
数据结构
分治
动态规划
数学

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