Codeforces Round 807 (Div. 2)
B
Mark the Dust Sweeper
Codeforces
900
原题
← AC →
翻译中…

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 \lt 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.

翻译中…

Input

翻译中…

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$.

翻译中…

Output

翻译中…

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.

翻译中…

Note

翻译中…

In the first case, one possible sequence of operations is as follows.

翻译中…
  • Choose $i=1$ and $j=2$, yielding the array $[1,1,0]$.
  • Choose $i=1$ and $j=3$, yielding the array $[0,1,1]$.
  • Choose $i=2$ and $j=3$, yielding the array $[0,0,2]$.
翻译中…

At this point, $a_1=a_2=0$, completing the process.

翻译中…

In the second case, one possible sequence of operations is as follows.

翻译中…
  • Choose $i=4$ and $j=5$, yielding the array $[0,2,0,1,1]$.
  • Choose $i=2$ and $j=3$, yielding the array $[0,1,1,1,1]$.
  • Choose $i=2$ and $j=5$, yielding the array $[0,0,1,1,2]$.
  • Choose $i=3$ and $j=5$, yielding the array $[0,0,0,1,3]$.
  • Choose $i=4$ and $j=5$, yielding the array $[0,0,0,0,4]$.
翻译中…

In the last case, the array already satisfies the condition.

翻译中…

样例

Input #1
4
3
2 0 0
5
0 2 0 2 0
6
2 0 3 0 4 6
4
0 0 0 10
Output #1
3
5
11
0
InputCopy OutputCopy
题目信息
题号
1705B
时间限制
1.5 s
内存限制
256 MB
标签
构造
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。