n = int(input())
lis = [int(e) for e in input().split()]
ct = []
c0 = 0
c1 = 0
for i in range(len(lis)):
    if lis[i]==0:c0+=1
    if lis[i]==1:c1+=1
ct.append(c0)
ct.append(c1)

for i in range(1,len(lis)):
    c0 = 0
    c1 = 0
    for j in range(i-1,-1,-1):
        if lis[j]==0:
            c0+=1
    for j in range(i,len(lis)):
        if lis[j]==1:
            c1+=1
    #print("c0=",c0)
    #print("c1=",c1)
    ct.append(c0+c1)

#print(ct)
print(max(ct))

n = int(input())
a = list(map(int, input().split()))
one = []
zero = 0
for i in range(n):
    if a[i] == 1:
        one.append(i)
    else:
        zero += 1

if len(one) == 0:
    print(zero)
else:
    num = zero
    for i in range(len(one)):
        pre0 = 0
        suf0 = 0
        tmp1 = 0
        for j in range(n):
            if j < one[i] and a[j] == 0:
                pre0 += 1
            elif one[i] <= j and a[j] == 1:
                tmp1 += 1
        num = max(pre0 + tmp1, num)
    print(num)

n = int(input())
lst = list(map(int, input().split()))
l = [lst[:i].count(0)+lst[i:].count(1) for i in range(n+1)]
print (max(l))

n = int(input())
games = list(map(int, input().split()))
after = [0] * n
if games[-1] == 1:
    after[-1] = 1
else:
    after[-1] = 0
for i in range(n - 2, -1, -1):
    if games[i] == 1:
        after[i] = after[i + 1] + 1
    else:
        after[i] = after[i + 1]
curnull = 0
ans = 0
for i in range(n):
    if games[i] == 0:
        curnull += 1
    ans = max(ans, curnull + after[i])
print(ans)

n = int(input())
s = list(map(int, input().split(' ')))

ones = sum(1 for v in s if v == 1)
zeroes = sum(1 for v in s if v == 0)
zero_ones = 0

loss = 0
for i in range(n):
    if s[i] == 1:
        loss += 1
        continue

    loss_zeroes = 0
    j = i + 1
    while (j < n):
        if (s[j] == 0 and s[j-1] == 1):
            while (j < n and s[j] == 0):
                loss_zeroes += 1
                j += 1

            continue

        j += 1

    zero_ones = max(zero_ones, (n - loss - loss_zeroes))

# print (f"leading {leading_zeroes}")
# print (f"ones {ones}")
# print (f"zeroes {zeroes}")

print(max(ones, zeroes, zero_ones))

from sys import stdin, stdout

n = int(stdin.readline())
values = list(map(int, stdin.readline().split()))
dp = [[0, 0] for i in range(n)]

for i in range(n):
    if not values[i]:
        dp[i][0] = 1

        for j in range(i - 1, -1, -1):
            dp[i][0] = max(dp[i][0], dp[j][0] + 1)
    else:
        dp[i][1] = 1

        for j in range(i - 1, -1, -1):
            dp[i][1] = max(dp[i][1], max(dp[j]) + 1)

ans = 0
for i in range(n):
    ans = max(ans, max(dp[i]))

stdout.write(str(ans))

n = int(input())
a = [int(i) for i in input().split()]
answer = [0] * n
answer[0] = 1

for i in range(1, n):
    if a[i] == 1:
        answer[i] = max(answer) + 1
    else:
        temp_max = 0
        for j in range(i):
            if a[j] == 0 and answer[j] > temp_max:
                temp_max = answer[j]
        answer[i] = temp_max + 1

print(max(answer))

n = int(input())
val = list(map(int, input().split()))
k = 0
for i in range(n + 1):
    k = max(k, i - sum(val[:i]) + sum(val[i:]))
print(k)

n = int(input())
t = -1
an = 0
a = list(map(int, input().split()))
while a.count(1):
    an = max(a.index(1) + a.count(1), an)
    del a[a.index(1)]

print(max(an, len(a)))

n = int(input())
s = list(map(int, input().split()))
a = 0
for i in range(n+1):
    x = s[:i].count(0)
    y = s[i:].count(1)
    a = max(a, x+y)
print(a)

def read_ints():
	return [int(i) for i in input().split()]

n = int(input())
s = read_ints()
to_rem = n if sum(s) else 0
for i in range(n):
	to_rem = min(to_rem, s[:i].count(1) + (s[i:].count(0) if sum(s[i:]) else 0))

print(n - to_rem)

n = int(raw_input())

games = map(int, raw_input().split())

resume = 0
zero_cnt = 0
for i in range(n):
    if games[i] == 0:
        zero_cnt += 1
        resume = max(resume, zero_cnt + games[i:].count(1))

print max(games.count(1), resume)

n=int(input())
a=list(map(int,input().split()))
print(max(a[:i].count(0)+a[i:].count(1) for i in range(n+1)))

n=int(input())
s=list(map(int,input().split()))
ans=0
for i in range(n+1):
    x=s[:i].count(0)
    y=s[i:].count(1)
    ans=max(ans,x+y)
print(ans)

T = input()
n = map(int, raw_input().split())
maximum = 0

for i in range(0,T):
    count_for_one = 0
    if n[i] == 1:
        for j in range(0,i):
            if n[j] == 0:
                count_for_one += 1
        for k in range(i, T):
            if n[k] == 1:
                count_for_one += 1
    maximum = max(maximum,count_for_one)

for i in range(0,T):
    count_for_zero = 0
    if n[i] == 0:
        for j in range(0,i+1):
            if n[j] == 0:
                count_for_zero += 1
        for k in range(i+1, T):
            if n[k] == 1:
                count_for_zero += 1
    maximum = max(maximum,count_for_zero)

print maximum

n=input()
a=raw_input().split(" ")
j=0
val=0
for i in xrange(n):
  if a[i]=='1':
    j+=1
  val=max(val,n-j+1-a[i:].count('0'),n-a.count('1'))
print val

n = int(raw_input())
a = map(int, raw_input().split())
o = [0] * (n + 1)
ans = 0
for i, x in enumerate(a):
    o[i+1] = o[i] + x
for i in xrange(n + 1):
    t = i - o[i] + o[n] - o[i]
    if ans < t:
        ans = t
print ans

n=int(input())
a=list(map(int,input().split()))
r=0
for i in range(n+1):
    x=a[:i].count(0)
    y=a[i:].count(1)
    r=max(r,x+y)
print(r)

N = input()
A = map(int, raw_input().split())

if 0 not in A or 1 not in A:
    print N
    exit()

ans = 0
tmp = 0

for i in xrange(N):
    if A[i] == 0:
        tmp += 1
        ans = max(ans, tmp)
    else:
        ans = max(ans, tmp + sum(A[i:]))

print ans

n = int(input())

s = list(map(int, input().split()))

print(max([s[:i].count(0) + s[i:].count(1) for i in range(n + 1)]))

#!/usr/bin/env python
# -*- coding: utf-8 -*-

n = int(raw_input())
a = map(int, raw_input().split())

ones = 0
zeroes = 0
for a_i in a:
    if a_i == 1:
        ones += 1
    else:
        zeroes += 1

if ones == 0:
    print zeroes
elif zeroes == 0:
    print ones
else:
    max_games = max(ones, zeroes)
    ones_so_far = 0
    zeroes_so_far = 0
    for a_i in a:
        if a_i == 1:
            games = ones - ones_so_far + zeroes_so_far
            max_games = max(max_games, games)

        if a_i == 1:
            ones_so_far += 1
        else:
            zeroes_so_far += 1

    print max_games

import sys

sys.setrecursionlimit(100000)

def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield(number)

input_parser = parser()

def get_word():
    global input_parser
    return next(input_parser)

def get_number():
    data = get_word()
    try:
        return int(data)
    except ValueError:
        return float(data)

ans = 0

a = input()
st = input().split()
m0 = 0
m1 = 0
ans = 0
for i in st:
    if int(i):
        m1 = max(m0, m1) + 1
    else:
        m0 += 1
    ans = max(m0, m1)
print(ans)

n = int(input())
t = list(map(int, input().split()))
#print(t)
print( max( [ i-sum(t[:i])  + sum(t[i:]) for i in range(n+1) ] ))

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 20 23:21:21 2017

@author: soham
"""

n=int(raw_input())
s=map(int,raw_input().split())

memo=[0]*n
ones=[0]*n

cnt=0
for i in range(0,n):
    if s[i]==0:
        cnt+=1
    else:
        memo[i]=cnt

cnt=0
for i in range(n-1,-1,-1):
    if s[i]==1:
        cnt+=1
        ones[i]=cnt

ans=0

for i in range(0,n):
    if s[i]==0:
        ans+=1

for i in range(0,n):
    if s[i]==1:
        temp=memo[i]+ones[i]
        ans=max(ans,temp)

print ans

n = int(input())
a = list(map(int, input().split()))
l = [a[i+1:].count(0)+a[:i].count(1) for i in range(n)]
print(n - min(l))

n = int(input())
s = list(map(int, input().split()))
res = max(i-sum(s[:i]) + 1 + sum(s[i+1:]) for i in range(n))
print(res)

n = int(input())
s = list(map(int, input().split()))
res = max([len(s[:i])-sum(s[:i])+sum(s[i:]) for i in range(n+1)])
print(res)

n = int(input())
s = list(map(int, input().split()))
res = max([(i-sum(s[:i])) + 1 + sum(s[i+1:]) for i in range(n)])
print(res)

n = int(input())
a = [int(i) for i in input().split()]
ans = sum(a)
for i in range(len(a) - 1, - 1, -1):
    ans = min(ans, (len(a) - i - sum(a[i:])) + sum(a[:i]))
print(len(a) - ans)

n = int(input())
a = [int(x) for x in input().split(" ")]

case1 = 0
for i in range(n):
    if a[i] == 0:
        case1 += 1

case2 = 0
for i in range(n):
    if a[i] == 1:
        case2 += 1

final = max(case1, case2)

for i in range(n-1):
    result = 0
    s1 = a[:i+1]
    s2 = a[i+1:]
    for j in s1:
        if j == 0:
            result += 1
    for j in s2:
        if j == 1:
            result += 1
    final = max(final, result)

print(final)

n = int(input())
nums = list(map(int, input().split()))

right = [0 for i in range(n)]
for i in range(n - 1, -1, -1):
    right[i] = nums[i]
    if i < n - 1:
        right[i] += right[i + 1]

left = [0 for i in range(n)]
for i in range(n):
    left[i] = nums[i]
    if i > 0:
        left[i] += left[i - 1]

res = 0
for i in range(-1, n):
    count = 0
    if i >= 0:
        count = i + 1 - left[i]
    if i + 1 < n:
        count += right[i + 1]
    res = max(res, count)

print(res)

input()
n = [int(x) for x in input().split()]
a = -1
aa = 0
for i, x in enumerate(n):
    aaa = aa + len([x for x in n[i:] if x])
    if aaa > a:
        a = aaa
    if not x:
        aa += 1
print(max(a,aa))

n = int(input())
games = list(map(int, input().split()))

strike = [0] * n

for i in range(n):
	strike[i] = 1
	for j in range(i):
		if games[j] <= games[i]:
			strike[i] = max(strike[i], strike[j] + 1)

# print(strike)
print(max(strike))

n = int(input())
arr = input()
arr = arr.split(' ')
arr = list(map(int, arr))
maxi = -1
ones = list(range(n+1))
ones[n]=0
for j in range(1, n):
    if arr[n-j] == 1:
        ones[n-j] = ones[n-j+1] + 1
    else:
        ones[n-j] = ones[n-j+1]
if arr[0] == 1:
    ones[0] = ones[1] + 1
else:
    ones[0] = ones[1]
counter = 0
if arr[0] == 1:
    maxi = ones[0]
for i in range(0, n):
    if arr[i] == 0:
        counter = counter + 1
        if i < n-1:
            if maxi < counter + ones[i + 1]:
                maxi = counter + ones[i + 1]
        elif i == n-1:
            if maxi < counter:
                maxi = counter
print(maxi)
# 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0  0 0 0 0 1 1 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0

total = 0
n = int(input())
a = [int(i) for i in input().split()]
max1 = 0
for i in range(n+1):
	sum1 = 0
	for j in range(n):
		if(a[j] == 1 and j>=i):
			sum1 += 1
		elif(a[j] == 0 and j<i):
			sum1 += 1
	max1 = max(max1,sum1)
print(max1)

waste = input()
s = input()
m = -1
for i in range(len(s)+1):
	p = s[:i].count('0') + s[i:].count('1')
	if p > m:
		m = p
print(m)

KoKoKo = int
kudaxKudax = input
kukareku = max
kokokokoko = print
kudkudax = range

kokoko = KoKoKo(kudaxKudax())
ko = [KoKoKo(x) for x in kudaxKudax().split()]
KO = KOKOKO = 0
for kok in kudkudax(kokoko - 1, -1, -1):
    KO += ko[kok]
    KOKOKO = kukareku(KOKOKO, KO + ko[:kok + 1].count(0))
kokokokoko(KOKOKO)

n = int(input())
res = list(map(int, input().split()))
ans = 0

for i in range(n+1):
    x = res[:i].count(0)
    y = res[i:].count(1)
    ans = max(ans, x + y)
print(ans)

n = input()
s = map(int,raw_input().split())
r = []
for i in xrange(n+1):
	r.append(s[:i].count(0)+s[i:].count(1))
print max(r)

n = int(input())
xs = list(map(int, input().split()))

s = [0] * n

if xs[-1] == 1:
    s[-1] = 1

for i in range(n - 2, -1, -1):
    s[i] = s[i + 1]
    if xs[i] == 1:
        s[i] += 1

res = 0

tmp = 0
for i in range(n):
    if xs[i] == 0:
        tmp += 1
    res = max(res, tmp + s[i])

print(res)

n=int(input())
s=input().split()
k=0
m=0
for i in range(n):
    if s[i]=='0':
        k+=1
    x=0
    for j in range(i,n):
        if s[j]=='1':
            x+=1
    if k+x>m:
        m=k+x
print(m)

n = int(input())
a = [int(x) for x in input().split()]

r = 0
for i in range(n + 1) :
    x = a[:i].count(0)
    y = a[i:].count(1)
    r = max(r, x + y)

print(r)

def main():
	n = int(input())
	games = list(map(int, input().split(' ')))

	result = max([games[:i].count(0) + games[i:].count(1) for i in range(n+1)])

	print(result)

if __name__ == "__main__":
	main()

n=int(input())
s=[int(a) for a in input().split()]
ans = 0
for i in range(n+1):
    g = 0
    for j in range(i):
        if s[j]==0:
            g+=1
    for j in range(i,n):
        if s[j]==1:
            g+=1
    if g > ans:
        ans = g
print(ans)

'''
i=n-1
x=0
y=0
while i>=0 and s[i]==1:
    i-=1
while i > -1:
    if s[i]==1:
        x+=1
    i-=1
i=0
while i<n and s[i]==0:
    i+=1
while i < n:
    if s[i]==0:
        y+=1
    i+=1
print(n-min(,y))
'''

"""
Codeforces Round #334 (Div. 2)

Problem 846 A. Curriculum Vitae

@author yamaton
@date 2017-09-13
"""

def count(xs, i):
    return xs[:i].count(0) + xs[i:].count(1)

def solve(xs):
    n = len(xs)
    return max(count(xs, i) for i in range(n + 1))

def main():
    n = int(input())
    xs = [int(c) for c in input().split()]
    assert len(xs) == n
    result = solve(xs)
    print(result)

if __name__ == '__main__':
    main()

def solve(s):
    '''
    >>> solve([1, 1, 0, 1])
    3
    >>> solve([0, 1, 0, 0, 1, 0])
    4
    >>> solve([0])
    1
    '''
    zeroes = accum(s, 0)
    ones = list(reversed(accum(reversed(s), 1)))
    return max(x + y for x, y in zip(zeroes, ones))

def accum(lst, value):
    counts = [0]
    for el in lst:
        if el == value:
            counts.append(counts[-1] + 1)
        else:
            counts.append(counts[-1])
    return counts

def main():
    n = input()
    s = list(map(int, input().split()))
    print(solve(s))

if __name__ == '__main__':
    main()

import sys

#######################################################################

def get_lines():
	return sys.stdin.readlines()

#######################################################################

def diversity():
	lines = sys.stdin.readlines()
	s = lines[0][0:-1]
	k = int(lines[1])

	if k > len(s) or k > 26:
		print "impossible"
		return 0

	unique = ''
	for c in s:
		if not c in unique:
			unique += c

	print max(0, k - len(unique))
	return 0

#######################################################################

def curriculum():
	lines = get_lines()
	n = int(lines[0])
	s = [int(word) for word in lines[1].split()]
	max_kept = 0
	pos = 0
	for start in range(len(s)):
		removed = 0
		for before_start in s[:start]:
			if before_start == 1:
				removed += 1
		for after_start in s[start+1:]:
			if after_start == 0:
				removed += 1
		if n - removed > max_kept:
			max_kept = n - removed
			#print pos
		pos += 1

	print max_kept
	return max_kept

curriculum()

n = int(input())
line = list(map(int, input().split()))
ans = -1
for i in range(n + 1):
    ans1 = 0
    for j in range(n):
        if j < i and line[j] == 0:
            ans1 += 1
        elif j >= i and line[j] == 1:
            ans1 += 1
    ans = max(ans, ans1)
print(ans)

n = int(input())
a = [int(x) for x in input().split()]

r = 0
for i in range(len(a) + 1):
	x = a[:i].count(0)
	y = a[i:].count(1)
	r = max(r, x + y)
print(r)

n = int(input())
a = list(map(int, input().split()))
cnt = a.count(0)
ans = 0
for i in range(cnt + 1):
    a1 = a[::-1]
    a2 = []
    cnt1 = i
    index = 0
    while cnt1:
        if not a1[index]:
            cnt1 -= 1
        else:
            a2.append(a1[index])
        index += 1
    for j in range(index, n):
        a2.append(a1[j])
    a2 = a2[::-1]
    index = len(a2) - 1
    ansi = 0
    while index > -1 and a2[index]:
        index -= 1
    for o in range(index, -1, -1):
        if a2[o]:
            ansi += 1
    ans = max(len(a2) - ansi, ans)
print(ans)

input()
s = input()
print(max([s[:i].count('0') +  s[i:].count('1') for i in range(len(s) + 1)]))

n = int(input())
val = list(map(int, input().split()))
k = 0
for i in range(n + 1):
    k = max(k, i - sum(val[:i]) + sum(val[i:]))
print(k)

n = int(input())
a = [int(x) for x in input().split()]
ans = 0
for i in range(n + 1):
    lans = a[:i].count(0) + sum(a[i:])
    ans = max(ans, lans)
print(ans)

n = int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in range(a.count(0) + 1):
    zc = 0
    for j in range(n):
        if a[j] == 0:
            zc += 1
        if zc == i:
            break
    ans = max(ans, i + a[j:].count(1))
print(ans)

waste = input()
s = input()
m = -1#you want a  seq like [...0 0 0 0 0 0 0 1 1 1 1 1 ...]
for i in range(len(s)+1):
	p = s[:i].count('0') + s[i:].count('1')
	if p > m:
		m = p
print(m)

#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

def main():
    n = int(input())
    s = [int(i) for i in input().split()]

    zero_count = [0] * (n + 1)
    one_count = [0] * (n + 1)

    for i in range(n):
        zero_count[i + 1] = zero_count[i] + (s[i] == 0)
        one_count[i + 1] = one_count[i] + (s[i] == 1)

    sol = zero_count[-1]

    for i, j in enumerate(s):
        if j == 1:
            ones = one_count[-1] - one_count[i]
            zeros = zero_count[i + 1]

            sol = max(sol, ones + zeros)

    print(sol)

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()

if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

n = int(input())
m=list(map(int,input().split()))
x=m.count(0)
y=n-x
a=0
b=0
l=max(x,y)
for i in range(n):
    if m[i]==1:
        a+=1
        c=[]
        d=0
        e=0
        for j in range(i+1):
            if m[j]==1:
                d+=1
            else:
                e+=1
            c.append(e+a-d)
        l=max(l,max(c)+y-a)

    else:
        b+=1
print(l)

#!/usr/bin/env python
# coding: utf-8

# In[21]:

n=int(input())
A=input().split()
#print(A)
#cons_0=[]
#cons_1=[]
count_0=0
count_1=0
i=0
if n==1:
    print(n)
else:
    for i in range(0,n):
        if A[i]=='0':
            count_0+=1
        if A[i]=='1':
            count_1+=1

    maxm=max(count_0, count_1)
    for i in range(0,n):
        max_now=0
        for j in range(0,i+1):
            if A[j]=='0':
                max_now+=1
        for j in range(i+1,n):
            if A[j]=='1':
                max_now+=1
        #print(max_now)
        if max_now>maxm:
            maxm=max_now

    #print(cons_0)
    #print(cons_1)

    print(maxm)

# In[ ]:

# In[ ]:

INF = 10 ** 9 + 7

def cutoff(games, pos):
    leave = 0

    for i, g in enumerate(games):
        if i < pos:
            if g == 0:
                leave += 1
        else:
            if g == 1:
                leave += 1

    return leave

def main():
    n = int(input())

    games = [int(x) for x in input().split()]

    max_leave = -INF

    for i in range(n+1):
        max_leave = max(max_leave, cutoff(games, i))

    print(max_leave)

if __name__ == "__main__":
    main()

n =  int(input())
data = list(map(int, input().split()))
data2 = [0] * n
data2[0] = data[0]
for i in range(1, n):
    if data[i]:
        data2[i] = data2[i - 1] + 1
    else:
        data2[i] = data2[i - 1]
data2 = [0] + data2
ma = -2
for i in range(n + 1):
    ma = max(ma, data2[-1] - data2[i] + i - data2[i])
print(ma)

n = int(input())
lst = list(map(int,input().split()))
l,r = 0,lst.count(0)
res=r
for i,x in enumerate(lst):
    if x==0:r-=1
    else:l+=1
    if l+r<res:res=l+r
print(n-res)

n=int(input())
s=list(map(int,input().split()))
c=[]
t=0
while s[t]==0 and t<n-1:
    t+=1
for i in range(t,n):
    if s[i]==1:
        k=0
        for j in range(i):
            if s[j]==1:
                k+=1
        for j in range(i+1,n):
            if s[j]==0:
                k+=1
        c.append(k)
    else:
        k=0
        for j in range(i):
            if s[j]==1:
                k+=1
        for j in range(i+1,n):
            if s[j]==0:
                k+=1
        c.append(k)
print(n-min(c))

n = int(input())
m = list(map(int, input().split()))
maxi = 0
for i in range(n+1):
    cur = m[:i].count(0) + m[i:].count(1)
    if cur > maxi:
        maxi = cur
print(maxi)

n = int(input())
arr = list(map(int, input().split()))
pref1 = [0]
for i in arr:
    pref1.append(pref1[-1] + i)
suf0 = [0]
for i in arr[::-1]:
    suf0.append(suf0[-1] + (i + 1) % 2)
ans = []
for i in range(n + 1):
    ans.append(n - (suf0[::-1][i] + pref1[i]))
print(max(ans))

n = int(input())
p = list(map(int, input().split()))
ans = n
for i in range(1, n):
    if p[i] == 0 and p[i - 1] == 1:
        j = i
        k1 = 1
        while j < n - 1 and p[j + 1] == 0:
            k1 += 1
            j += 1
        j = i
        k2 = 0
        while j > 0:
            if p[j - 1] == 1:
                k2 += 1
            j -= 1

        if k1 < k2:
            ans -= k1
        else:
            ans -= k2
print(ans)

def main():
	n = int(input())
	games = [0]
	games = games + list(map(int, input().split(' ')))

	result = max([games[:i].count(0) + games[i:].count(1) for i in range(n+1)])

	print(result)

if __name__ == "__main__":
	main()

n = int(input())
val = list(map(int, input().split()))
s = sum(val)
j = 0
k = 0
for elem in val:
    if elem == 0:
        j += 1
    else:
        break
for elem in val[::-1]:
    if elem == 1:
        k += 1
    else:
        break
s = sum(val[j:n - k])
print(j + k + max(s, n - k - j - s))

import sys

n = int(input())
a = [0] + list(map(int, input().split()))
ans = 0
for x, y in zip(a, a[1:]):
    if not (x == 1 and y == 0):
        ans += 1
print(ans)

n = int(input())
s = list(map(int, input().split()))
n = len(s)

ans = 0
j1 = 0
while j1 < n and s[j1] == 0:
    ans += 1
    j1 += 1

j2 = n-1
while j2 > 0 and s[j2] == 1:
    ans += 1
    j2 -= 1

ctr0 = 0
ctr1 = 0
last = 0

for i in range(j1, j2+1):
    if s[i] == 0:
        ctr0 += 1
    else:
        ctr1 += 1

if ctr0  > ctr1 :
    ans += ctr0
else:
    ans += ctr1

print(ans)

n = int(input())
a = [int(i) for i in input().split()]
answer = [0] * n
answer[0] = 1

for i in range(1, n):
    if a[i] == 1:
        answer[i] = max(answer) + 1
    else:
        temp_max = -1
        for j in range(i):
            if a[j] == 0 and answer[j] > temp_max:
                temp_max = answer[j]
        answer[i] = temp_max + 1

print(max(answer))

n = int(input())
res = input().split()
last0 = 0

for i in range(len(res)):
    if res[i] == '0': last0 = i

try: ans = max(res.count('1') + res[:res.index('1')].count('0'), res.count('0'))
except: ans = n

print(ans)

n = int(input())
a = list(map(int, input().split()))
#n = len(a)
delete0 = n - a[a.index(1) if 1 in a else 0:].count(0)
a.reverse()
delete1 = n - a[a.index(0) if 0 in a else 0:].count(1)
print(max(delete0, delete1))

a=int(input())
b=input()
print(a-b.count('1 0'))

def main():
	n = int(input())
	games = list(map(int, input().split(' ')))

	one = 0
	zero = 0
	for i in range(0, len(games)):
		if games[i] == 1:
			one = one + 1
		else:
			zero = zero + 1

	if zero >= one:
		result = len(games) - one
	else:
		result = len(games) - zero

	print(result)

if __name__ == "__main__":
	main()

n = int(input())
nums = list(map(int, input().split()))

d = [0, 0]

for i in nums:
    if i == 0:
        d[0] += 1
    else:
        d[0] += 1
        d[1] += 1

print(max(d[0], d[1]))

n = int(input())
line = list(map(int, input().split()))
ans = -1
for i in range(n):
    ans1 = 0
    for j in range(n):
        if i <= j and line[j] == 0:
            ans1 += 1
        elif i >= j and line[j] == 1:
            ans1 += 1
    ans = max(ans, ans1)
print(ans)

n = int(input())
a = [int(x) for x in input().split()]

r = 0
for i in range(len(a)):
	x = a[:i].count(0)
	y = a[i:].count(1)
	r = max(r, x + y)
print(r)

n = int(input())
lis = [int(e) for e in input().split()]

count = 0
for i in range(1,n):
    if lis[i]==0 and lis[i-1]==1:
        continue
    else:
        count += 1

print(count+1)

n = int(input())
g = list(map(int, input().split()))
ans = [g[0]]
for i in range(1, len(g)):
    if g[i-1] == 1 and g[i] == 0:
        do = 'nothing'
    else:
        ans.append(g[i])
#    print(ans)
#    print(i)
print(len(ans))

n = input()
a = map(int, raw_input().split())

z = 0
zeros = [0] * len(a)
for i in range(len(a)):
    zeros[i] = z
    if a[i] == 0:
        z += 1

o = 0
ones = [0] * len(a)
for i in range(len(a)-1, -1, -1):

    if a[i] == 1:
        o += 1
    ones[i] = o

max_num = 1
for i in range(len(a)):
    t = ones[i] + zeros[i]
    max_num = max(t, max_num)

print max_num

n = int(input())

def it():
  p = 0
  for a in map(int,input().split()):
    if not (p == 1 and a == 0):
      yield 1
    p = a

print(sum(it()))

"""
Codeforces Round #334 (Div. 2)

Problem 846 A. Curriculum Vitae

@author yamaton
@date 2017-09-13
"""

import itertools as it
import functools
import operator
import collections
import math
import sys

def solve(xs):
    return 1 + sum(1 for (i, j) in zip(xs, xs[1:]) if (i, j) != (1, 0))

# def pp(*args, **kwargs):
#     return print(*args, file=sys.stderr, **kwargs)

def main():
    n = int(input())
    xs = [int(c) for c in input().split()]
    assert len(xs) == n
    result = solve(xs)
    print(result)

if __name__ == '__main__':
    main()

from sys import stdin, stdout

n = int(stdin.readline())
values = list(map(int, stdin.readline().split()))
dp = [[0, 0] for i in range(n)]

for i in range(n):
    if not values[i]:
        dp[i][0] = 1

        for j in range(i - 1, -1, -1):
            dp[i][0] = max(dp[i][0], dp[j][0] + 1)
    else:
        dp[i][1] = 1

        for j in range(i - 1, -1, -1):
            dp[i][1] = max(dp[i][1], max(dp[j]) + 1)

ans = 0
for i in range(n):
    ans = max(dp[i])

stdout.write(str(ans))

n = int(input())

s = list(map(int, input().split()))

res = n

for i in range(n - 1):
    if(s[i] == 1 and s[i + 1] == 0):
        res -= 1

print(res)

import sys
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
m = 0
d = Counter(a)
for i in range(d[0]):
    j = 0
    k = 0
    while k != i:
        if a[j] == 0:
            k += 1
        j += 1
    while j < n:
        if a[j] == 1:
            k += 1
        j += 1
    m = max(m,k)
print(m)

n = int(input())
arr = input()
arr = arr.split(' ')
arr = list(map(int, arr))
for i in range(1, n):
    if arr[i] == 0 and arr[i-1] == 1:
        n = n - 1
print(n)

n = int(input())
s = [int(i) for i in input().split()]
k = 0
for i in range(1, len(s)):
    if s[i] == 0 and s[i - 1] == 1:
        k += 1
print(len(s) - k)

def solve(n, games):
    zeros_after_ones = 0
    was_one = False
    for game in games:
        if game == 1:
            was_one = True
        if was_one and game == 0:
            zeros_after_ones += 1

    ones_before_zeros = 0
    was_zero = 0
    for game in reversed(games):
        if game == 0:
            was_zero = True
        if was_zero and game == 1:
            ones_before_zeros += 1

    games_to_del = min(ones_before_zeros, zeros_after_ones)
    return n - games_to_del

def main():
    n = int(input())
    games = list(map(int, input().split()))

    print(solve(n, games))

main()

n=int(input())
s=[int(c) for c in input().split()]
res=0

for i in range(n):
    if s[i]==0 and s[i-1]==1:
        n=n
    else:
        res+=1
print(res)

size = int(raw_input())
org_arr = map(int,raw_input().split())

freq_arr = []

if size == 1:
    print '1'
else:

    for i in range(len(org_arr)):
        freq = 0
        for j in range(len(org_arr)):
            if j < i  and org_arr[j] == 0:
                freq += 1
            elif j>=i and org_arr[j] == 1:
                freq +=1

        freq_arr.append(freq)

    print max(freq_arr)

n=int(input())
s=[]
for x in input().split():
    s.append(int(x))
s.reverse()
k=1
c1=0
c0=0
for i in range(n):
    if s[i]==1:
        k=i
        c1+=1
for i in range(i):
    if s[i]==0:
        c0+=1
if c1>=c0:
    while c0!=0:
        s.remove(0)
        c0-=1
elif c1<c0 and s[0]!=1:
    while c1!=0:
        s.remove(1)
        c1-=1
print(*s)
print(len(s))

n = int(input())
games = list(map(int, input().split()))

strike = [0] * n
strike[0] = 1

for i in range(1, n):
	if games[i] < games[i - 1]:
		strike[i] = strike[i - 1]
	else:
		strike[i] = strike[i - 1] + 1

print(strike[-1])

n=int(input())
l=[int(i) for i in input().split()]
a=l.count(1)
b=l.count(0)
print(n-min(a,b))

n = int(raw_input().strip())
a = map(int, raw_input().strip().split(" "))
saw1 = False
removed = 0
for i in xrange(n - 1):
    if a[i] == 1 and a[i + 1] == 0:
        removed += 1

print n - removed

n = int(input())
a = list(map(int, input().split()))
delete0 = n - a[a.index(1) if 1 in a else 0:-1].count(0)
a.reverse()
delete1 = n - a[a.index(0) if 0 in a else 0:-1].count(1)
print(max(delete0, delete1))

n=int(input())
s=input()
res1=s[:s.find('1')].count('0')+s.count('1')
res2=s[s.rfind('0'):].count('1')+s.count('0')
print(max(res1,res2))

input()
n = [int(x) for x in input().split()]
a = 0
f = 0
for x in n:
    if x:
        f=1
    else:
        if f:
            a+=1
print(max( len(n)-a,  len([x for x in n if not x])  ))

def is_good_resume(games):
    success_flag = False
    for game in games:
        if game == 0 and success_flag:
            return False
        elif game == 1 and not success_flag:
            success_flag = True
    else:
        return True

n = int(raw_input())
resume_games = 0

games = map(int, raw_input().split())

successful_games = games.count(1)
unsuccessful_games = games.count(0)

if is_good_resume(games):
    print n
else:
    while True:
        for i in range(len(games)):
            if games[i] == 1:
                del games[i]
                break
            if games[len(games) - 1 - i] == 0:
                del games[len(games) - 1 - i]
                break
        if is_good_resume(games):
            resume_games = len(games)
            break
    print max([resume_games, successful_games, unsuccessful_games])

n = int(input())
p = list(map(int, input().split()))
ans = n
for i in range(1, n):
    if p[i] == 0 and p[i - 1] == 1:
        j = i
        k1 = 1
        while j < n - 1 and p[j + 1] == 0:
            k1 += 1
            j += 1
        j = i
        k2 = 0
        while j > 0 and p[j - 1] == 1:
            k2 += 1
            j -= 1

        if k1 < k2:
            ans -= k1
        else:
            ans -= k2
print(ans)

n=int(input())
k=-1
l=list(map(int,input().split()))
for i in range(n+1):
    s=l[:i].count('0')+l[i:].count('1')
    if(s>k):
        k=s
print(k)

n = int(input())
s = list(map(int, input().split()))

ans = 0
j1 = 0
while j1 < n and s[j1] == 0:
    ans += 1
    j1 += 1

j2 = n-1
while j2 > j1 and s[j2] == 1:
    ans += 1
    j2 -= 1

ctr0 = 0
ctr1 = 0
last = 0

for i in range(j1, j2+1):
    if s[i] == 0:
        ctr0 += 1
    else:
        ctr1 += 1

if ctr0 > ctr1:
    ans+= ctr0
else:
    ans+= ctr1

print(ans)

n =int(raw_input())

m = map(int, raw_input().split())

one = 0
zero = 0
for i in range(n):
	if(m[i]==1):
		one+=1
	else:
		zero+=1
for i in m:
	if(i==0):
		zero-=1
	else:
		break
if(zero>one):
	print n-one
else:
	print n-zero

n = int(input())
g = list(map(int, input().split()))
ans = [g[0]]
for i in range(1, len(g)):
    if ans[-1] == 1 and g[i] == 0:
        ans.pop()
        ans.append(0)
    else:
        ans.append(g[i])
print(len(ans))

n = int(input())
s = list(map(int, input().split()))

ans = 0
ans1 = 0
ans2 = 0
j1 = 0
while j1 < n and s[j1] == 0:
    ans1 += 1
    j1 += 1

j2 = n-1
while j2 > j1 and s[j2] == 1:
    ans2 += 1
    j2 -= 1

ctr0 = 0
ctr1 = 0
last = 0

for i in range(j1, j2+1):
    if s[i] == 0:
        ctr0 += 1
    else:
        ctr1 += 1

if ctr0 + ans1 > ctr1 + ans2:
    ans = ctr0 + ans1
else:
    ans = ctr1 + ans2

print(ans)

n = int(input())
a = list(map(int, input().split()))
#n = len(a)
delete0 = n - a[a.index(1) if 1 in a else 0:].count(0)
a.reverse()
delete1 = n - a[a.index(0) if 0 in a else 0:].count(1)
print(max(delete0, delete1))

from sys import stdout
from random import randint
from math import *
import re

n = int(input())
s = input().replace(' ', '')
temp = re.findall("1.*", s)
if not temp:
    ans = len(s)
else:
    ns = temp[0]
    ans = len(s) - len(ns) + max(ns.count('1'), ns.count('0'))

print(ans)

n = int(input())
g = list(map(int, input().split()))
ans = [g[0]]
for i in range(1, len(g)):
    if g[i-1] == 1 and g[i] == 0:
        do = 'nothing'
    else:
        ans.append(g[i])
#    print(ans)
#    print(i)
print(len(ans))

n = int(input())
s = input()
maxs=[]
for i in range(n):
	curr = s[i]
	maxi = 1
	for j in range(i+1, n):
		if curr == '0':
			maxi += 1
			curr = s[j]
		elif s[j] == '0':
			continue
		else:
			maxi +=1
	maxs.append(maxi)
maxi=0
for x in s:
	if x == '0':
		maxi += 1
maxs.append(maxi)
print(max(maxs))

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 20 23:21:21 2017

@author: soham
"""

n=int(raw_input())
s=map(int,raw_input().split())

memo=[0]*n
ones=[0]*n

cnt=0
for i in range(0,n):
    if s[i]==0:
        cnt+=1
    else:
        memo[i]=cnt
        cnt=0

cnt=0
for i in range(n-1,-1,-1):
    if s[i]==1:
        cnt+=1
        ones[i]=cnt

ans=0

for i in range(0,n):
    if s[i]==0:
        ans+=1

for i in range(0,n):
    if s[i]==1:
        temp=memo[i]+ones[i]
        ans=max(ans,temp)

print ans

n = int(input())
s = [int(i) for i in input().split()]
print(max([s[:i].count(1) + s[i:].count(0) for i in range(len(s) + 1)]))

n = int(input())

l = list(map(int, input().split()))

max_games = [0 for i in range(n)]

max_games[0] = 1
if n > 1:
    max_games[1] = 2 if (l[1] - l[0]) != -1 else 1

if n > 2:
    for i in range(2, n):
        if l[i] == 0:
            if l[i - 1] == l[i - 2] == 1:
                max_games[i] = 1
            elif l[i - 1] == 1:
                max_games[i] = max_games[i - 2] + 1
            elif l[i - 2] == 1:
                max_games[i] = max_games[i - 1] + 1
            else:
                max_games[i] = max([max_games[i - 1], max_games[i - 2]]) + 1
        else:
            max_games[i] = max([max_games[i - 1], max_games[i - 2]]) + 1

print(max(max_games))

n = int(input())
arr = list(map(int, input().split()))
ans = arr.count(0)
for i in range(1, n + 1):
    one = 0
    ind = -1
    j = n - 1
    for j in range(n -1, -1, -1):
        if one == i:
            ind = j
            break
        if arr[j] == 1:
            one += 1
    else:
        if one == i:
            ind = j
        else:
            break
    ans = max(ans, i + arr[:ind].count(0))
print(ans)

n = int(input())
lis = [int(e) for e in input().split()]
ct = []
c0 = 0
c1 = 0
for i in range(len(lis)):
    if lis[i]==0:c0+=1
    if lis[i]==1:c1+=1
ct.append(c0)
ct.append(c1)

for i in range(1,len(lis)-1):
    c0 = 0
    c1 = 0
    for j in range(i-1,-1,-1):
        if lis[j]==0:
            c0+=1
    for j in range(i,len(lis)):
        if lis[j]==1:
            c1+=1
    #print("c0=",c0)
    #print("c1=",c1)
    ct.append(c0+c1)

#print(ct)
print(max(ct))

# import sys
# sys.stdin = open('in', 'r')

n = input()
s = input()

print(max([s[:i].count('0') + s[i:].count('1') for i in range(len(s))]))

n = int(input())
lst = list(map(int,input().split()))
for i,x in enumerate(lst):
    if x==1:break
one,zero=0,0
for j in range(i,n):
    if lst[j]==1:one+=1
    else:zero+=1
print(i+max(one,zero))

n = int(raw_input())
ns = map(int, raw_input().split())

m = sum(ns)
ans = 0

cnt = 0
l, r = 0, n
for i in xrange(n):
    if ns[i] == 1:
        cnt += 1
    l += 1
    r -= 1
    ans = max(ans, (l - cnt) + (m - cnt))
print ans

def main():
	n = int(input())
	games = list(map(int, input().split(' ')))

	cnt = 0
	for i in range(0, len(games)):
		if games[i] == 1:
			cnt = cnt + 1

	result = len(games) - cnt
	print(result)

if __name__ == "__main__":
	main()

from time import sleep
n = int(input())
s = list(map(int, input().split()))

index = n - 1
while index > 0 and s[index]:
    index -= 1

answer = n - index - 1
s = s[:index + 1]
# index = 0
# index = 0
# answer = 0
z = s.count(0) - 1
# o = s.count(1)
# if

# print(answer)

now = 0

while s:
    while s and s[0] == 0:
        s = s[1:]
        answer += 1
    # print(s)
    z = s.count(0)
    o = s.count(1)
    # print(z, o)
    if o >= z:
        answer += o
        break
    else:
        answer += 1
    # x -= 1
    # s = s[index + 1:]
    s = s[s.index(0) + 1:]
print(answer)

n = input()
nums = map(int, raw_input().split())

cumsum = [0]
for num in nums:
	cumsum.append(cumsum[-1] + num)

max_ = 1
# nums[i] + ... nums[j] = cumsum[j+1] - cumsum[i]  # cumsum is 1 based. nums is 0 based.
for idx in xrange(1, n+1):
	# 0 0 0 0....1....1 1 1.
	if nums[idx-1] == 1:
		# count all zeros before idx and all ones after idx.
		all_zeros_before = (idx-1) - cumsum[idx-1]
		all_ones_after = cumsum[n] - cumsum[idx-1]
		max_ = max(max_, all_ones_after + all_zeros_before)

print max_

n = int(input())
a = [int(i) for i in input().split()]
fz = 0
while len(a) > 0 and a[0] == 0:
    fz += 1
    a = a[1:]
mans = [0]
o = a.count(1)
for i in range(o):
    q = 0
    j = 0
    z = 0
    while j < len(a) and q <= i:
        if j == 1:
            q += 1
        else:
            z += 1
            q += 1
    mans.append(o + z - i)
print(fz + max(mans))

def read_ints():
	return [int(i) for i in input().split()]

n = int(input())
s = read_ints()
to_rem = n if sum(s) else 0
for i in range(n):
	if s[i]:
		to_rem = min(to_rem, s[:i].count(1) + s[i:].count(0))

print(n - to_rem)

n = int(input())
arr = list(map(int, input().split()))
ans = 0
z = arr.count(0)
o = arr.count(1)
i = n - 1
if n == 1:
    print(arr[0])
    exit()
if z == n:
    print(z)
    exit()
while arr[i] == 1 and i >= 0:
    i -= 1
    ans += 1
    o -= 1
beg = -1
for j in range(n):
    if arr[j] == 0:
        ans += 1
        z -= 1
    else:
        beg = j - 1
        break
while i > beg:
    #print(i, o, z, ans)
    if o > z:
        while arr[i] == 0 and i > beg:
            z -= 1
            i -= 1
        while arr[i] == 1 and i > beg:
            o -= 1
            i -= 1
            #print('!', i, o, z, ans + 1)
            ans += 1
    else:
        #print(i + 1)
        while arr[i] == 0 and i > beg:
            z -= 1
            i -= 1
            ans += 1
        while arr[i] == 1 and i > beg:
            o -= 1
            i -= 1
print(ans)

n=int(input())
l=[int(i) for i in input().split()]
cnt=0
for i in range(n-1,-1,-1):
    if l[i]==1:
        cnt+=1
        ind=i
    else:
        break
ind1=0
cnt1= 0
for i in range(n):
    if l[i]==0:
        cnt1+=1
        ind1=i+1
    else:
        break
if cnt==n:
    print(n)
    exit()
if cnt==0:
    ind=n
l=l[ind1:ind]
print(cnt+cnt1+max(l.count(0),l.count(1)))

n = input()
a = map(int, raw_input().split())

z = 0
zeros = [0] * len(a)
for i in range(len(a)):
    zeros[i] = z
    if a[i] == 0:
        z += 1

o = 0
ones = [0] * len(a)
for i in range(len(a)-1, -1, -1):

    if a[i] == 1:
        o += 1
    ones[i] = o

max_num = 1
for i in range(len(a)):
    t = ones[i] + zeros[i]
    max_num = max(t, max_num)

if o == 0:
    print z
else:
    print max_num

n = int(input())
val = list(map(int, input().split()))
s = sum(val)
j = 0
k = 0
for elem in val:
    if elem == 0:
        j += 1
    else:
        break
for elem in val[::-1]:
    if elem == 1:
        k += 1
    else:
        break
print(max(s + j, n - s + k))

n = int(input())
nums = list(map(int, input().split()))

d = [0, 0]

for i in nums:
    if i == 0:
        d[0] += 1
    else:
        d[0] += 1
        d[1] += 1

print(max(d[0], d[1]))

def rezume(lst):
    a, b = [0] * len(lst), [0] * len(lst)
    a[0], b[len(lst) - 1] = 0, 0
    for i in range(1, len(lst)):
        a[i] = a[i - 1] + (not lst[i])
    for i in range(len(lst) - 2, -1, -1):
        b[i] = b[i + 1] + lst[i]
    result = 0
    for i in range(len(lst)):
        result = max(result, a[i] + b[i])
    return result + 1

n = int(input())
c = [int(j) for j in input().split()]
print(rezume(c))

N = input()
A = map(int, raw_input().split())

if 0 not in A or 1 not in A:
    print N
    exit()

i = 0
j = N-1
while A[i] == 0:
    i += 1
while A[j] == 1:
    j -= 1

cnt0 = A[i:j+1].count(0)
cnt1 = A[i:j+1].count(1)
print i + N - j - 1 + max(cnt0, cnt1)

n=int(input())
s=[int(a) for a in input().split()]
i=n-1
x=0
y=0
while i>=0 and s[i]==1:
    i-=1
while i > -1:
    if s[i]==1:
        x+=1
    i-=1
i=0
while i<n and s[i]==0:
    i+=1
while i < n:
    if s[i]==0:
        y+=1
    i+=1
print(n-min(x,y))

def main():
	n = int(input())
	games = [0]
	games = games + (list(map(int, input().split(' '))))

	cnt = 0
	for i in range(1, len(games)):
		if games[i] == 0:
			if games[i-1] ==1:
				cnt = cnt+1

	result = len(games) -1 - cnt
	print(result)

if __name__ == "__main__":
	main()

n = int(input())
s = list(map(int, input().split()))

# index = 0
# while index < len(s) and not s[index]:
#     index += 1

# answer = index
# s = s[index:]
# index = 0
index = 0
answer = 0

while s:
    o = 0
    while index < len(s) and s[index]:
        o += 1
        index += 1
    z = 0
    while index < len(s) and not s[index]:
        z += 1
        index += 1

    answer += max(o, z)
    s = s[index:]
    index = 0
print(answer)

input()
n = [int(x) for x in input().split()]
a = -1
aa = 0
for i, x in enumerate(n):
    if x:
        continue
    aa+=1
    aaa = aa + len([x for x in n[i+1:] if x])
    if aaa > a:
        a = aaa
print(a)

n = int(input())
res = list(map(int, input().split()))
ans = []

for i in range(len(res)):
    if res[i] == 0 and res[i:].count(0) > res[i:].count(1):
        ans.append(0)
    if res[i] == 1 and res[i:].count(1) > res[i:].count(0):
        ans.append(1)
if res[-1] == 1: ans.append(1)
print(len(ans))

T = input()
n = map(int, raw_input().split())
count = 1
for y in range(1,T):
    if(n[y-1] == 1 and n[y]==0):
        pass
    else:
        count += 1
print count,

n=input()
s=raw_input().split()
cnt=0
cnt1=0
ans=-1
for i in range(n):
    cnt=0
    for j in range(i):
        if int(s[j])==0:cnt+=1
    for j in range(i,n):
        if int(s[j])==1: cnt+=1
    ans=max(cnt,ans)
print ans

n = int(raw_input().strip())
a = map(int, raw_input().strip().split(" "))
best = [[0 for j in xrange(2)] for i in xrange(n)]
if a[0] == 0:
    best[0][0] = 1
else:
    best[0][1] = 1
for i in xrange(1, n):
    if a[i] == 0:
        best[i][0] = best[i - 1][0] + 1
        best[i][1] = best[i - 1][1]
    else:
        best[i][0] = best[i - 1][0]
        best[i][1] = best[i - 1][1] + 1
print max(best[n - 1])

n=int(input())
s=[int(a) for a in input().split()]
ans = 0
for i in range(n):
    g = 0
    for j in range(i):
        if s[j]==0:
            g+=1
    for j in range(i,n):
        if s[j]==1:
            g+=1
    if g > ans:
        ans = g
print(ans)

'''
i=n-1
x=0
y=0
while i>=0 and s[i]==1:
    i-=1
while i > -1:
    if s[i]==1:
        x+=1
    i-=1
i=0
while i<n and s[i]==0:
    i+=1
while i < n:
    if s[i]==0:
        y+=1
    i+=1
print(n-min(,y))
'''

n = int(input())
s = list(map(int, input().split()))
n = len(s)

ans = 0
ans2 = 0
j1 = 0
while j1 < n and s[j1] == 0:
    ans += 1
    j1 += 1

j2 = n-1
while j2 > 0 and s[j2] == 1:
    ans2 += 1
    j2 -= 1

ctr0 = 0
ctr1 = 0

for i in range(j1, j2+1):
    if s[i] == 0:
        ctr0 += 1
    else:
        ctr1 += 1

if ctr0  >= ctr1 and (ans2 > 0) :
    ans += ctr0
    ans += ans2
else:
    ans += ctr1
    ans += ans2

print(ans)

from sys import stdin, stdout

n = int(stdin.readline())
values = list(map(int, stdin.readline().split()))
dp = [[0, 0] for i in range(n)]

for i in range(n):
    if not values[i]:
        dp[i][0] = 1

        for j in range(i - 1, -1, -1):
            dp[i][0] = max(dp[i][0], dp[j][0] + 1)
    else:
        dp[i][1] = 1

        for j in range(i - 1, -1, -1):
            dp[i][1] = max(dp[i][1], max(dp[j]) + 1)

stdout.write(str(max(dp[-1])))

N = int(input())
A = list(map(int, input().split()))

ans = -1
for i in range(N):
    if A[i] == 1:
        pref = sum(1 for x in A[:i] if x == 0)
        suff = sum(1 for x in A[i:] if x == 1)
        ans = max(ans, pref + suff)

ans = N if ans == -1 else ans
print(ans)

n = int(input())
s = [int(i) for i in input().split()]
print(max([s[:i].count(1) + s[i:].count(0) for i in range(len(s) + 1)]))

#Bhargey Mehta (Sophomore)
#DA-IICT, Gandhinagar
import sys, math, queue, bisect
#sys.stdin = open("input.txt", "r")
MOD = 10**9+7
sys.setrecursionlimit(1000000)

n = int(input())
ans = n
a = list(map(int, input().split()))
for i in range(1, n):
    if a[i] == 0 and a[i-1] == 1:
        ans -= 1
print(ans)

import sys
from collections import Counter
n = int(input())
a = ''.join(input().split())
i = 0
k = 0
s,e = 0,0
while i < n and a[i] == '0':
    k += 1
    i += 1
s = i
i = n-1
while i >= 0 and a[i] == '1':
    k += 1
    i -= 1
e = i+1
d = Counter(a[s:e])
print(k+max(d['0'],d['1']))

length = int(input())
array = list(map(int, input().split()))

index = 0
while(array[index] == 0):
    index += 1

    if index == length:
        break

print(max(array[index:].count(1), array.count(0)))


a, b=list(map(int, input().split()))
q=list({1, 2, 3}-{a, b})[0]
print(q)

a, b = map(int, input().split())
numbers = 3+2+1
c = numbers - a - b
print(c)

print(6 - sum([int(i) for i in input().split()]))

a, b = [int(x) for x in input().split()]
for i in range(1, 4):
    if i != a and i != b:
        print(i)

a, b = map(int, input().split())
for i in range(1, 4):
    if (i != a and i != b):
        print(i)
        break

m,n = map(int,input().split())
if (m + n) != 3:
    print(abs(m-n))
else:
    print(m+n)

a, b = map(int, input().split())
if a != 1 and b!= 1:
    print(1)
elif a != 2 and b != 2:
    print(2)
else:
    print(3)

a, b = map(int, input().split())
print(6//(a*b))

s = 6
x1, x2 = input().split()
x1, x2 = int(x1), int(x2)
print(s-x1-x2)

a, b = input().split()
if int(a)+int(b)==3:
    print('3')
if int(a)+int(b)==4:
    print('2')
if int(a)+int(b)==5:
    print('1')

a , b = input().split()
print( 6 - (int(a) + int(b)) )

s1 = [int(x) for x in input().split()]
s2 = (1,2,3)
for i in s2:
    if i not in s1:
        print(i)

a = {1,2,3}
x = set(map(int,input().split()))
b = list(a-x)
print(b[0])

a, b = map(int, input().split())
print(a ^ b)

n,m = map(int,input().split(' '));print(6-(m+n));

cH = input()
a, b = map(int, cH.split())
c = 6 - a - b
print(c)

print(6 - sum(map(int, input().split())))

a, b = map(int, input().split())

print(sum(i for i in range(1, 4) if i not in (a, b)))

print(*{1, 2, 3} - set(map(int, input().split())))

print(6 - sum(list(map(int, input().split()))))

inp = list(map(int,input().split(" ")))
print(6-sum(inp))

bros = set(map(lambda x: x+1, range(3)))

a, b = map(lambda x: int(x), input().split())
bros.remove(a)
bros.remove(b)
print(bros.pop())

a,b=input().split()
a=int(a)
b=int(b)
c=None
if a==1 and b==2:
    c=3
    print(c)
if a==2 and b==1:
    c=3
    print(c)
if a==3 and b==2:
    c=1
    print(c)
if a==2 and b==3:
    c=1
    print(c)
if a==1 and b==3:
    c=2
    print(c)
if a==3 and b==1:
    c=2
    print(c)

# encoding: utf-8
a = input()
b = []
b = a.split(' ')
n = 0
for x in b:
    b[n] = int(x)
    n += 1
a = b[1] + b[0]
if a == 4:
    print('2')
elif a == 3:
    print('3')
else:
    print('1')

a, b = map(int, input().split())
if a == 1 and b == 2 or a == 2 and b == 1:
    print(3)
elif a == 3 and b == 1 or a == 1 and b == 3 :
    print(2)
elif a == 2 and b == 3 or a == 3 and b == 2 :
    print(1)
else :
    print(0)

def read_ints():
    return [int(x) for x in input(' ').split()]

def main():
    a, b = read_ints()
    print(({1, 2, 3}.difference({a, b})).pop())

if __name__ == '__main__':
    main()

inStr = input()
a,b=inStr.split()
print(6-int(a)-int(b))

n,k=(int(z) for z in input().split())
print(6-n-k)

a, b = map(int, input().split())

if a == 1:
    if b == 2:
        print(3)
    else:
        print(2)
elif a == 2:
    if b == 1:
        print(3)
    else:
        print(1)
else:
    if b == 1:
        print(2)
    else:
        print(1)

a = [0,0,0]
inp = input()
inp = inp.split()
a[int(inp[0]) - 1] += 1
a[int(inp[1]) - 1] += 1
for i in range(len(a)):
    if a[i] == 0:
        print(i + 1)

s = input()
ids = s.split(' ')
normal_ids = ['1','2','3']
normal_ids.remove(ids[0])
normal_ids.remove(ids[1])
print(normal_ids[0])

a, b = map(int, input().split())
print(6 - a - b)

a, b = list(map(int,input().split()))

s = a + b
c = 6
print(c-s)

a, b = map(int, input().split())
p = [1, 2, 3]
p.remove(a)
p.remove(b)
print(p[0])

a, b = input().split()
a = int(a)
b = int(b)
c = 6- (a+b)
print(c)

a, b = map(int, input().split())
A = [a, b]
if not(1 in A):
    print(1)
elif not(2 in A):
    print(2)
else:
    print(3)

s=[1,2,3]
a,b=map(int,input().split())
s.remove(a)
s.remove(b)
print(*s)

a, b = set(map(int, input().split()))
print(*({1, 2, 3} ^ {a, b}))

a = [int(i) for i in input().split()]
print(6-a[0]-a[1])

a,b = input().split()
print(6-int(a)-int(b))

k = {1, 2, 3}
r = set([int(s) for s in input().split()])
print(*(k-r))

a=set(map(int,input().split()))
b=()
for i in range(1,4):
    if i not in a:
        print(i)

a, b = map(int, input().split())
for i in range(1, 4):
    if(i != a and i != b):
        print (i)

A = {1, 2, 3}
B = set (map (int, input().split()))
print(*A.difference(B))

pr = [str(i) for i in input().split()];
bro = ["1","2","3"];
bro.remove(pr[0]);
bro.remove(pr[1]);
print(bro[0]);

a,b = input().split(' ')
print(str(int(a) ^ int(b)))

a, b = map(int,input().split())
print(6-a-b)

a,b=[int(i) for i in input().split()]
print(6-a-b)

print(list({1, 2, 3}.difference({int(i) for i in input().split()}))[0])

a = {1, 2, 3} - set(map(int, input().split()))
for elem in a: print(elem)

a,b = map(int,input().split())
c = []
c.append(a)
c.append(b)
c.sort()
if c[0] == 1 and c[1]==2:
    print('3')
elif c[0] ==1 and c[1] == 3:
    print('2')
elif c[0] ==2 and c[1] == 3:
    print('1')

a, b = list (map(int,input().split()))
if (a==1 or b==1) and (a==2 or b==2):
    print (3)
elif (a==1 or b==1) and (a==3 or b==3):
    print (2)
else:
    print(1)

print(6 - sum(map(int, input().split())))

a, b = list(map(int, input().split()))

print(6 - a - b)

a,b = map(int, input().split())
c = a + b
if c == 3:
    print(3)
elif c == 4:
    print(2)
elif c == 5:
    print(1)

l = list(map(int, input().split()))
if 1 not in l:
    print(1)
if 2 not in l:
    print(2)
if 3 not in l:
    print(3)

ind = map(int,raw_input().split(' '))
mas = [1,2,3]
for i in mas:
    if i not in ind:
        print i
        break

a,b = map(int, raw_input().split(' '))
print(6-a-b)

ab = raw_input("").split(' ')

c = 6 - int(ab[0]) - int(ab[1])
print(`c`)

a, b = map(int, raw_input().split(" ")[0:2])
for i in range(1, 4):
    if i != a and i != b : print i

brat = [1, 2, 3]
a, b = (int(i) for i in input().split())
brat.remove(a)
brat.remove(b)
print(brat[0])

v=input()
a=int(v[0])
b=int(v[2])
print(6-a-b)

a = ['1', '2', '3']
x, y = input().split()
for i in range(3):
    if (a[i] != x) and (a[i] != y):
        z = a[i]
print(z)

print(str(6 - sum(map(int, input().split()))))

s1 = (int(x) for x in input().split())
s2 = (1,2,3)
for i in s2:
    if i not in s1:
        print(i)

a = input('HOMEPA 6PATbEB\n')
b = []
b = a.split(' ')
n = 0
for x in b:
    b[n] = int(x)
    n += 1
a = b[1] + b[0]
if a == 4:
    print('2')
elif a == 3:
    print('3')
else:
    print('1')

s = input().split()
for i in range(len(s)):
    s[i] = int(s[i])
a = s[0]
b = s[1]
if a==1 and b==2:
    c = 3
elif a==1 and b==3:
    c = 2
elif a==2 and b==1:
    c = 3
elif a==2 and b==3:
    c = 2
elif a==3 and b==1:
    c = 2
elif a==3 and b==2:
    c = 1
print(c)

s = input().split()
for i in range(len(s)):
    s[i] = int(s[i])
a = s[0]
b = s[1]
if a==1 and b==2:
    c = 3
elif a==1 and b==3:
    c = 2
elif a==2 and b==1:
    c = 3
elif a==2 and b==3:
    c = 2
elif a==3 and b==1:
    c = 2
elif a==3 and b==2:
    c = 1
elif a==b:
    c = 0
print(c)