cswork / OAOpen practice workspace

AmazonOA #147

Find First Index Where Prefix Sum Becomes Non-Positive

Estimated EasyJudge-ready

Topics (AI-labelled)Prefix & Ranges

Find the earliest non-empty prefix whose sum is ≤0; if there is none, output −1. This site's standard I/O explicitly uses 0-based indices: the source's examples 2 and 3 mix index conventions, so the result of example 2 is corrected from 2 to 1.

Input

The first line contains n (1..100000), and the second line contains n integers in −10^9..10^9.

Output

Output a single integer answer.

Example 1

Input:

2
1 2

Output:

-1

Example 2

Input:

3
2 -4 1

Output:

1

Example 3

Input:

4
1 2 3 -6

Output:

3

Example notes

Example 1: the prefixes 1 and 3 are both positive, so output −1. Example 2: the prefixes are 2, −2, −1, and the first one occurs at index 1. Example 3: the prefixes are 1, 3, 6, 0, and the first one is at index 3.

Editorial

Loading solution…