CF1534B.Histogram Ugliness

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Little Dormi received a histogram with nn bars of height a1,a2,,ana_1, a_2, \ldots, a_n for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.

To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times:

  • Select an index ii ( 1in1 \le i \le n ) where ai>0a_i>0 , and assign ai:=ai1a_i := a_i-1 .

Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.

However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.

Consider the following example where the histogram has 44 columns of heights 4,8,9,64,8,9,6 :

The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is 4+4+1+3+6=184+4+1+3+6 = 18 , so if Little Dormi does not modify the histogram at all, the ugliness would be 1818 .

However, Little Dormi can apply the operation once on column 22 and twice on column 33 , resulting in a histogram with heights 4,7,7,64,7,7,6 :

Now, as the total vertical length of the outline (red lines) is 4+3+1+6=144+3+1+6=14 , the ugliness is 14+3=1714+3=17 dollars. It can be proven that this is optimal.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt ( 1t1041 \le t \le 10^4 ). Description of the test cases follows.

The first line of each test case contains a single integer nn ( 1n41051 \le n \le 4 \cdot 10^5 ).

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n ( 0ai1090 \le a_i \le 10^9 ).

It is guaranteed that the sum of nn over all test cases does not exceed 41054 \cdot 10^5 .

输出格式

For each test case output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.

输入输出样例

  • 输入#1

    2
    4
    4 8 9 6
    6
    2 1 7 4 0 0

    输出#1

    17
    12

说明/提示

Example 11 is the example described in the statement.

The initial histogram for example 22 is given below:

The ugliness is currently 2+1+6+3+4=162+1+6+3+4=16 .

By applying the operation once on column 11 , six times on column 33 , and three times on column 44 , we can end up with a histogram with heights 1,1,1,1,0,01,1,1,1,0,0 :

The vertical length of the outline is now 1+1=21+1=2 and Little Dormi made 1+6+3=101+6+3=10 operations, so the final ugliness is 2+10=122+10=12 , which can be proven to be optimal.

首页