while true
    n = gets.to_i
    break if n == 0
    arr = [[gets.to_i, 1]]
    (n - 1).times{|i|
        x = gets.to_i
        if i.even? && arr[-1][0] != x
            a, b = arr.pop
            if arr.empty?
                arr << [x, b + 1]
            else
                arr[-1][1] += b + 1
            end
        else
            if arr[-1][0] == x
                arr[-1][1] += 1
            else
                arr << [x, 1]
            end
        end
    }
    count = 0
    (arr[0][0] == 0 ? 0 : 1).step(arr.length - 1, 2){|i| count += arr[i][1]}
    puts count
end

