#K84957. Smallest Positive Subarray Sum

    ID: 36534 Type: Default 1000ms 256MiB

Smallest Positive Subarray Sum

Smallest Positive Subarray Sum

You are given an array of n integers. Your task is to find the smallest positive integer \(k\) for which there exists at least one contiguous subarray whose sum is exactly \(k\). In case no subarray yields a positive sum, output \(-1\).

A subarray is defined as a contiguous segment of the array. For example, consider the array \([1, -1, 2, 3, -2]\): the subarray \([2, 3, -2]\) has a sum of \(3\); however, the smallest positive sum achievable by any subarray in this array is \(1\). Your program should implement an efficient algorithm to determine this value.

Note: A valid subarray must consist of at least one element.

inputFormat

The first line contains an integer \(n\), representing the number of elements in the array. The second line contains \(n\) space-separated integers, which are the elements of the array.

Example:

5
1 -1 2 3 -2

outputFormat

Output a single integer representing the smallest positive subarray sum \(k\) if it exists, otherwise output \(-1\).

Example:

1
## sample
5
1 -1 2 3 -2
1