This problem differs from one which was on the online contest.

The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n.

The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements.

You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.

Input

The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.

Output

In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.

Examples

Input

7
2 3 1 6 5 4 6
4
1 3 5 6


Output

3
3 5 6 


Input

5
1 2 0 2 1
3
1 0 1


Output

2
0 1

Source: codeforces | Difficulty: VERY_HARD
Tags: Dynamic programming


Solutions:
n = int(input())
a = [0] + list(map(int, input().split()))
m = int(input())
b = [0] + list(map(int, input().split()))
d = [[0 for j in range(m + 1)] for i in range(n + 1)]
prev = [0] * (n + 1)
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if a[i] == b[j]:
			d[i][j] = 1
			for k in range(1, i):
				if a[k] < a[i] and d[i][j] < d[k][j] + 1:
					d[i][j] = d[k][j] + 1
					prev[i] = k
		else:
			d[i][j] = d[i][j - 1]
pos = 0
for i in range(1, n + 1):
	if d[pos][m] < d[i][m]:
		pos = i
ans = []
while pos != 0:
	ans.append(a[pos])
	pos = prev[pos]
print(len(ans))
print(*ans[::-1])


n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
dp = [[0] * (m + 1) for i in range(n + 1)]
p = [[0] * (m + 1) for i in range(n + 1)]
v1 = 0
mx = 0
for i in range(1, n + 1):
	bst = 0
	v = 0
	for j in range(1, m + 1):
		dp[i][j] = dp[i - 1][j]
		p[i][j] = p[i - 1][j]
		if a[i - 1] == b[j - 1] and bst + 1 > dp[i][j]:
			dp[i][j] = bst + 1
			p[i][j] = v
		if a[i - 1] > b[j - 1] and dp[i - 1][j] > bst:
			v = b[j - 1]
			bst = dp[i - 1][j]
		if dp[i][j] > mx:
			mx = dp[i][j]
			v1 = b[j - 1]
print(mx)
i = n - 1
j = m - 1
arr = [0] * mx
for k in range(mx):
	while a[i] != v1:
		i = i - 1
	while b[j] != v1:
		j = j - 1
	arr[mx - 1 - k] = a[i]
	v1 = p[i + 1][j + 1]
for k in range(mx):
	print(arr[k], end=' ')


def lcis(a, b):
	(n, m) = (len(a), len(b))
	dp = [0] * m
	(best_seq, max_len) = ([], 0)
	for i in range(n):
		current = 0
		seq = []
		for j in range(m):
			if a[i] == b[j]:
				f = seq + [a[i]]
				if len(f) > max_len:
					max_len = len(f)
					best_seq = f
				dp[j] = max(current + 1, dp[j])
			if a[i] > b[j]:
				if dp[j] > current:
					current = dp[j]
					seq.append(b[j])
	return (max_len, best_seq)
n = int(input())
a = input().split(' ')
a = [int(x) for x in a]
m = int(input())
b = input().split(' ')
b = [int(x) for x in b]
(x, y) = lcis(a, b)
print(x)
y = [str(x) for x in y]
print(' '.join(y))


__author__ = 'Darren'

def solve():
	n = int(input())
	first = list(map(int, input().split()))
	m = int(input())
	second = list(map(int, input().split()))
	if m > n:
		(n, m) = (m, n)
		(first, second) = (second, first)
	(dp, prev) = ([0] * m, [-1] * m)
	for i in range(n):
		(max_len, last_index) = (0, -1)
		for j in range(m):
			if first[i] == second[j] and dp[j] < max_len + 1:
				dp[j] = max_len + 1
				prev[j] = last_index
			elif first[i] > second[j] and max_len < dp[j]:
				max_len = dp[j]
				last_index = j
	(max_value, index_max) = (0, -1)
	for (index, value) in enumerate(dp):
		if value > max_value:
			max_value = value
			index_max = index
	print(max_value)
	if max_value > 0:
		seq = []
		index = index_max
		while index >= 0:
			seq.append(str(second[index]))
			index = prev[index]
		seq.reverse()
		print(' '.join(seq))
solve()


def get_lcis_length(X, Y):
	len_Y = len(Y)
	curr_lcis_len = [0] * len_Y
	for u in X:
		lcis_prefix_max = []
		curr_max = 0
		for (i, v) in enumerate(Y):
			if v < u:
				curr_max = max(curr_max, curr_lcis_len[i])
			lcis_prefix_max.append(curr_max)
		for (i, v) in enumerate(Y):
			if u == v:
				curr_lcis_len[i] = max(curr_lcis_len[i], (0 if i == 0 else lcis_prefix_max[i - 1]) + 1)
	max_lcis_length = curr_lcis_len[0]
	for (i, u) in enumerate(curr_lcis_len):
		max_lcis_length = max(max_lcis_length, u)
	if max_lcis_length == 0:
		print(0)
		return max_lcis_length
	curr_lcis_len = [0] * len_Y
	bt = [-1] * len_Y
	for u in X:
		lcis_prefix_max = []
		bt_id = []
		curr_max = 0
		curr_id_max = -1
		for (i, v) in enumerate(Y):
			if v < u:
				if curr_max < curr_lcis_len[i]:
					curr_max = curr_lcis_len[i]
					curr_id_max = i
			lcis_prefix_max.append(curr_max)
			bt_id.append(curr_id_max)
		for (i, v) in enumerate(Y):
			if u == v:
				if curr_lcis_len[i] < (0 if i == 0 else lcis_prefix_max[i - 1]) + 1:
					curr_lcis_len[i] = (0 if i == 0 else lcis_prefix_max[i - 1]) + 1
					bt[i] = -1 if i == 0 else bt_id[i - 1]
					if curr_lcis_len[i] == max_lcis_length:
						lcis_list = []
						now = i
						while now != -1:
							lcis_list.append(Y[now])
							now = bt[now]
						lcis_list.reverse()
						print(max_lcis_length)
						for (i, u) in enumerate(lcis_list):
							print(u, end='\n' if i == max_lcis_length - 1 else ' ')
						return (max_lcis_length, lcis_list)
	return max_lcis_length
x = []
try:
	while True:
		y = input()
		x.append(y)
except EOFError:
	pass
x = ' '.join(x)
x = x.split()
x = [int(i) for i in x]
X = []
Y = []
num_x = x[0]
for i in range(1, num_x + 1):
	X.append(x[i])
for i in range(x[num_x + 1]):
	Y.append(x[num_x + 2 + i])
get_lcis_length(X, Y)


n1 = int(input())
arr1 = list(map(int, input().split()))
n2 = int(input())
arr2 = list(map(int, input().split()))
if n1 > n2:
	temp = n1
	n1 = n2
	n2 = temp
	temp1 = arr1.copy()
	arr1 = arr2.copy()
	arr2 = temp1
table = []
count = [0] * n2
table1 = []
for i in range(n2):
	table.append([])
for i in range(n1):
	table1.append([])
for i in range(n1):
	ct = 0
	for j in range(n2):
		if arr1[i] == arr2[j]:
			if ct + 1 > count[j]:
				count[j] = ct + 1
				table[j] = table1[i].copy()
				table[j].append(arr1[i])
		if arr1[i] > arr2[j]:
			if count[j] > ct:
				ct = count[j]
				table1[i] = table[j].copy()
mx = 0
res = []
for i in range(n2):
	if count[i] > mx:
		mx = count[i]
		res = table[i]
print(mx)
if mx != 0:
	for i in res:
		print(i, end=' ')
else:
	print()


n = int(input())
a = [0] + list(map(int, input().split()))
m = int(input())
b = [0] + list(map(int, input().split()))
n += 1
m += 1
dp = [[0] * max(m, n) for i in range(max(m, n))]
pr = [0] * (max(n, m) + 1)
ans = []
for i in range(1, n):
	for j in range(1, m):
		if a[i] == b[j]:
			dp[i][j] = 1
			for k in range(1, i):
				if a[k] < a[i]:
					if dp[i][j] < dp[k][j] + 1:
						pr[i] = k
						dp[i][j] = dp[k][j] + 1
		else:
			dp[i][j] = dp[i][j - 1]
mi = 1
mj = 1
for i in range(1, n):
	if dp[i][m - 1] > dp[mi][m - 1]:
		mi = i
print(dp[mi][m - 1])
k = dp[mi][m - 1]
s = 0
while mi != 0 and k > s:
	ans.append(a[mi])
	s += 1
	mi = pr[mi]
if ans:
	ans.reverse()
	print(*ans)


N = int(input())
a = list(map(int, input().split()))
M = int(input())
b = list(map(int, input().split()))
dp = [[0 for j in range(M + 1)] for i in range(N + 1)]
parent = [[None for j in range(M + 1)] for i in range(N + 1)]
Max = 0
for i in range(1, N + 1):
	best = 0
	for j in range(1, M + 1):
		dp[i][j] = dp[i - 1][j]
		parent[i][j] = (i - 1, j, False)
		if a[i - 1] == b[j - 1] and dp[i - 1][best] + 1 > dp[i][j]:
			dp[i][j] = dp[i - 1][best] + 1
			parent[i][j] = (i - 1, best, True)
		if dp[i - 1][j] >= dp[i - 1][best] and a[i - 1] > b[j - 1]:
			best = j
for j in range(1, M + 1):
	if Max < dp[N][j]:
		Max = dp[N][j]
		im = N
		jm = j
ans = [None for i in range(Max)]
flag = Max - 1
while flag >= 0:
	if parent[im][jm][2]:
		ans[flag] = b[jm - 1]
		flag -= 1
	(im, jm, indikator) = parent[im][jm]
print(len(ans))
print(*ans)


n = int(input())
ar = list(map(int, input().split()))
m = int(input())
br = list(map(int, input().split()))
t = [0] * m
A = [[] for i in range(m)]
for i in range(n):
	c = 0
	p = -1
	for j in range(m):
		if ar[i] == br[j] and c + 1 > t[j]:
			t[j] = c + 1
			A[j] = (A[p] if p != -1 else []) + [ar[i]]
		if ar[i] > br[j] and c < t[j]:
			c = t[j]
			p = j
mx = max(t)
print(mx)
if mx > 0:
	ar = []
	for el in A:
		if len(el) > len(ar):
			ar = el
	print(*ar)


def lcis(a, b):
	g = set(b)
	f = [[float('Inf')] for j in range(len(b))]
	prev = {}
	for i in range(len(a)):
		if a[i] in g:
			k = 0
			for j in range(len(b)):
				if b[j] == a[i]:
					if j == 0:
						if len(f[j]) > 1:
							f[j][1] = min(f[j][1], a[i])
						else:
							f[j].append(a[i])
						k = 1
					else:
						(left, right) = (1, len(f[j - 1]) - 1)
						h = 0
						while left <= right:
							mid = int((left + right) / 2)
							if f[j - 1][mid] < a[i]:
								h = mid
								left = mid + 1
							else:
								right = mid - 1
						if h + 1 < len(f[j]):
							if a[i] < f[j][h + 1]:
								f[j][h + 1] = a[i]
								prev[a[i]] = f[j - 1][h]
						else:
							f[j].append(a[i])
							prev[a[i]] = f[j - 1][h]
						k = h + 1
				elif j > 0 and k > 0:
					if k < len(f[j]):
						f[j][k] = min(f[j][k], f[j - 1][k])
					else:
						f[j].append(f[j - 1][k])
	(out, k) = ([], f[-1][-1])
	while True:
		if k != float('Inf'):
			out.append(k)
		if k in prev:
			k = prev[k]
		else:
			break
	return (len(out), out[::-1])
n = int(input())
a = input().split(' ')
a = [int(x) for x in a]
m = int(input())
b = input().split(' ')
b = [int(x) for x in b]
(x, y) = lcis(a, b)
print(x)
y = [str(x) for x in y]
print(' '.join(y))


def sub_seq(initial, lis1, lis2, sus):
	total = []
	inc_initial = initial
	c_lis2 = lis2
	if sus == []:
		subs = [initial]
	else:
		subs = sus
	recur_sub = [initial]
	ind = 0
	for i in lis1:
		if int(i) > int(inc_initial):
			if i in c_lis2:
				subs.append(i)
				inc_initial = i
				c_lis2 = c_lis2[c_lis2.index(i) + 1:]
			ind += 1
		elif int(i) > int(initial):
			if i in lis2:
				temp_subs = subs.copy()
				for h in range(len(subs) - 1, -1, -1):
					if int(subs[h]) < int(i):
						temp_subs.append(int(i))
						break
					else:
						temp_subs.pop()
				recur_sub = sub_seq(i, lis1[ind + 1:], lis2[lis2.index(i):], temp_subs)
				total.append(recur_sub)
			ind += 1
		else:
			ind += 1
	min = 0
	su = []
	for z in total:
		if len(z) > min:
			min = len(z)
			su = z
	if min > len(subs):
		return su
	else:
		return subs
is1 = []
ns1 = int(input())
s1 = input()
s1 = s1.split()
ns2 = int(input())
s2 = input()
s2 = s2.split()
if ns1 > ns2:
	indexi = 0
	for i in s2:
		if i in s1:
			if indexi + 1 < len(s2):
				is1.append(sub_seq(i, s2[indexi + 1:], s1[s1.index(i):], []))
			else:
				is1.append([i])
		indexi += 1
else:
	indexi = 0
	for i in s1:
		if i in s2:
			if indexi + 1 < len(s1):
				is1.append(sub_seq(i, s1[indexi + 1:], s2[s2.index(i):], []))
			else:
				is1.append([i])
		indexi += 1
min = 0
su = []
for i in is1:
	if len(i) > min:
		min = len(i)
		su = i
print(min)
print(*su)


n = int(input())
s1 = [0] + [int(x) for x in input().split()]
m = int(input())
s2 = [0] + [int(x) for x in input().split()]
table = [0] * (m + 1)
pos = [0] * (m + 1)
for i in range(1, n + 1):
	curr = 0
	for j in range(1, m + 1):
		if s1[i] == s2[j]:
			table[j] = table[curr] + 1
			pos[j] = curr
		if s1[i] > s2[j] and table[curr] < table[j]:
			curr = j
p = 0
for i in range(1, m + 1):
	if table[i] > table[p]:
		p = i
print(table[p])

def out(p):
	if not p:
		return
	out(pos[p])
	print(s2[p], end=' ')
out(p)
print(' ')


n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
d = [[0] * m for i in range(n)]
p = [[-1] * m for i in range(n)]
for i in range(n):
	j1 = -1
	for j in range(m):
		if i == 0:
			if a[i] != b[j]:
				d[i][j] = 0
			else:
				d[i][j] = 1
			continue
		if a[i] != b[j]:
			d[i][j] = d[i - 1][j]
			p[i][j] = m
		elif j1 == -1:
			d[i][j] = 1
		else:
			d[i][j] = d[i - 1][j1] + 1
			p[i][j] = j1
		if a[i] > b[j]:
			if (j1 == -1 or d[i - 1][j] > d[i - 1][j1]) and d[i - 1][j] >= 1:
				j1 = j
x = n - 1
y = 0
for i in range(m):
	if d[n - 1][i] > d[n - 1][y]:
		y = i
if d[x][y] == 0:
	print(0)
	exit(0)
print(d[n - 1][y])
ans = []
while y != -1:
	cp = p[x][y]
	if cp == m:
		x -= 1
	else:
		ans.append(b[y])
		x -= 1
		y = cp
for i in reversed(ans):
	print(i, end=' ')