Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job.

During all his career Hideo has produced n games. Some of them were successful, some were not. Hideo wants to remove several of them (possibly zero) from his CV to make a better impression on employers. As a result there should be no unsuccessful game which comes right after successful one in his CV.

More formally, you are given an array s_1, s_2, ..., s_{n} of zeros and ones. Zero corresponds to an unsuccessful game, one — to a successful one. Games are given in order they were produced, and Hideo can't swap these values. He should remove some elements from this array in such a way that no zero comes right after one.

Besides that, Hideo still wants to mention as much games in his CV as possible. Help this genius of a man determine the maximum number of games he can leave in his CV.


-----Input-----

The first line contains one integer number n (1 ≤ n ≤ 100).

The second line contains n space-separated integer numbers s_1, s_2, ..., s_{n} (0 ≤ s_{i} ≤ 1). 0 corresponds to an unsuccessful game, 1 — to a successful one.


-----Output-----

Print one integer — the maximum number of games Hideo can leave in his CV so that no unsuccessful game comes after a successful one.


-----Examples-----
Input
4
1 1 0 1

Output
3

Input
6
0 1 0 0 1 0

Output
4

Input
1
0

Output
1

Difficulty: MEDIUM
Tags: Implementation

Complete search
Skill types: Complete search

Starter code:

Solutions:
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))


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))


import sys
cases = sys.stdin.readline()
numbers = [int(x) for x in sys.stdin.readline().strip().split(' ')]
my_sum = sum(numbers)
answers = []
ones_so_far = 0
zeroes_so_far = 0
index = 0
for x in numbers:
	if x == 1:
		ones_so_far += 1
	else:
		zeroes_so_far += 1
	remaining_ones = my_sum - ones_so_far
	remaining_zeroes = len(numbers) - remaining_ones - index - 1
	a = max(ones_so_far, zeroes_so_far) + remaining_ones
	b = zeroes_so_far + remaining_zeroes
	index += 1
	answers.append(max(a, b))
print(max(answers))


n = int(input())
seq = list(input().split(' '))
totalzeroes = seq.count('0')
ans = totalzeroes
(ones, zeroes) = (0, 0)
for s in seq[::-1]:
	if s == '1':
		ones += 1
	else:
		zeroes += 1
	ans = max(ans, totalzeroes + ones - zeroes)
print(ans)


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)


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)


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


input()
a = list(map(int, input().split()))
f = 0
for i in range(len(a)):
	s = 0
	for j in range(len(a)):
		if j <= i:
			if a[j] == 0:
				s += 1
		if j >= i:
			if a[j] == 1:
				s += 1
	if s > f:
		f = s
print(f)


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(input())
p = list(map(int, input().split()))
ind_z = []
ind_o = []
for i in range(n):
	if p[i] == 0:
		ind_z.append(i)
	else:
		ind_o.append(i)
z = [0] * len(ind_z)
o = [0] * len(ind_o)
for i in range(len(z)):
	z[i] = i + 1
	for j in range(ind_z[i], n):
		if p[j] == 1:
			z[i] += 1
for i in range(len(o)):
	o[i] = len(o) - i
	for j in range(ind_o[i] - 1, -1, -1):
		if p[j] == 0:
			o[i] += 1
z = [0] + z
o = [0] + o
print(max(max(o), max(z)))


import sys
input = sys.stdin.readline
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left, bisect
import math
from operator import itemgetter
from heapq import heapify, heappop, heappush
n = int(input())
l = list(map(int, input().split()))
(c1, c2) = (0, 0)
for i in l:
	if i == 0:
		c1 += 1
(x, y) = (0, 0)
m = 1
for i in l:
	if i == 0:
		x += 1
	else:
		c = y + c1 - x
		m = max(m, n - c)
		y += 1
print(max(m, max(c1, n - c1)))


N = 107
n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
f = [[0 for j in range(N)] for i in range(N)]
f[0][0] = 0
f[1][0] = 0
f[1][1] = 1
for i in range(2, n + 1):
	for j in range(i):
		if a[j] == 0 or (a[j] == 1 and a[i] == 1):
			f[i][i] = max(f[i][i], f[i - 1][j] + 1)
		f[i][j] = max(f[i][j], f[i - 1][j])
res = 0
for i in range(n + 1):
	res = max(res, f[n][i])
print(res)


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)


n = int(input())
q = input().split(' ')
t = q.count('0')
a = t
o = 0
z = 0
for s in q[::-1]:
	if s == '1':
		o += 1
	else:
		z += 1
	a = max(a, t + o - z)
print(a)


def solve(n, games):
	games_to_keep = 0
	ones_prefix = [0 for i in range(n)]
	if games[0] == '1':
		ones_prefix[0] = 1
	for i in range(1, n):
		ones_prefix[i] = ones_prefix[i - 1]
		if games[i] == '1':
			ones_prefix[i] += 1

	def ones_after(pos):
		return ones_prefix[-1] - ones_prefix[pos]
	zeros_cnt = 0
	for (i, game) in enumerate(games):
		if game == '0':
			zeros_cnt += 1
			ones_after_zero = ones_after(i)
			games_lasts = zeros_cnt + ones_after_zero
			if games_lasts > games_to_keep:
				games_to_keep = games_lasts
	ones_count = ones_prefix[-1]
	zeros_count = n - ones_count
	games_to_keep = max(games_to_keep, ones_count, zeros_count)
	return games_to_keep

def main():
	n = int(input())
	games = input().split()
	print(solve(n, games))
main()


length = int(input())
array = list(map(int, input().split()))
count = []
total = 0
for k in range(length):
	if array[length - 1 - k] == 1:
		total += 1
	count.append(total)
count.reverse()
total = 0
maximum = 0
for k in range(length):
	if array[k] == 0:
		total += 1
	maximum = max(maximum, total + count[k])
print(maximum)


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())
s = list(map(int, input().split()))
print(max([s[:i].count(0) + s[i:].count(1) for i in range(n + 1)]))


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 = input()
a = [int(x) for x in input().split()]
sum0 = 0
sum1 = 0
for i in a:
	if i == 1:
		sum1 = max(sum1 + 1, sum0 + 1)
	else:
		sum0 = sum0 + 1
print(max(sum1, sum0))


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)


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())
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())
t = list(map(int, input().split()))
print(max([i - sum(t[:i]) + sum(t[i:]) for i in range(n + 1)]))


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


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())
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())
projects = input().split(' ')
best = 0
for i in range(len(projects)):
	cur = 1
	cur += projects[:i].count('0')
	cur += projects[i + 1:n].count('1')
	best = max(best, cur)
print(best)


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())
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())
s = list(map(int, input().split()))
ans = 0
x0 = 0
x1 = 0
for i in range(n):
	if s[i] == 1:
		x1 = max(x0, x1) + 1
	else:
		x0 = x0 + 1
	ans = max(x0, x1)
print(ans)


INF = 10 ** 9 + 7

def cutoff(games, pos):
	leave = 0
	for (i, g) in enumerate(games):
		if i < pos:
			if g == 0:
				leave += 1
		elif 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)

def __starting_point():
	main()
__starting_point()


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)
main()


n = int(input())
m = 0
ls = list(map(int, input().split()))
for i in range(n + 1):
	s = 0
	for j in range(i):
		s += not ls[j]
	for j in range(i, n):
		s += ls[j]
	m = max(s, m)
print(m)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	ans = max(sum(a), n - sum(a))
	for i in range(n):
		ans = max(ans, sum(a[i:]) + i - sum(a[:i]))
	print(ans)
main()


a = int(input())
b = input().split()
zerocount = 0
zero = []
onecount = 0
one = []
maxi = 0
for i in range(a):
	if b[i] == '0':
		zerocount += 1
	zero.append(zerocount)
for i in range(a - 1, -1, -1):
	if b[i] == '1':
		onecount += 1
	one.append(onecount)
one.reverse()
if maxi < one[0]:
	maxi = one[0]
for i in range(a - 1):
	if maxi < zero[i] + one[i + 1]:
		maxi = zero[i] + one[i + 1]
if maxi < zero[a - 1]:
	maxi = zero[a - 1]
print(maxi)


n = int(input())
A = input().split()
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
		if max_now > maxm:
			maxm = max_now
	print(maxm)


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(max(ones, zeroes, zero_ones))


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)


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)
main()


n = int(input())
l = input().split()
(z, o) = (1, l.count('1'))
maxi = max(o, n - o)
for i in l:
	if i == '1':
		o -= 1
		if o + z > maxi:
			maxi = o + z
	else:
		z += 1
print(maxi)


n = int(input())
s = list(map(int, input().split()))
f0 = [0] * (n + 1)
for i in range(n):
	if s[i] == 0:
		f0[i] = f0[i - 1] + 1
	else:
		f0[i] = f0[i - 1]
f1 = [0] * (n + 1)
for i in range(n - 1, -1, -1):
	if s[i] == 1:
		f1[i] = f1[i + 1] + 1
	else:
		f1[i] = f1[i + 1]
ans = 0
for i in range(n):
	if ans < f0[i] + f1[i]:
		ans = f0[i] + f1[i]
ans = max(f0[n - 1], f1[0], ans)
print(ans)


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)


def solve(s):
	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))
main()


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
	ct.append(c0 + c1)
print(max(ct))


n = input()
s = list(map(int, input().split()))
x = 0
for i in range(len(s)):
	y = 0
	for j in range(len(s)):
		if j <= i:
			if s[j] == 0:
				y += 1
		if j >= i:
			if s[j] == 1:
				y += 1
	if y > x:
		x = y
print(x)


n = int(input())

def it():
	A = list(map(int, input().split()))
	A.reverse()
	c1 = 0
	r0 = len(A) - sum(A)
	yield r0
	for a in A:
		if a == 1:
			c1 += 1
			yield (c1 + r0)
		else:
			r0 -= 1
print(max(it()))


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] + 1):
	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())
s = [int(c) for c in input().split()]
r = 0
for i in range(n + 1):
	res = s[:i].count(0)
	res += s[i:].count(1)
	r = max(res, r)
print(r)


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 + 1].count(0))
print(ans)


n = int(input())
string = ''.join(map(str, input().split()))
maxx = 0
for i in range(n + 1):
	maxx = max(maxx, string[:i].count('0') + string[i:].count('1'))
print(maxx)


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())
s = [int(i) for i in input().split()]
print(max([s[:i].count(0) + s[i:].count(1) for i in range(len(s) + 1)]))


import math
n = int(input())
a = list(map(int, input().split()))
z2 = a.count(0)
o2 = n - z2
z1 = 0
o1 = 0
res = z1 + o2
i = 0
while i < n:
	z2 -= 1 - a[i]
	z1 += 1 - a[i]
	o2 -= a[i]
	o1 += a[i]
	res = max(res, z1 + o2)
	i += 1
print(res)


n = int(input())
s = input()

def max_l(s):
	if len(s) <= 1:
		return len(s)
	lenE = 0
	ind = 0
	prev = -1
	for i in range(len(s)):
		if s[i] == '1':
			lenE += 1
		if s[i] == '1' and prev == '0':
			ind = i - 1
		prev = s[i]
	lenZ = 0
	for i in range(ind + 1):
		if s[i] == '0':
			lenZ += 1
	return max(lenE, lenZ + max_l(s[ind + 1:]))
a = max_l(s)
print(a)


import sys
input = sys.stdin.readline
n = int(input())
s = list(map(int, input().split()))
(zc, res) = ([0 for i in range(105)], 0)
for i in range(1, n + 1):
	zc[i] = zc[i - 1] + (1 if s[i - 1] == 0 else 0)
for i in range(1, n + 1):
	lzc = zc[i - 1]
	roc = n - i - (zc[n] - zc[i])
	res = max(res, lzc + 1 + roc)
print(res)


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(max(strike))


def procv(s, n):
	for i in range(n - 1):
		if s[i] == '1':
			for j in range(i, n - 1):
				if s[j + 1] == '0':
					s[j + 1] = 'x'
					break
	f = list(filter(lambda v: v != 'x', s))
	return len(f)
n = int(input())
s = list(input().split())
print(procv(s, n))


N = int(input())
A = list(map(int, input().split()))
ans = sum((1 for x in A if x == 0))
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)
print(ans)


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
n = int(input())
ans = n
a = list(map(int, input().split()))
p = [0 for i in range(n + 1)]
for i in range(n - 1, -1, -1):
	p[i] = p[i + 1] + a[i]
q = [0 for i in range(n + 1)]
for i in range(n):
	if a[i] == 0:
		q[i] = q[i - 1] + 1
	else:
		q[i] = q[i - 1]
ans = 0
for i in range(n):
	if a[i] == 0:
		ans = max(ans, q[i] + p[i + 1])
	else:
		ans = max(ans, q[i - 1] + p[i])
print(ans)


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())
s = [0] + list(map(int, input().split()))
(a, b) = ([0] * (n + 1), [0] * (n + 2))
for i in range(n):
	a[i + 1] = a[i] if s[i + 1] else a[i] + 1
	b[n - i] = b[n - i + 1] + 1 if s[n - i] else b[n - i + 1]
ans = 0
for i in range(n + 1):
	ans = max(ans, a[i] + b[i + 1])
print(ans)


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 = [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)


n = int(input())
nums = list(map(int, input().split()))
result = -1
for l in range(n):
	r = l + 1
	tempresult = nums[:l + 1].count(0) + nums[r:].count(1)
	result = max(result, tempresult)
result = max(result, nums.count(1))
result = max(result, nums.count(0))
print(result)


n = int(input())
t = list(map(int, input().split()))
zeros = 0
ones = sum(t)
m = zeros + ones
for b in range(n):
	if t[b] == 0:
		zeros += 1
	else:
		ones -= 1
	if m < zeros + ones:
		m = zeros + ones
print(m)


a = int(input())
b = [int(i) for i in input().split()]
c = b.copy()
ans = 0
for i in range(a):
	if b[i] == 1:
		ans += b[i:].count(1)
		break
	ans += 1
ans1 = 0
for i in range(a):
	if b[i] == 1:
		ans1 = max(ans1, b[:i].count(0) + b[i:a].count(1))
print(max(ans, ans1, b.count(0), b.count(1)))


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


def list_input():
	return list(map(int, input().split()))

def map_input():
	return map(int, input().split())

def map_string():
	return input().split()
n = int(input())
a = list_input()
cur = 0
ans = 0
for i in range(n):
	if a[i] == 0:
		cur += 1
		ans = max(ans, cur + a[i + 1:].count(1))
print(max(ans, a.count(1)))


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)


a = int(input())
b = list(map(int, input().split()))
c = []
d = []
count = 0
for i in b:
	if i == 0:
		count += 1
	c.append(count)
count = 0
for i in b[::-1]:
	if i == 1:
		count += 1
	d.append(count)
d = d[::-1]
x = []
for i in range(a):
	x.append(c[i] + d[i])
print(max(x))


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())
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())
a = list(map(int, input().split()))
max_l = 0
(z, o) = (0, 0)
for i in range(n):
	o = 0
	if a[i] == 0:
		z += 1
	for j in range(i, n):
		if a[j] == 1:
			o += 1
	max_l = max(max_l, z + o)
print(max_l)


def test(i):
	answer = s[:i].count(0) + s[i:].count(1)
	return answer
n = int(input())
s = list(map(int, input().split()))
answer = []
for i in range(n + 1):
	answer.append(test(i))
print(max(answer))


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())
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)


n = int(input())
a = ''
count = 0
for j in input().split():
	count = count + 1
	if a != '' and j == '0':
		a = a[:-1]
		count = count - 1
	elif j == '1':
		a = a + j
print(count)


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)


(n, t) = (int(input()), input()[::2])
print(max((t[:i].count('0') + t[i:].count('1') for i in range(n + 1))))


from sys import stdout
from random import randint
from math import *
import re
n = int(input())
s = input().replace(' ', '')
ones = s.count('1')
zeros = 0
mx = 0
for c in s:
	zeros += c == '0'
	if zeros + ones > mx:
		mx = zeros + ones
	ones -= c == '1'
print(mx)


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 = [int(x) for x in input().split()]
max_fails = []
fails = 0
for i in range(n):
	if s[i] == 0:
		fails += 1
	max_fails.append(fails)
max_successes = []
successes = 0
for i in range(n - 1, -1, -1):
	if s[i] == 1:
		successes += 1
	max_successes.append(successes)
max_successes = reversed(max_successes)
print(max([x + y for (x, y) in zip(max_fails, max_successes)]))


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())
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)


def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] >= arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	maximum = 0
	for i in range(n):
		maximum = max(maximum, lis[i])
	return maximum
n = int(input())
a = list(map(int, input().split()))
print(lis(a))


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


n = int(input())
l = list(map(int, input().split()))
max_games = [1 for i in range(n)]
if n > 1:
	if l[1] == 0:
		if l[0] == 1:
			max_games[1] = 1
		else:
			max_games[1] = 2
	else:
		max_games[1] = 2
t_max = max(max_games)
if n > 2:
	for i in range(2, n):
		if l[i] == 0:
			j = i - 1
			while j >= 0:
				if l[j] == 0:
					max_games[i] = max_games[j] + 1
					t_max = max([t_max, max_games[i]])
					break
				j -= 1
		else:
			max_games[i] = t_max + 1
			t_max += 1
print(t_max)


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))


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)


def rezume(lst):
	(ones, zeros) = (0, 0)
	(l, r) = ([0] * len(lst), [0] * len(lst))
	result = 0
	for i in range(len(lst)):
		l[i] = ones
		if lst[i]:
			ones += 1
		r[len(lst) - i - 1] = zeros
		if not lst[len(lst) - i - 1]:
			zeros += 1
	for i in range(len(lst)):
		result = max(result, len(lst) - l[i] - r[i])
	return result
n = int(input())
c = [int(j) for j in input().split()]
print(rezume(c))


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


from __future__ import division, print_function
import sys
import os
from io import BytesIO, IOBase
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt, log2
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
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):
	(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))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def solve(case):
	n = int(inp())
	a = lis()
	prev = a[0]
	arr = []
	cnt = 0
	for i in range(n):
		if a[i] == prev:
			cnt += 1
		else:
			arr.append((cnt, prev))
			prev = a[i]
			cnt = 1
	if cnt != 0:
		arr.append((cnt, prev))
	right = [0] * (len(arr) + 1)
	ans = 0
	for i in range(len(arr) - 1, -1, -1):
		if arr[i][1] == 0:
			right[i] = right[i + 1] + arr[i][0]
		else:
			right[i] = right[i + 1]
	help = 0
	for i in range(len(arr)):
		if arr[i][1] == 1:
			ans = max(ans, n - right[i] - help)
			help += arr[i][0]
	ans = max(ans, max(a.count(1), a.count(0)))
	print(ans)
testcase(1)


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)


import sys
import math
import heapq
import bisect
from collections import Counter
from collections import defaultdict
from io import BytesIO, IOBase

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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
		self.BUFSIZE = 8192

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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 = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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:
			self.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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split(' ')))

def get_int_grid(n):
	return [get_ints() for _ in range(n)]

def yes_no(b):
	if b:
		return 'YES'
	else:
		return 'NO'

def solve():
	n = get_int()
	s = get_ints()
	p = [0]
	for x in s:
		p.append(p[-1] + x)
	m = s.count(0)
	for i in range(n):
		if s[i] == 1:
			x = i - (p[i + 1] - 1)
			y = p[-1] - p[i + 1]
			m = max(m, 1 + x + y)
	return m
print(solve())


import sys
n = int(input())
a = list(map(int, input().split()))
dp = [[-10 ** 9, -10 ** 9] for _ in range(n + 1)]
dp[0][0] = 0
for (i, x) in enumerate(a):
	if x == 0:
		dp[i + 1][0] = dp[i][0] + 1
		dp[i + 1][1] = dp[i][1]
	else:
		dp[i + 1][0] = dp[i][0]
		dp[i + 1][1] = max(dp[i][0], dp[i][1]) + 1
print(max(dp[-1]))


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)