311266: CF1958D. Staircase

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

D. Staircasetime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

There is a staircase consisting of $n$ steps. Each step is either intact, or broken. For each broken step, an integer $a_i$ is given denoting the difficulty of repairing it.

Every day, you can either:

  • repair an arbitrary broken step. The effort it takes to repair the $i$-th step is equal to $a_i$;
  • or repair two adjacent broken steps. The effort it takes to repair both the $i$-th step and the $(i+1)$-th step is equal to $2 \cdot (a_i+a_{i+1})$.

You want to repair all broken steps of the staircase, and you want to do it in the minimum possible number of days. What is the minimum total effort it will take to repair all broken steps in the minimum number of days?

Input

The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

Each test case consists of two lines:

  • the first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) — the number of steps;
  • the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^8$). If $a_i = 0$, then the $i$-th step does not need to be repaired; otherwise, the $i$-th step is broken and $a_i$ is the difficulty of repairing it.

Additional constraint on the input: the sum of values of $n$ does not exceed $3 \cdot 10^5$.

Output

For each test case, print one integer — the minimum possible total effort it will take to repair all broken steps in the minimum number of days.

ExampleInput
6
5
0 0 0 0 0
4
0 13 15 8
4
13 15 0 8
8
1 2 3 4 5 6 7 8
5
99999999 100000000 99999999 99999999 99999999
5
2 3 4 3 2
Output
0
59
64
72
899999993
24
Note

In the first test case, you don't have to do anything.

In the second test case, you can repair the $3$-rd and the $4$-th step during the first day, and the $2$-nd step during the second day. The total effort will be $2 \cdot (15 + 8) + 13 = 59$.

In the third test case, you can repair the $4$-th step during the first day, and two first steps during the second day. The total effort will be $8 + 2 \cdot (13 + 15) = 64$.

Output

题目大意:
有一个楼梯,由n个台阶组成,每个台阶要么是完整的,要么是破碎的。对于每个破碎的台阶,给出了一个整数a_i,表示修复它的难度。

每天,你可以选择:
1. 修复任意一个破碎的台阶。修复第i个台阶的努力等于a_i;
2. 或者修复两个相邻的破碎台阶。修复第i个和第(i+1)个台阶的努力等于2*(a_i+a_{i+1})。

你想要在尽可能短的时间内修复所有破碎的台阶,求修复所有破碎台阶所需的最小总努力。

输入数据格式:
第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。
每个测试用例包含两行:
1. 第一行包含一个整数n(1≤n≤3*10^5)——台阶的数量;
2. 第二行包含n个整数a_1, a_2, …, a_n(0≤a_i≤10^8)。如果a_i=0,则第i个台阶不需要修复;否则,第i个台阶是破碎的,a_i是修复它的难度。

输出数据格式:
对于每个测试用例,输出一个整数——在尽可能短的时间内修复所有破碎台阶所需的最小总努力。题目大意: 有一个楼梯,由n个台阶组成,每个台阶要么是完整的,要么是破碎的。对于每个破碎的台阶,给出了一个整数a_i,表示修复它的难度。 每天,你可以选择: 1. 修复任意一个破碎的台阶。修复第i个台阶的努力等于a_i; 2. 或者修复两个相邻的破碎台阶。修复第i个和第(i+1)个台阶的努力等于2*(a_i+a_{i+1})。 你想要在尽可能短的时间内修复所有破碎的台阶,求修复所有破碎台阶所需的最小总努力。 输入数据格式: 第一行包含一个整数t(1≤t≤10^4)——测试用例的数量。 每个测试用例包含两行: 1. 第一行包含一个整数n(1≤n≤3*10^5)——台阶的数量; 2. 第二行包含n个整数a_1, a_2, …, a_n(0≤a_i≤10^8)。如果a_i=0,则第i个台阶不需要修复;否则,第i个台阶是破碎的,a_i是修复它的难度。 输出数据格式: 对于每个测试用例,输出一个整数——在尽可能短的时间内修复所有破碎台阶所需的最小总努力。

加入题单

上一题 下一题 算法标签: