#K6931. Minimum Operations to Transform Array to All Ones

    ID: 33058 Type: Default 1000ms 256MiB

Minimum Operations to Transform Array to All Ones

Minimum Operations to Transform Array to All Ones

You are given an array A of N integers where each element is either 0 or 1. Your task is to determine the minimum number of operations required to change the array so that every element becomes 1. In one operation, you can change a 0 to a 1.

However, if the array does not contain any 1's initially (i.e. if the array is all zeros), it is impossible to perform the task. In such a case, print -1.

Mathematically, if we denote the array by A, the answer is given by:

[ f(A)= \begin{cases} -1, & \text{if } \forall i, A_i=0 \ #{i \mid A_i = 0}, & \text{otherwise} \end{cases} ]

inputFormat

The first line contains an integer N denoting the number of elements in the array.

The second line contains N space-separated integers, each of which is either 0 or 1.

outputFormat

Print a single integer which is the minimum number of operations required to make all elements 1 (i.e., the number of zeros), or -1 if it is impossible.

## sample
4
1 1 1 1
0

</p>