Non-negative Partial Sums

Time limit3sMemory limit128 MB

Summary
Given a cyclic array, count how many rotations make every prefix sum of the rotated sequence non-negative.
Level

Medium5 of 10

Topics
Prefix sum, Array
Solved
No attempts yet

Problem

You are given a sequence of nn numbers a0,a1,…,an−1a_0, a_1, \dots, a_{n-1}. A cyclic shift by kk positions (0≤k≤n−10 \le k \le n - 1) produces the sequence ak,ak+1,…,an−1,a0,a1,…,ak−1.a_k, a_{k+1}, \dots, a_{n-1}, a_0, a_1, \dots, a_{k-1}. Count how many of the nn cyclic shifts satisfy the following condition: the sum of the first ii numbers of the shifted sequence is greater than or equal to zero for every ii with 1≤i≤n1 \le i \le n.

Input

The input contains several test cases.

Each test case consists of two lines. The first line contains the integer nn (1≤n≤1061 \le n \le 10^6), the number of integers in the sequence. The second line contains nn integers a0,a1,…,an−1a_0, a_1, \dots, a_{n-1} (−1000≤ai≤1000-1000 \le a_i \le 1000) describing the sequence.

The input ends with a line containing a single 00.

Output

For each test case, print one line containing the number of cyclic shifts of the given sequence that satisfy the condition above.

Examples1

  1. Example 1

    Input
    3
    2 2 1
    3
    -1 1 1
    1
    -1
    0
    
    Expected output
    3
    2
    0