#P8175. [CEOI 2021] Tortoise

[CEOI 2021] Tortoise

Background

Translated from CEOI 2021 Day 2 T2. Tortoise.

Problem 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 NN equally spaced locations, each of which is either a shop or an empty lot.

Each shop has a certain number of candies (possibly 00). 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 NN, as described above.

The second line contains NN integers a1,a2,,aNa_1,a_2,\dots,a_N describing the NN locations on the shopping street. If ai=1a_i=-1, then the ii-th location is an empty lot; otherwise, it is a shop selling aia_i candies. A shop may have no candies (i.e., ai=0a_i=0).

It is guaranteed that there is at least one empty lot.

Output Format

Output the number of candies Wilco will buy.

5
-1 1 1 1 1
2
8
-1 1 0 0 -1 0 0 3
1
8
2 -1 2 -1 2 -1 2 -1
1

Hint

Constraints and conventions

For 100%100\% of the testdata: 1n5×1051\leq n \leq 5\times 10^51ai104-1\leq a_i \leq 10^4

Subtask Points Constraints
11 88 1N201\leq N\leq 201ai1-1\leq a_i\leq 1
22 1010 1N3001\leq N\leq 3001ai1-1\leq a_i\leq 1
33 3030 1N3001\leq N\leq 3001ai104-1\leq a_i\leq 10^4
44 2525 1N5×1031\leq N\leq 5\times 10^31ai104-1\leq a_i\leq 10^4
55 2727 1N5×1051\leq N\leq 5\times 10^51ai104-1\leq a_i\leq 10^4

Translated by ChatGPT 5