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

#GL0000328. Chuyển kẹo giữa các hộp (Send Boxes to Alice – Easy Version)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chuyển kẹo giữa các hộp (Send Boxes to Alice – Easy Version)

Send Boxes to Alice (Easy Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn candy boxes arranged in a line, numbered from 11 to nn. Box ii initially contains aia_i candies, where in this Easy Version every aia_i is either 00 or 11, and at least one box contains a candy.

In one second, you may move exactly one candy from box ii to an adjacent existing box i−1i-1 or i+1i+1. A box may contain more than one candy after moves.

Find the minimum number of seconds until there exists an integer k>1k>1 such that the candy count in every box is divisible by the same kk. Empty boxes are allowed since 00 is divisible by kk. Print -1 if this is impossible.

Input

The first line contains nn. The second line contains nn integers a1,…,ana_1,\ldots,a_n.

Output

Print the minimum number of seconds, or -1 if impossible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; ai∈{0,1}a_i\in\{0,1\}; ∑ai≥1\sum a_i\ge1.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; ai∈{0,1}a_i\in\{0,1\}; ∑ai≥1\sum a_i\ge1.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le100000; ai∈{0,1}a_i\in\{0,1\}; ∑ai≥1\sum a_i\ge1.

Examples

Example 1

Input

3
1 0 1

Output

2

Explanation

There are two candies, so choose k=2k=2. Moving the candy from box 11 through box 22 to box 33 takes two seconds; the final counts are [0,0,2][0,0,2].

Example 2

Input

1
1

Output

-1

Explanation

There is only one candy. Total candies are invariant, so no common divisor k>1k>1 is possible.

Example 3

Input

5
1 0 0 1 1

Output

4

Explanation

There are 33 candies, hence k=3k=3 is the only option. Moving candies from positions 1,4,51,4,5 to position 44 costs 3+0+1=43+0+1=4 moves.