PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000117. Tìm vị trí cân bằng (Find Pivot Index)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tìm vị trí cân bằng (Find Pivot Index)

Find Pivot Index

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An array of nn integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1} is indexed from 0. An index ii is balanced if the sum strictly to its left equals the sum strictly to its right. The element aia_i belongs to neither sum. An empty side has sum 0. Find the smallest balanced index, or return −1-1 if none exists.

Input

The first line contains integer nn.

The second line contains nn integers a0,…,an−1a_0,\ldots,a_{n-1}.

Output

Print one integer: the smallest balanced index (zero-based), or −1-1 if none exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; −1000≤ai≤1000-1000\le a_i\le 1000; 0≤i<n0\le i<n.
  • Subtask 2 (30 points): 1≤n≤1041\le n\le 10^4; 1≤ai≤10001\le a_i\le 1000; 0≤i<n0\le i<n.
  • Subtask 3 (50 points): 1≤n≤1041\le n\le 10^4; −1000≤ai≤1000-1000\le a_i\le 1000; 0≤i<n0\le i<n.

Example 1

Input

6
1 7 3 6 5 6

Output

3

Explanation

At indices 0, 1, and 2 the left/right sums are (0,27)(0,27), (1,20)(1,20), and (8,17)(8,17). At index 3 they are 1+7+3=111+7+3=11 and 5+6=115+6=11, so the smallest valid index is 3.

Example 2

Input

3
1 2 3

Output

-1

Explanation

The left/right pairs are (0,5)(0,5), (1,3)(1,3), and (3,0)(3,0). None are equal.

Example 3

Input

3
2 1 -1

Output

0

Explanation

At index 0 the left side is empty and sums to 0; the right sum is 1−1=01-1=0. No smaller index is possible.