Domain: Algorithms & Data Structures

You are fighting a vertical column of `n` enemies arranged in a single stack.

- Enemy `1` is at the bottom, enemy `n` is at the top.
- Enemy `i` starts with health `h[i]`.

### Attack rule
In one hit, you may choose **any currently alive enemy** (in any stack that exists at that moment) and reduce its health by `1`.

### Collapse rule after a death
Whenever an enemy’s health becomes `0` or less, that enemy dies immediately. Then:

1. All enemies that were **above** the dead one fall downward and become a **new stack** (separate from whatever remains below).
2. In this new stack, the **bottom** enemy takes **fall damage** equal to how many enemies were below it before it fell.  
   That number includes the enemy that just died.
3. If the bottom enemy of the new stack dies from fall damage, the same falling process repeats again for the enemies above it, potentially causing a chain reaction.

This continues until no newly formed stack has its bottom enemy killed by the fall damage it just received.

### Goal
Determine the minimum number of hits required to kill **all** enemies.

---

## Input
The input contains multiple test cases.

- The first line contains an integer `t` (`1 ≤ t ≤ 10^4`).
- For each test case:
  - The first line contains an integer `n` (`2 ≤ n ≤ 2·10^5`) — the number of enemies in the initial stack.
  - The second line contains `n` integers `h[1], h[2], …, h[n]` (`1 ≤ h[i] ≤ 10^9`) — their initial health values.

It is guaranteed that the sum of `n` over all test cases does not exceed `2·10^5`.

---

## Output
For each test case, output one integer: the minimum number of hits needed to eliminate every enemy.

Grading rubric:
criterion: Does the answer define dp[i] as the minimum number of attacks needed to eliminate mobs 1 through i?
weight: 10
criterion: Does the answer state that the final output for a test case equals dp[n]?
weight: 10
criterion: Does the answer state that a mob can receive fall damage at most once?
weight: 10
criterion: Does the answer state the base case dp[0] = 0?
weight: 10
criterion: Does the answer state the base case dp[1] = h[1]?
weight: 10
criterion: Does the answer state that one option makes mob i take exactly 1 fall damage?
weight: 10
criterion: Does the answer give the transition term dp[i-1] + max(0, h[i] - 1)?
weight: 10
criterion: Does the answer state that the other option makes mob i take exactly i-1 fall damage?
weight: 10
criterion: Does the answer give the transition term dp[i-2] + h[i-1] + max(0, h[i] - (i-1))?
weight: 10
criterion: Does the answer state a per-test-case time complexity of O(n)?
weight: 10

Answer:
### Key observations
1. **A creature can receive fall damage at most once.**  
   After it falls, it becomes the bottom of its new stack, and nothing can later fall “under it” again.

2. **If a creature ever receives fall damage greater than 1,** then the creature directly below it must have been killed by sword hits (not by fall damage).  
   Reason: a creature that has already fallen is at the bottom; killing it would only ever make the next creature fall by **1**.

3. Therefore, whenever we want a creature `i` to get a large fall damage amount, we should arrange it so that creature `i-1` is killed **while the original stack below is still intact**, because that maximizes the fall damage to `i` without increasing the number of sword hits needed elsewhere.

---

### DP definition
Let `dp[i]` be the minimum number of sword hits needed to eliminate creatures `1..i` (from the bottom up, considering only these `i` creatures).

Base:
- `dp[0] = 0`
- `dp[1] = h[1]`

For `i ≥ 2`, we consider two ways to handle creature `i`:

---

### Option A: creature `i` only falls by 1
If creature `i-1` dies when it is the bottom of the current stack containing `i-1` and `i`, then `i` falls and takes **1** damage.

Cost:
- clear `1..i-1`: `dp[i-1]`
- finish creature `i` after 1 fall damage: `max(0, h[i] - 1)`

So:
\[
\text{costA} = dp[i-1] + \max(0, h[i] - 1)
\]

---

### Option B: creature `i` gets maximum fall damage `(i-1)`
Kill creature `i-1` directly **before** removing any of `1..i-2`, so creature `i` falls from the original height and takes `(i-1)` damage.

Cost:
- clear `1..i-2` eventually: `dp[i-2]`
- kill creature `i-1` by hits: `h[i-1]`
- finish creature `i` after `(i-1)` fall damage: `max(0, h[i] - (i-1))`

So:
\[
\text{costB} = dp[i-2] + h[i-1] + \max(0, h[i] - (i-1))
\]

---

### Transition and answer
\[
dp[i] = \min(\text{costA}, \text{costB})
\]
The required minimum hits for the full stack is `dp[n]`.

---

### Complexity
Each `dp[i]` is computed in `O(1)`, so the solution is `O(n)` per test case (and `O(\sum n)` overall).