#P1667. 数列

数列

Problem Description

Given a sequence AA of length nn, we call a sequence "perfect" if and only if the sum of any of its subarrays is positive.

You have an operation that can modify the sequence: choose an interval [l,r][l,r] satisfying ∑i=lrAi<0\sum\limits_{i = l}^r A_i < 0, where 1<l≤r<n1 < l \le r < n.

Let S=∑i=lrAiS = \sum\limits_{i = l}^r A_i. Add SS to Al−1A_{l - 1} and Ar+1A_{r + 1}, and subtract SS from AlA_l and ArA_r (if l=rl = r, subtract twice). Find the minimum number of such operations needed to make the sequence perfect.

Input Format

The first line contains an integer nn.

Lines 22 through n+1n + 1: the ii-th line contains an integer AiA_i.

Output Format

Output a single integer: the minimum number of operations. If there is no solution, output −1-1.

5
13
-3 
-4
-5
62
2

Hint

Sample explanation:

First choose the interval [2,4][2,4], then the sequence becomes 1,9,−4,7,501,9,-4,7,50. Then choose [3,3][3,3], the sequence becomes 1,5,4,3,501,5,4,3,50.

Constraints:

  • For 20%20\% of the testdata, 1≤n≤51 \le n \le 5.
  • For 100%100\% of the testdata, 1≤n≤1051 \le n \le 10^5; 1≤∣Ai∣<2311 \le |A_i| < 2^{31}.

Translated by ChatGPT 5