P8175 [CEOI 2021] Tortoise
Background
Translated from CEOI 2021 Day 2 T2. [Tortoise](https://hsin.hr/ceoi/competition/ceoi2021_day2_tasks.pdf).
Description
Wilco wants to buy candies, so it will visit the Nakamise shopping street.
Tom wants Wilco to eat fewer candies, so it will buy some candies before Wilco does. The shopping street has $N$ equally spaced locations, each of which is either a shop or an empty lot.
Each shop has a certain number of candies (possibly $0$). Wilco will walk from the first location to the last one, visiting all locations in order. Whenever it arrives at a shop, it buys all the candies there. Tom moves at twice Wilco’s speed at any moment and can move in both directions. However, at any moment, Tom can carry at most one candy. Once Tom takes a candy, it will carry it away until it gives it to children playing on an empty lot. Assume that buying candies and giving them away takes no time.
Tom’s goal is to minimize the number of candies Wilco can obtain. Initially, both of them are at the first location, and Tom always moves before Wilco at any moment. That is, if the first location is a shop, Tom can buy one candy before Wilco.
So, with Tom’s interference, how many candies can Wilco get?
Input Format
The first line contains an integer $N$, as described above.
The second line contains $N$ integers $a_1,a_2,\dots,a_N$ describing the $N$ locations on the shopping street. If $a_i=-1$, then the $i$-th location is an empty lot; otherwise, it is a shop selling $a_i$ candies. A shop may have no candies (i.e., $a_i=0$).
It is guaranteed that there is at least one empty lot.
Output Format
Output the number of candies Wilco will buy.
Explanation/Hint
#### Constraints and conventions
For $100\%$ of the testdata: $1\leq n \leq 5\times 10^5$,$-1\leq a_i \leq 10^4$。
| Subtask | Points | Constraints |
| :-----: | :----: | :-----------------------------------: |
| $1$ | $8$ | $1\leq N\leq 20$,$-1\leq a_i\leq 1$ |
| $2$ | $10$ | $1\leq N\leq 300$,$-1\leq a_i\leq 1$ |
| $3$ | $30$ | $1\leq N\leq 300$,$-1\leq a_i\leq 10^4$ |
| $4$ | $25$ | $1\leq N\leq 5\times 10^3$,$-1\leq a_i\leq 10^4$ |
| $5$ | $27$ | $1\leq N\leq 5\times 10^5$,$-1\leq a_i\leq 10^4$ |
Translated by ChatGPT 5