题库练习 Mark the Dust Sweeper
← 上一题 下一题 →

A15202 | Mark the Dust Sweeper

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Mark is cleaning a row of $n$ rooms. The $i$ -th room has a nonnegative dust level $a_i$ . He has a magical cleaning machine that can do the following three-step operation.

- Select two indices $i<j$ such that the dust levels $a_i$ , $a_{i+1}$ , $\dots$ , $a_{j-1}$ are all strictly greater than $0$ .
- Set $a_i$ to $a_i-1$ .
- Set $a_j$ to $a_j+1$ .

Mark's goal is to make $a_1 = a_2 = \ldots = a_{n-1} = 0$ so that he can nicely sweep the $n$ -th room. Determine the minimum number of operations needed to reach his goal.

输入格式

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

The first line of each test case contains a single integer $n$ ( $2\leq n\leq 2\cdot 10^5$ ) — the number of rooms.

The second line of each test case contains $n$ integers $a_1$ , $a_2$ , ..., $a_n$ ( $0\leq a_i\leq 10^9$ ) — the dust level of each room.

It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$ .

输出格式

For each test case, print a line containing a single integer — the minimum number of operations. It can be proven that there is a sequence of operations that meets the goal.

输入输出样例

输入 #1
4
3
2 0 0
5
0 2 0 2 0
6
2 0 3 0 4 6
4
0 0 0 10
输出 #1
3
5
11
0
C++ 编辑器
输入
输出