problems / 263

Suki's Balanced Bench

easy prefix-sums arrays 1000 ms · 256 MB

Suki's Balanced Bench

Suki has found a long garden bench with n treats lying along it in a row, numbered 1 to n from left to right. Each treat has a value a_i; some are tasty (positive), some are the vegetables she'd rather not eat (negative), and some are crumbs worth 0. Suki wants to hop up and sit on one of the treats so that everything to her left is worth exactly as much as everything to her right. The treat she sits on counts for neither side, and a side with no treats on it is worth 0.

Find the leftmost position where Suki can sit: the smallest i such that a_1 + … + a_(i−1) = a_(i+1) + … + a_n.

Input

The first line contains n (1 ≤ n ≤ 2·10^5), the number of treats.

The second line contains n integers a_1, a_2, …, a_n (−10^9 ≤ a_i ≤ 10^9), the values of the treats from left to right.

Output

Print the smallest position i (1 ≤ i ≤ n) where the two sides are worth the same, or -1 if there is no such position.

Notes

In the first example, sitting on treat 4 leaves 4 − 2 + 5 = 7 on her left and 3 + 0 + 4 = 7 on her right.

In the second example, sitting on treat 1 leaves nothing (worth 0) on her left and 4 + 0 − 4 + 0 = 0 on her right. Treat 5 would also work, but 1 is further left.

In the third example each side is worth 2·10^9, which does not fit in a 32-bit integer. Sums can reach 2·10^14 in absolute value, so use 64-bit integers.

In the fourth example no position works.

Example 1

Input

7
4 -2 5 9 3 0 4

Output

4

Example 2

Input

5
0 4 0 -4 0

Output

1

Example 3

Input

5
1000000000 1000000000 7 1000000000 1000000000

Output

3

Example 4

Input

3
1 2 3

Output

-1

Hints · reveal one at a time if you're stuck

Hint 1

Adding up both sides again for every position is too slow for n = 2·10^5. As Suki moves one treat to the right, how much do the two sides change?

Hint 2

Add up all the treats once. If you also know the sum of the treats to Suki's left, the right side follows from those two numbers and the treat she sits on: a prefix sum. The sums can reach about 2·10^14, so use 64-bit integers.

report an issue Prefer your own editor? sooks prep 263 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.