CF1918D.Blocking Elements
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array of numbers a1,a2,…,an . Your task is to block some elements of the array in order to minimize its cost. Suppose you block the elements with indices 1≤b1<b2<…<bm≤n . Then the cost of the array is calculated as the maximum of:
- the sum of the blocked elements, i.e., ab1+ab2+…+abm .
- 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,b1−1 ], [ b1+1,b2−1 ], [ … ], [ bm−1+1,bm−1 ], [ bm+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.
输入格式
The first line of the input contains a single integer t ( 1≤t≤30000 ) — the number of queries.
Each test case consists of two lines. The first line contains an integer n ( 1≤n≤105 ) — the length of the array a . The second line contains n elements a1,a2,…,an ( 1≤ai≤109 ) — the array a .
It is guaranteed that the sum of n over all test cases does not exceed 105 .
输出格式
For each test case, output a single number — the minimum cost of blocking the array.
输入输出样例
输入#1
3 6 1 4 5 3 3 2 5 1 2 3 4 5 6 4 1 6 3 10 7
输出#1
7 5 11
说明/提示
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 a2+a4=7 .
- the maximum sum of the segments into which the array is divided when the blocked elements are removed, i.e., the maximum of a1 , a3 , a5+a6=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 .