Coin Flipping

View as PDF

Submit solution


Points: 5
Time limit: 0.6s
Memory limit: 125M

Author:
Problem types

You are playing a game with N coins lined up side by side in a straight line, each coin either head or tails. Each move consists of taking 2 adjacent coins and flipping both coins (switching each coin from heads to tails or vice versa). The objective of this game is to get all the coins to be tails in the least number of moves.

Input Specification

The first line will consist of an integer N (1<N<101). The next N lines will contain the result of a coin flip, either a 0 (representing tails) or a 1 (representing heads).

Output Specification

In the exact order of the coins entered, your program will output the minimum number of moves required to win the game (all coins to be tails). If it is impossible to get all coins to be tails, output -1.

Sample Input 1

5
0
1
0
1
0

Sample Output 1

2

Sample Input 2

6
1
1
0
0
1
0

Sample Output 2

-1

Credits go to AndyMan68 for the problem idea.


Comments

There are no comments at the moment.