Under construction

Still being built. Things may change

Evacuation Route

Time limit2sMemory limit512 MB

Summary
On a line of W cells, each an exit, entrance, or fire door with a seal time, find the maximum number of people who can reach an exit, where each entrance emits one person per time step.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Prefix sum
Solved
No attempts yet

Problem

Disaster prevention research is active in Japan, and its importance has grown in recent years. Evaluating evacuation routes is one important research topic. This time we evaluate the safety of a straight corridor.

The corridor is divided into WW units, numbered 1,2,3,…,W1, 2, 3, \ldots, W from one end to the other. Each unit in the corridor has exactly one of an entrance door, an exit door, or a fire door. There can be multiple entrance doors, exit doors, and fire doors in the corridor.

In this problem we assume a fire breaks out at time t=0t=0. As a result, people outside the corridor who are trying to evacuate enter the corridor through entrance doors, and try to escape through exit doors to reach a safer place. Each evacuating person, in every unit of time, either moves to one unit or stays in the current unit. That is, if a person is in unit ii at time tt, then at time t+1t+1 that person can choose one of the three numbers i−1,i,i+1i-1, i, i+1 that lies between 11 and WW inclusive, and move to the unit with that number. A unit with a fire door becomes completely sealed after a certain time, so evacuating people cannot enter that unit. Also, people who were inside that unit can no longer move from it to another unit.

The goal of this problem is, given the information about each door, to compute the maximum number of people who can reach an exit door when the evacuating people act optimally.

The corridor information is given as WW integers aia_i.

  • ai=0a_i = 0 means unit ii is an exit door.
  • ai<0a_i < 0 means unit ii becomes inaccessible from time ∣ai∣|a_i| onward because of a fire door.
  • ai>0a_i > 0 means unit ii is an entrance door, and exactly one person appears in unit ii at each of times t=0,1,2,…,ai−1t=0, 1, 2, \ldots, a_i-1. A person who appears at time tt starts moving from time t+1t+1 onward.

Multiple people may be in the same unit.

Find the maximum number of people who can reach an exit door.

Input

The input is given in the following format.

WW

a1a_1 a2a_2 ...... aWa_W

Output

Print the maximum number of people on one line.

Constraints

  • 1≤W≤1051 ≤ W ≤ 10^5
  • ∣ai∣≤104|a_i| ≤ 10^4
  • All input values are integers.

Examples3

  1. Example 1

    Input
    7
    2 0 -2 3 2 -2 0
    
    Expected output
    4
    
  2. Example 2

    Input
    4
    1 1 1 1
    
    Expected output
    0
    
  3. Example 3

    Input
    9
    10 -10 10 -10 10 -10 10 -10 0
    
    Expected output
    24