A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon.

A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence a_1, a_2, ..., a_{n}.

Little Tommy is among them. He would like to choose an interval [l, r] (1 ≤ l ≤ r ≤ n), then reverse a_{l}, a_{l} + 1, ..., a_{r} so that the length of the longest non-decreasing subsequence of the new sequence is maximum.

A non-decreasing subsequence is a sequence of indices p_1, p_2, ..., p_{k}, such that p_1 < p_2 < ... < p_{k} and a_{p}_1 ≤ a_{p}_2 ≤ ... ≤ a_{p}_{k}. The length of the subsequence is k.


-----Input-----

The first line contains an integer n (1 ≤ n ≤ 2000), denoting the length of the original sequence.

The second line contains n space-separated integers, describing the original sequence a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 2, i = 1, 2, ..., n).


-----Output-----

Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence.


-----Examples-----
Input
4
1 2 1 2

Output
4

Input
10
1 1 2 2 2 1 1 2 2 1

Output
9



-----Note-----

In the first example, after reversing [2, 3], the array will become [1, 1, 2, 2], where the length of the longest non-decreasing subsequence is 4.

In the second example, after reversing [3, 7], the array will become [1, 1, 1, 1, 2, 2, 2, 2, 2, 1], where the length of the longest non-decreasing subsequence is 9.

Difficulty: MEDIUM_HARD

Solutions:
n = int(input())
a = list(map(int, input().split()))
pref = [0] * (n + 1)
for i in range(n):
	pref[i + 1] = pref[i] + (1 if a[i] == 1 else 0)
suf = [0] * (n + 1)
for i in reversed(range(n)):
	suf[i] = suf[i + 1] + (1 if a[i] == 2 else 0)
dp = [0, 0, 0, 0]
for i in range(n):
	new_dp = [max(dp[i], dp[i - 1]) if i > 0 else dp[i] for i in range(4)]
	if a[i] == 1:
		new_dp[0] += 1
		new_dp[2] += 1
	else:
		new_dp[1] += 1
		new_dp[3] += 1
	dp = new_dp
print(max(max([pref[i] + suf[i] for i in range(n + 1)]), max(dp)))


n = int(input())
(a, ab, aba, abab) = (0, 0, 0, 0)
b = [int(x) for x in input().split()]
for x in b:
	if x == 1:
		a += 1
		aba += 1
	else:
		ab += 1
		abab += 1
	ab = max(a, ab)
	aba = max(aba, ab)
	abab = max(abab, aba)
print(abab)


a = [0] * 4
input()
for n in map(int, input().split()):
	if n == 1:
		a[0] += 1
		a[2] = max(a[1] + 1, a[2] + 1)
	else:
		a[1] = max(a[0] + 1, a[1] + 1)
		a[3] = max(a[2] + 1, a[3] + 1)
print(max(a))


import sys

def input():
	return sys.stdin.buffer.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
ans = max(a.count(1), a.count(2))
prefix = [0] * (n + 10)
suffix = [0] * (n + 10)
for i in range(n):
	if a[i] == 1:
		prefix[i + 1] = 1
a.reverse()
for i in range(n):
	if a[i] == 2:
		suffix[i + 1] = 1
a.reverse()
for i in range(n):
	prefix[i + 1] += prefix[i]
	suffix[i + 1] += suffix[i]
INF = 1 << 60
a.reverse()
ans = -INF
for l in range(n):
	max_case_1 = 0
	max_case_2 = 0
	for r in range(l, n):
		if a[r] == 1:
			max_case_1 += 1
		elif a[r] == 2:
			max_case_2 = max(max_case_2, max_case_1) + 1
		K = max(max_case_1, max_case_2)
		ans = max(ans, K + prefix[n - 1 - r] + suffix[l])
print(ans)


n = int(input())
A = list(map(int, input().split()))
one = [0]
two = [0]
for i in A:
	one.append(one[-1])
	two.append(two[-1])
	if i == 1:
		one[-1] += 1
	else:
		two[-1] += 1
rdp1 = [[1] * n for _ in range(n)]
rdp2 = [[1] * n for _ in range(n)]
for l in range(n):
	for r in range(l + 1, n):
		if A[r] == 2:
			rdp1[l][r] = rdp1[l][r - 1] + 1
		elif rdp1[l][r - 1] == one[r] - one[l]:
			rdp1[l][r] = rdp1[l][r - 1] + 1
		else:
			rdp1[l][r] = rdp1[l][r - 1]
		if A[r] == 1:
			rdp2[l][r] = rdp2[l][r - 1] + 1
		elif rdp2[l][r - 1] == two[r] - two[l]:
			rdp2[l][r] = rdp2[l][r - 1] + 1
		else:
			rdp2[l][r] = rdp2[l][r - 1]
dp = [0] * n
dp[0] = 1
for i in range(1, n):
	if A[i] == 2:
		dp[i] = dp[i - 1] + 1
	elif dp[i - 1] == one[i]:
		dp[i] = dp[i - 1] + 1
	else:
		dp[i] = dp[i - 1]
	dp[i] = max(dp[i], rdp2[0][i])
	for j in range(i):
		if rdp1[0][j] == one[j + 1]:
			dp[i] = max(dp[i], rdp1[0][j] + rdp2[j + 1][i])
		dp[i] = max(dp[i], rdp1[0][j] + two[i + 1] - two[j + 1])
print(dp[-1])


n = int(input())
a = list(map(int, input().split()))
d = [0 for _ in range(4)]
for val in a:
	if val == 1:
		d[0] += 1
		d[2] = max(d[2] + 1, d[1] + 1)
	else:
		d[1] = max(d[1] + 1, d[0] + 1)
		d[3] = max(d[3] + 1, d[2] + 1)
print(max(d))


R = lambda : map(int, input().split())
n = int(input())
dp = [0] * 5
for x in R():
	if x == 1:
		(dp[1], dp[3]) = (dp[1] + 1, max(dp[3], dp[2]) + 1)
	else:
		(dp[2], dp[4]) = (max(dp[1], dp[2]) + 1, max(dp[3], dp[4]) + 1)
print(max(dp))


DP = [0] * 4
input()
for x in map(int, input().split()):
	if x == 1:
		DP[0] += 1
		DP[2] += 1
	else:
		DP[1] += 1
		DP[3] += 1
	for j in range(1, 4):
		DP[j] = max(DP[j], DP[j - 1])
print(DP[3])


import sys
import math
from collections import defaultdict
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
pre = [0]
(cnt1, cnt2) = (0, 0)
(last1, last2) = (0, 0)
for i in range(n):
	if arr[i] == 1:
		cnt1 += 1
		x = max(last1 + 1, 0)
		pre.append(x)
		last1 = x
	else:
		x = max(last1 + 1, last2 + 1)
		pre.append(x)
		last2 = x
suf = [0 for _ in range(n + 1)]
(last1, last2) = (0, 0)
for i in range(n - 1, -1, -1):
	if arr[i] == 1:
		x = max(last1 + 1, last2 + 1)
		suf[i] = x
		last1 = x
	else:
		x = last2 + 1
		suf[i] = x
		last2 = x
ans = max(max(pre), max(suf))
for i in range(1, n + 1):
	(cnt1, cnt2) = (0, 0)
	(last1, last2) = (0, 0)
	for j in range(i, n + 1):
		if arr[j - 1] == 1:
			x = max(last1 + 1, last2 + 1)
			last1 = x
			ans = max(pre[i - 1] + x + suf[j], ans)
		else:
			x = last2 + 1
			last2 = x
			ans = max(pre[i - 1] + x + suf[j], ans)
print(ans)


def solve(lst):
	a = b = c = d = 0
	for i in range(len(lst)):
		if lst[i] == 1:
			a += 1
			c = max(b, c) + 1
		else:
			b = max(a, b) + 1
			d = max(c, d) + 1
	return max(a, b, c, d)
n = input()
lst = list(map(int, input().split()))
print(solve(lst))


n = int(input())
a = list(map(int, input().split()))
dp = [0] * 4
for j in range(n):
	dp = [dp[i] if i == 0 else max(dp[i], dp[i - 1]) for i in range(4)]
	dp = [dp[i] + (1 if i % 2 == a[j] - 1 else 0) for i in range(4)]
print(max(dp))