310992: CF1918D. Blocking Elements

Memory Limit:256 MB Time Limit:4 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

D. Blocking Elementstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given an array of numbers $a_1, a_2, \ldots, a_n$. Your task is to block some elements of the array in order to minimize its cost. Suppose you block the elements with indices $1 \leq b_1 < b_2 < \ldots < b_m \leq n$. Then the cost of the array is calculated as the maximum of:

  • the sum of the blocked elements, i.e., $a_{b_1} + a_{b_2} + \ldots + a_{b_m}$.
  • the maximum sum of the segments into which the array is divided when the blocked elements are removed. That is, the maximum sum of the following ($m + 1$) subarrays: [$1, b_1 − 1$], [$b_1 + 1, b_2 − 1$], [$\ldots$], [$b_{m−1} + 1, b_m - 1$], [$b_m + 1, n$] (the sum of numbers in a subarray of the form [$x,x − 1$] is considered to be $0$).

For example, if $n = 6$, the original array is [$1, 4, 5, 3, 3, 2$], and you block the elements at positions $2$ and $5$, then the cost of the array will be the maximum of the sum of the blocked elements ($4 + 3 = 7$) and the sums of the subarrays ($1$, $5 + 3 = 8$, $2$), which is $\max(7,1,8,2) = 8$.

You need to output the minimum cost of the array after blocking.

Input

The first line of the input contains a single integer $t$ ($1 \leq t \leq 30\,000$) — the number of queries.

Each test case consists of two lines. The first line contains an integer $n$ ($1 \leq n \leq 10^5$) — the length of the array $a$. The second line contains $n$ elements $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the array $a$.

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

Output

For each test case, output a single number — the minimum cost of blocking the array.

ExampleInput
3
6
1 4 5 3 3 2
5
1 2 3 4 5
6
4 1 6 3 10 7
Output
7
5
11
Note

The first test case matches with the array from the statement. To obtain a cost of $7$, you need to block the elements at positions $2$ and $4$. In this case, the cost of the array is calculated as the maximum of:

  • the sum of the blocked elements, which is $a_2 + a_4 = 7$.
  • the maximum sum of the segments into which the array is divided when the blocked elements are removed, i.e., the maximum of $a_1$, $a_3$, $a_5 + a_6 = \max(1,5,5) = 5$.

So the cost is $\max(7,5) = 7$.

In the second test case, you can block the elements at positions $1$ and $4$.

In the third test case, to obtain the answer $11$, you can block the elements at positions $2$ and $5$. There are other ways to get this answer, for example, blocking positions $4$ and $6$.

Output

题目大意:给定一个数组 $a_1, a_2, \ldots, a_n$,通过阻塞某些元素来最小化数组的成本。假设阻塞的元素索引为 $1 \leq b_1 < b_2 < \ldots < b_m \leq n$。那么数组的成本由以下两种情况的最大值计算得出:

1. 阻塞元素的和,即 $a_{b_1} + a_{b_2} + \ldots + a_{b_m}$。
2. 当移除阻塞元素后,数组被划分成的段的最大和。具体来说,就是以下 $m + 1$ 个子数组的最大和:$$
1, b_1 − 1$$$, $$
b_1 + 1, b_2 − 1$$$, \ldots, $$
b_{m−1} + 1, b_m - 1$$$, $$
b_m + 1, n$$$(形式为 $$
x,x − 1$$$ 的子数组的和被认为是 $0$)。

例如,如果 $n = 6$,原始数组是 $[1, 4, 5, 3, 3, 2]$,并且你阻塞了位置 $2$ 和 $5$ 的元素,那么数组的成本将是阻塞元素的和($4 + 3 = 7$)和子数组的和($1$, $5 + 3 = 8$, $2$)的最大值,即 $\max(7,1,8,2) = 8$。

输入输出数据格式:

输入:
- 第一行包含一个整数 $t$($1 \leq t \leq 30,000$)——查询的数量。
- 每个测试案例包含两行。第一行包含一个整数 $n$($1 \leq n \leq 10^5$)——数组 $a$ 的长度。第二行包含 $n$ 个元素 $a_1, a_2, \ldots, a_n$($1 \leq a_i \leq 10^9$)——数组 $a$。
- 保证所有测试案例中 $n$ 的总和不超过 $10^5$。

输出:
- 对于每个测试案例,输出一个数字——阻塞数组的最小成本。题目大意:给定一个数组 $a_1, a_2, \ldots, a_n$,通过阻塞某些元素来最小化数组的成本。假设阻塞的元素索引为 $1 \leq b_1 < b_2 < \ldots < b_m \leq n$。那么数组的成本由以下两种情况的最大值计算得出: 1. 阻塞元素的和,即 $a_{b_1} + a_{b_2} + \ldots + a_{b_m}$。 2. 当移除阻塞元素后,数组被划分成的段的最大和。具体来说,就是以下 $m + 1$ 个子数组的最大和:$$ 1, b_1 − 1$$$, $$ b_1 + 1, b_2 − 1$$$, \ldots, $$ b_{m−1} + 1, b_m - 1$$$, $$ b_m + 1, n$$$(形式为 $$ x,x − 1$$$ 的子数组的和被认为是 $0$)。 例如,如果 $n = 6$,原始数组是 $[1, 4, 5, 3, 3, 2]$,并且你阻塞了位置 $2$ 和 $5$ 的元素,那么数组的成本将是阻塞元素的和($4 + 3 = 7$)和子数组的和($1$, $5 + 3 = 8$, $2$)的最大值,即 $\max(7,1,8,2) = 8$。 输入输出数据格式: 输入: - 第一行包含一个整数 $t$($1 \leq t \leq 30,000$)——查询的数量。 - 每个测试案例包含两行。第一行包含一个整数 $n$($1 \leq n \leq 10^5$)——数组 $a$ 的长度。第二行包含 $n$ 个元素 $a_1, a_2, \ldots, a_n$($1 \leq a_i \leq 10^9$)——数组 $a$。 - 保证所有测试案例中 $n$ 的总和不超过 $10^5$。 输出: - 对于每个测试案例,输出一个数字——阻塞数组的最小成本。

加入题单

上一题 下一题 算法标签: