Kotlin Heroes: Episode 10 |
---|

Finished |

The following languages are only available languages for the problems from the contest

Kotlin Heroes: Episode 10:

- Kotlin 1.7.20
- Kotlin 1.9.21

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

*special problem

*1600

No tag edit access

The problem statement has recently been changed. View the changes.

×
D. Staircase

time limit per test

2 secondsmemory limit per test

512 megabytesinput

standard inputoutput

standard outputThere 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.

Example

Input

650 0 0 0 040 13 15 8413 15 0 881 2 3 4 5 6 7 8599999999 100000000 99999999 99999999 9999999952 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$$$.

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jul/17/2024 13:24:51 (j1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|