#GL0000328. Chuyển kẹo giữa các hộp (Send Boxes to Alice – Easy Version)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 candy boxes arranged in a line, numbered from to . Box initially contains candies, where in this Easy Version every is either or , and at least one box contains a candy.
In one second, you may move exactly one candy from box to an adjacent existing box or . A box may contain more than one candy after moves.
Find the minimum number of seconds until there exists an integer such that the candy count in every box is divisible by the same . Empty boxes are allowed since is divisible by . Print -1 if this is impossible.
Input
The first line contains . The second line contains integers .
Output
Print the minimum number of seconds, or -1 if impossible.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Example 1
Input
3
1 0 1
Output
2
Explanation
There are two candies, so choose . Moving the candy from box through box to box takes two seconds; the final counts are .
Example 2
Input
1
1
Output
-1
Explanation
There is only one candy. Total candies are invariant, so no common divisor is possible.
Example 3
Input
5
1 0 0 1 1
Output
4
Explanation
There are candies, hence is the only option. Moving candies from positions to position costs moves.