All problems

Problem 822

Minimum Value to Get Positive Step by Step Sum

Easy · 10 pts · Array, Prefix Sum

Problem

You choose a positive starting value S and then add the array elements one by one from left to right. Every running total, after each addition, must be at least 1. Find the smallest positive S that works. If the lowest prefix sum of the array is M, the running totals are S plus the prefix sums, so S must satisfy S + M >= 1, and S must also be at least 1.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the minimum starting value.

Constraints

1 <= N <= 100000; -100 <= A[i] <= 100

Example input

5
-3 2 -3 4 2

Example output

5

Explanation

For -3 2 -3 4 2, the prefix sums are -3, -1, -4, 0 and 2, with minimum -4. Starting at 5 keeps every total at least 1 (the lowest is 5 - 4 = 1), so the answer is 5.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Number of Teams Next: Check If N and Its Double Exist