Vasily the bear has got a sequence of positive integers a_1, a_2, ..., a_{n}. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. 

The beauty of the written out numbers b_1, b_2, ..., b_{k} is such maximum non-negative integer v, that number b_1 and b_2 and ... and b_{k} is divisible by number 2^{v} without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b_1 and b_2 and ... and b_{k} is divisible by 2^{v} without a remainder), the beauty of the written out numbers equals -1. 

Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible.

Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and".


-----Input-----

The first line contains integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_1 < a_2 < ... < a_{n} ≤ 10^9).


-----Output-----

In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b_1, b_2, ..., b_{k} — the numbers to write out. You are allowed to print numbers b_1, b_2, ..., b_{k} in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.


-----Examples-----
Input
5
1 2 3 4 5

Output
2
4 5

Input
3
1 2 4

Output
1
4

Difficulty: MEDIUM_HARD
Tags: Number theory

Complete search

Implementation

Greedy algorithms
Skill types: Complete search

Greedy algorithms

Starter code:

Solutions:
x = int(input())
s = list(map(int, input().split()))
ans = []
for u in range(0, 30):
	cur = 1 << u
	v = (1 << u + 1) - 1
	tem = []
	for n in s:
		if n & cur:
			tem.append(n)
	for n in tem:
		v &= n
	if v % (1 << u) == 0:
		ans = tem
print(len(ans))
print(*ans)


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))
for _ in range(1):
	n = nmbr()
	a = lst()
	bits = [[0 for _ in range(34)] for _ in range(n)]
	for i in range(n):
		for j in range(34):
			bits[i][33 - j] = a[i] >> j & 1
	bit = -1
	x = (1 << 32) - 1
	for bp in range(34):
		nd = x
		for i in range(n):
			if bits[i][33 - bp]:
				nd &= a[i]
		if nd % (1 << bp) == 0:
			bit = bp
	if bit == -1:
		print(-1)
	else:
		ans = []
		for i in range(n):
			if bits[i][33 - bit]:
				ans += [a[i]]
		print(len(ans))
		print(*ans)


import functools
n = int(input())
nums = list(map(int, input().split()))
bits = ['{0:b}'.format(num) for num in nums]

def possible(v):
	possible_vals = [nums[x] for x in range(n) if len(bits[x]) > v and bits[x][len(bits[x]) - v - 1] == '1']
	if len(possible_vals) == 0:
		return (False, [])
	res = functools.reduce(lambda x, y: x & y, possible_vals, pow(2, v + 1) - 1)
	return (bool(res & (1 << v + 1) - 1 == 1 << v), possible_vals)
for x in range(30, -1, -1):
	(p, vals) = possible(x)
	if p:
		print(len(vals))
		print(' '.join(list(map(str, vals))))
		break


n = int(input())
arr = list(map(int, input().split()))
max_arr = max(arr)
v = 1 << 31
while v > max_arr:
	v >>= 1
while True:
	d = -1
	for i in range(n):
		if arr[i] & v:
			d &= arr[i]
	if d % v == 0:
		break
	v >>= 1
b = [i for i in arr if i & v]
print(len(b))
print(' '.join(map(str, b)))


n = int(input())
a = list(map(int, input().split()))
mxa = max(a)
v = 1 << 30
while v > mxa:
	v >>= 1
while True:
	d = -1
	for i in range(n):
		if a[i] & v:
			d &= a[i]
	if d % v == 0:
		break
	v >>= 1
b = [i for i in a if i & v]
print(len(b))
print(' '.join(map(str, b)))


n = int(input())
t = list(map(int, input().split()))
p = [bin(i) for i in t]
p = ['0' * (32 - len(i)) + i[2:] for i in p]
p = [''.join(i) for i in zip(*p)]
x = 0
for i in range(30):
	x = p[i]
	if '1' in x and (not any((all((x[k] == y[k] for k in range(n) if x[k] == '1')) for y in p[i + 1:]))):
		break
t = [str(t[k]) for k in range(n) if x[k] == '1']
print(len(t))
print(' '.join(t))