Breaking the Wall
Contest: Codeforces Round 786 (Div. 3)
Difficulty rating: 2000
Tags: binary search, brute force, constructive algorithms, greedy, math

Monocarp plays "Rage of Empires II: Definitive Edition" — a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall.

The wall consists of $$$n$$$ sections, aligned in a row. The $$$i$$$-th section initially has durability $$$a_i$$$. If durability of some section becomes $$$0$$$ or less, this section is considered broken.

To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager — a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals $$$2$$$ damage to the target section and $$$1$$$ damage to adjacent sections. In other words, if the onager shoots at the section $$$x$$$, then the durability of the section $$$x$$$ decreases by $$$2$$$, and the durability of the sections $$$x - 1$$$ and $$$x + 1$$$ (if they exist) decreases by $$$1$$$ each.

Monocarp can shoot at any sections any number of times, he can even shoot at broken sections.

Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him!

Input format:
The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of sections.

The second line contains the sequence of integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the initial durability of the $$$i$$$-th section.

Output format:
Print one integer — the minimum number of onager shots needed to break at least two sections of the wall.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example, it is possible to break the $$$2$$$-nd and the $$$4$$$-th section in $$$10$$$ shots, for example, by shooting the third section $$$10$$$ times. After that, the durabilities become $$$[20, 0, 10, 0, 20]$$$. Another way of doing it is firing $$$5$$$ shots at the $$$2$$$-nd section, and another $$$5$$$ shots at the $$$4$$$-th section. After that, the durabilities become $$$[15, 0, 20, 0, 15]$$$.

In the second example, it is enough to shoot the $$$2$$$-nd section once. Then the $$$1$$$-st and the $$$3$$$-rd section will be broken.

In the third example, it is enough to shoot the $$$2$$$-nd section twice (then the durabilities become $$$[5, 2, 4, 8, 5, 8]$$$), and then shoot the $$$3$$$-rd section twice (then the durabilities become $$$[5, 0, 0, 6, 5, 8]$$$). So, four shots are enough to break the $$$2$$$-nd and the $$$3$$$-rd section.

Example input:
5
20 10 30 10 20

Example output:
10

Example input:
3
1 8 1

Example output:
1

Editorial:
Let's analyze three cases based on the distance between two sections we are going to break:
1. break two neighboring sections ($$$i$$$ and $$$i+1$$$);
2. break two sections with another section between them ($$$i$$$ and $$$i+2$$$);
3. break two sections with more than one section between them.
Why exactly these cases? Because the damage from the shots and the possibility to hit both sections with the same shot depends on the distance between them.
If there is more than one section between the two we want to break, then any shot hits only one of these sections, so each shot should be aimed at one of those sections, and we break them independently. Let's pick two sections with minimum durability and calculate the number of shots required to break them; if these sections are $$$i$$$ and $$$j$$$, then the required number of shots is $$$\lceil \frac{a_i}{2} \rceil + \lceil \frac{a_j}{2} \rceil$$$. It actually does not matter if the distance between them is less than $$$3$$$; if it is so, these sections will be analyzed in one of the other cases.
Okay, now let's deal with two sections having exactly one section between them. We can iterate on all combinations of these sections (iterate on $$$i$$$ from $$$1$$$ to $$$n-2$$$ and pick sections $$$i$$$ and $$$i+2$$$). Let's analyze how can we damage them. If we shoot at the section between them, we deal $$$1$$$ damage to both sections; if we shoot at one of those sections, we deal $$$2$$$ damage to it and $$$0$$$ damage to the other section. So, each shot distributes $$$2$$$ damage between these two sections the way we want to distribute it, and the number of shots required to break these two sections is $$$\lceil \frac{a_i + a_{i+2}}{2} \rceil$$$.
The case when we try to break two adjacent sections is the trickiest one. Let's say that these sections are $$$i$$$ and $$$i+1$$$, $$$x = \max(a_i, a_{i+1})$$$, and $$$y = \min(a_i, a_{i+1})$$$. If we target one of these sections, we deal $$$2$$$ damage to it and $$$1$$$ damage to the other section. Let's try to run the following algorithm: shoot at the section with higher durability, until both of them break. It can be slow, but we can see that after the first $$$x-y$$$ shots, the durabilities of the sections become equal, and each pair of shots after that deals $$$3$$$ damage to both sections. So, we can model the first $$$x-y$$$ shots, subtract $$$2(x-y)$$$ from $$$x$$$ and $$$(x-y)$$$ from $$$y$$$, and then we'll need $$$\lceil \frac{x+y}{3} \rceil$$$ shots. The only case when this doesn't work is if we break both sections before we equalize their durabilities; it means that $$$2y \le x$$$ and we need to do only $$$\lceil \frac{x}{2} \rceil$$$ shots.