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

#PS0000205. Tổng dương nhỏ nhất (Minimum Positive Subarray Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng dương nhỏ nhất (Minimum Positive Subarray Sum)

Minimum Positive Subarray Sum

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn integers aia_i, find the smallest strictly positive sum of a nonempty consecutive interval. Print -1 if no such interval exists.

Input

The first line contains nn. The second line contains exactly nn integers aia_i.

Output

Print the minimum positive sum, or -1 if none exists.

Subtasks

  • Subtask 1 (25 points): 1≤n≤2001\le n\le 200; ∣ai∣≤109|a_i|\le10^9.
  • Subtask 2 (25 points): 1≤n≤2000001\le n\le 200000; ∣ai∣≤109|a_i|\le10^9; ai≥0a_i\ge0.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; ∣ai∣≤109|a_i|\le10^9.

Example 1

Input

3
3 -2 1

Output

1

Explanation

Intervals [1,2] and [3,3] sum to 1, the smallest possible positive integer.

Example 2

Input

3
-2 0 -3

Output

-1

Explanation

Every interval has a nonpositive sum.