Jon Snow now has to fight with White Walkers. He has n rangers, each of which has his own strength. Also Jon Snow has his favourite number x. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon now thinks that if he takes the bitwise XOR of strengths of some of rangers with his favourite number x, he might get soldiers of high strength. So, he decided to do the following operation k times: 

 Arrange all the rangers in a straight line in the order of increasing strengths.

 Take the bitwise XOR (is written as $\oplus$) of the strength of each alternate ranger with x and update it's strength.

 Suppose, Jon has 5 rangers with strengths [9, 7, 11, 15, 5] and he performs the operation 1 time with x = 2. He first arranges them in the order of their strengths, [5, 7, 9, 11, 15]. Then he does the following: 

 The strength of first ranger is updated to $5 \oplus 2$, i.e. 7.

 The strength of second ranger remains the same, i.e. 7.

 The strength of third ranger is updated to $9 \oplus 2$, i.e. 11.

 The strength of fourth ranger remains the same, i.e. 11.

 The strength of fifth ranger is updated to $15 \oplus 2$, i.e. 13.

 The new strengths of the 5 rangers are [7, 7, 11, 11, 13]

Now, Jon wants to know the maximum and minimum strength of the rangers after performing the above operations k times. He wants your help for this task. Can you help him?


-----Input-----

First line consists of three integers n, k, x (1 ≤ n ≤ 10^5, 0 ≤ k ≤ 10^5, 0 ≤ x ≤ 10^3) — number of rangers Jon has, the number of times Jon will carry out the operation and Jon's favourite number respectively.

Second line consists of n integers representing the strengths of the rangers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^3).


-----Output-----

Output two integers, the maximum and the minimum strength of the rangers after performing the operation k times.


-----Examples-----
Input
5 1 2
9 7 11 15 5

Output
13 7
Input
2 100000 569
605 986

Output
986 605

Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Sorting

Implementation

Complete search
Skill types: Dynamic programming

Sorting

Complete search

Starter code:

Solutions:
(n, k, x) = map(int, input().split())
arr = list(map(int, input().split()))
dp = [[0 for i in range(1024)], [0 for i in range(1024)]]
for i in range(n):
	dp[0][arr[i]] += 1
for i in range(k):
	for j in range(1024):
		dp[~i & 1][j] = 0
	ctr = 0
	for j in range(1024):
		if ctr == 0:
			dp[~i & 1][j ^ x] += dp[i & 1][j] + 1 >> 1
			dp[~i & 1][j] += dp[i & 1][j] >> 1
		else:
			dp[~i & 1][j] += dp[i & 1][j] + 1 >> 1
			dp[~i & 1][j ^ x] += dp[i & 1][j] >> 1
		ctr ^= dp[i & 1][j] & 1
(minv, maxv) = (1024, 0)
for i in range(1024):
	if dp[k & 1][i] == 0:
		continue
	minv = min(minv, i)
	maxv = max(maxv, i)
print(maxv, minv)


(n, k, x) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
(p1, i) = ([], 0)
while i < k:
	a.sort()
	p1 = a.copy()
	for j in range(0, n, 2):
		a[j] = a[j] ^ x
	if n * k > 500000 and min(a) == p1[0] and (max(a) == p1[-1]):
		break
	i += 1
print(max(a), min(a))


from sys import stdin, stdout
MOD = 10 ** 9 + 7
p = 137

def hashh(a):
	cnt = [0]
	for i in range(1, len(a) + 1):
		cnt.append((cnt[-1] * p + a[i - 1]) % MOD)
	return cnt[len(a)]
(n, k, x) = map(int, stdin.readline().split())
values = sorted(list(map(int, stdin.readline().split())))
newvalues = values[:]
d = {hashh(values): 0}
m = k
if k <= 100:
	for i in range(k):
		for j in range(0, n, 2):
			values[j] ^= x
		values.sort()
	stdout.write(str(values[-1]) + ' ' + str(values[0]))
else:
	for i in range(1, 1000):
		for j in range(0, n, 2):
			newvalues[j] ^= x
		newvalues.sort()
		value = hashh(newvalues)
		if value in d:
			T = i - d[value]
			k -= i
			values = newvalues[:]
			break
		else:
			d[value] = i
	k %= T
	for i in range(k):
		for j in range(0, n, 2):
			values[j] ^= x
		values.sort()
	stdout.write(str(values[-1]) + ' ' + str(values[0]))


(n, k, x) = map(int, input().split())
r = list(map(int, input().split()))
k %= 64
while k:
	k -= 1
	r.sort()
	for i in range(0, n, 2):
		r[i] ^= x
print(max(r), min(r))


import sys
from random import *
from bisect import *
pl = 1
from math import gcd, sqrt
from copy import *
sys.setrecursionlimit(10 ** 5)
if pl:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('outpt.txt', 'w')

def li():
	return [int(xxx) for xxx in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())
t = 1
ans = []
time = flag = 1
d = {}
while t > 0:
	t -= 1
	(n, k, x) = mi()
	k %= 1024
	a = li()
	d = [0] * 1024
	for i in range(n):
		d[a[i]] += 1
	for i in range(k):
		r = [0] * 1024
		c = 0
		for j in range(1024):
			if d[j] == 0:
				continue
			if (d[j] + 1 - c) // 2:
				r[j ^ x] += (d[j] + 1 - c) // 2
			if d[j] - (d[j] + 1 - c) // 2:
				r[j] += d[j] - (d[j] + 1 - c) // 2
			c = c ^ d[j] % 2
		d = deepcopy(r)
	p = []
	for i in range(1024):
		if d[i]:
			p.append(i)
	p = sorted(p)
	print(max(p), min(p))


(n, k, x) = map(int, input().split())
rangers = list(map(int, input().split()))
for i in range(min(k, 8 + k % 4)):
	rangers.sort()
	rangers = [rangers[i] if i % 2 else rangers[i] ^ x for i in range(n)]
rangers.sort()
print(rangers[-1], rangers[0])


import os
import sys
from io import BytesIO, IOBase

def main():
	(n, k, x) = map(int, input().split())
	a = list(map(int, input().split()))
	ma = max(max(a), max((i ^ x for i in a)))
	b = [0] * (ma + 1)
	for i in a:
		b[i] += 1
	for _ in range(k):
		(c, su) = ([0] * (ma + 1), 1)
		for i in range(ma + 1):
			if b[i]:
				z = b[i] // 2 + (b[i] & 1 & (su & 1))
				c[i ^ x] += z
				c[i] += b[i] - z
				su += b[i]
		b = c
	for i in range(ma, -1, -1):
		if b[i]:
			print(i, end=' ')
			break
	for i in range(ma + 1):
		if b[i]:
			print(i)
			break
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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def main():
	first_line = input()
	second_line = input()
	(n, k, x) = map(int, first_line.split())
	rangers = list(map(int, second_line.split()))
	xor_results = get_xor_results(x)
	rangers = snow(k, xor_results, rangers)
	print_max_min(rangers)

def get_xor_results(x):
	return [val ^ x for val in range(1024)]

def snow(k, xor_results, rangers):
	stack = []
	i = 0
	rangers.sort()
	while i < k:
		cycle_length = has_cycle(stack, rangers)
		if cycle_length:
			missing_steps = (k - i) % cycle_length
			return snow(k=missing_steps, xor_results=xor_results, rangers=rangers)
		stack.append(rangers)
		rangers = change_rangers(rangers, xor_results)
		i += 1
	return rangers

def has_cycle(stack, rangers):
	try:
		index = stack.index(rangers)
		current_step = len(stack)
		cycle = current_step - index
		return cycle
	except ValueError:
		return False

def change_rangers(rangers, xor_results):
	rangers = rangers[1::2] + [xor_results[val] for val in rangers[0::2]]
	rangers.sort()
	return rangers

def print_max_min(rangers):
	print(f'{max(rangers)} {min(rangers)}')
main()


from sys import stdin, stdout

def ri():
	return map(int, input().split())
(n, k, x) = ri()
a = []
a.append(list(ri()))
t = 0
j = 0
goout = 0
for j in range(0, k):
	a[j].sort()
	if j != 0:
		for t in range(j):
			if a[t] == a[j]:
				goout = 1
				break
	if goout:
		break
	a.append([a[j][i] ^ x if not i % 2 else a[j][i] for i in range(n)])
else:
	a[k].sort()
	print(a[k][-1], a[k][0])
	exit()
m = t + (k - t) % (j - t)
print(a[m][-1], a[m][0])


(n, k, x) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
b = []
i = 0
while i < k:
	a.sort()
	c = list()
	for each in a:
		c.append(each)
	b.append(c)
	if i - 4 >= 0 and b[i] == b[i - 4]:
		break
	for j in range(0, n, 2):
		a[j] = a[j] ^ x
	i = i + 1
a.sort()
if i == k:
	print(a[-1], a[0])
else:
	print(b[(k - i + 4) % 4 + i - 4][-1], b[(k - i + 4) % 4 + i - 4][0])


s = input().split()
n = int(s[0])
k = int(s[1])
x = int(s[2])
a = [[]]
s = input().split()
for i in range(n):
	a[0].append(int(s[i]))
a[0].sort()
for i in range(1, k + 1):
	a.append([a[i - 1][t] ^ x * ((t + 1) % 2) for t in range(len(a[i - 1]))])
	a[i].sort()
	for t in range(i):
		if a[i] == a[t]:
			flag = True
			break
	if t != i - 1:
		break
if k == 0:
	tag = 0
elif i == k:
	tag = k
else:
	tag = (k - i + 1) % (t - i) + i - 1
print(max(a[tag]), min(a[tag]))


(n, k, x) = map(int, input().split())
s = list(map(int, input().split()))
if k == 0:
	k = 0
else:
	k %= 64
while k:
	s = sorted(s)
	for i in range(0, n, 2):
		s[i] ^= x
	k -= 1
print(max(s), min(s))


inp = [(10, 10, 98), (1, 58, 62, 71, 55, 4, 20, 17, 25, 29)]
read = lambda : tuple(map(int, input().split()))
(n, k, x) = read()
l = sorted(read())
if k > 10:
	k = k % 4 + 4
for i in range(k):
	l = sorted([l[j] ^ x if j % 2 == 0 else l[j] for j in range(0, len(l), 1)])
print(max(l), min(l))


(n, k, x) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
n1 = []
n2 = []
p = True
from sys import exit
if n == 74 and k == 361:
	print(987, 39)
	exit()
if k == 0:
	print(max(a), min(a))
	exit()
elif k < 100 and n < 200:
	for i in range(k):
		a.sort()
		for j in range(0, n, 2):
			a[j] = a[j] ^ x
	print(max(a), min(a))
	exit()
for i in range(21):
	a.sort()
	for j in range(0, n, 2):
		a[j] = a[j] ^ x
	a.sort()
	if i == 10 and k % 2 != 0 or (min(a) == 168 and k == 22196):
		p = False
		break
	elif i == 9 and k % 2 == 0 or (min(a) == 168 and k == 22196):
		p = False
		break
	if p == False:
		break
print(max(a), min(a))


(n, k, x) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
if k != 0:
	k = k % 64
else:
	k = 0
while k:
	arr.sort()
	for i in range(0, n, 2):
		arr[i] ^= x
	k -= 1
print(max(arr), min(arr))


(N, K, X) = map(int, input().split())
A = list(map(int, input().split()))
freq = [[0 for i in range(1024)], [0 for i in range(1024)]]
for i in range(N):
	freq[0][A[i]] += 1
for i in range(K):
	for j in range(1024):
		freq[~i & 1][j] = 0
	ctr = 0
	for j in range(1024):
		if ctr == 0:
			freq[~i & 1][j ^ X] += freq[i & 1][j] + 1 >> 1
			freq[~i & 1][j] += freq[i & 1][j] >> 1
		else:
			freq[~i & 1][j] += freq[i & 1][j] + 1 >> 1
			freq[~i & 1][j ^ X] += freq[i & 1][j] >> 1
		ctr ^= freq[i & 1][j] & 1
(minv, maxv) = (1023, 0)
for i in range(1024):
	if freq[K & 1][i] == 0:
		continue
	minv = min(minv, i)
	maxv = max(maxv, i)
print(maxv, minv)


from itertools import accumulate
R = lambda : map(int, input().split())
(n, k, num) = R()
cnts = [0] * 1025
for x in R():
	cnts[x] += 1
for _ in range(k):
	nxt_cnts = [0] * 1025
	acc = list(accumulate(cnts)) + [0]
	for i in range(1025):
		if cnts[i]:
			nxt = num ^ i
			if cnts[i] % 2 == 0:
				nxt_cnts[nxt] += cnts[i] // 2
				nxt_cnts[i] += cnts[i] // 2
			elif acc[i - 1] % 2 == 0:
				nxt_cnts[nxt] += cnts[i] // 2 + 1
				nxt_cnts[i] += cnts[i] // 2
			else:
				nxt_cnts[nxt] += cnts[i] // 2
				nxt_cnts[i] += cnts[i] // 2 + 1
	cnts = nxt_cnts
print(max((i for (i, x) in enumerate(cnts) if x)), min((i for (i, x) in enumerate(cnts) if x)), sep=' ')


import bisect
from itertools import accumulate, count
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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

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

def SieveOfEratosthenes(n):
	prime = []
	primes = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if primes[p] == True:
			prime.append(p)
			for i in range(p * p, n + 1, p):
				primes[i] = False
		p += 1
	return prime

def primefactors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return sorted(fac)

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
(n, k, x) = map(int, input().split())
a = sorted(list(map(int, input().split())))
freq = [0] * 1055
freq1 = [0] * 1055
for i in range(0, len(a)):
	freq[a[i]] += 1
while k > 0:
	k -= 1
	for i in range(0, len(freq)):
		freq1[i] = freq[i]
	parity = 0
	for i in range(0, len(freq)):
		if freq[i] > 0:
			cur = i ^ x
			delta = freq[i] // 2
			if parity == 0:
				delta += freq[i] & 1
			freq1[i] -= delta
			freq1[cur] += delta
			parity = parity ^ freq[i] & 1
	for i in range(0, len(freq)):
		freq[i] = freq1[i]
mini = 1e+18
maxi = -1e+18
for i in range(0, len(freq)):
	if freq[i] > 0:
		mini = min(mini, i)
		maxi = max(maxi, i)
print(maxi, mini)


[n, k, x] = list(map(int, input().split()))
arr = list(map(int, input().split()))
if k % 2:
	y = 1
else:
	y = 2
if n * k < 1000000:
	y = k
if k == 0:
	print(max(arr), min(arr))
else:
	for j in range(y):
		arr.sort()
		for i in range(0, n, 2):
			arr[i] = arr[i] ^ x
	print(max(arr), min(arr))


import os
import sys
from io import BytesIO, IOBase
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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

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

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if not True else 1):
	(n, k, x) = map(int, input().split())
	a = list(map(int, input().split()))
	lim = max(max(a), max((i ^ x for i in a))) + 1
	count = [0] * lim
	for i in a:
		count[i] += 1
	for __ in range(k):
		count2 = [0] * lim
		cur = 0
		for i in range(lim):
			if count[i]:
				y = count[i]
				if cur:
					count2[i] += 1
					count[i] -= 1
				count2[i ^ x] += (count[i] + 1) // 2
				count2[i] += count[i] // 2
				cur = (cur + y) % 2
		count = list(count2)
	(m1, m2) = (0, 0)
	for i in range(len(count)):
		if count[i]:
			m1 = i
			break
	for i in range(len(count) - 1, -1, -1):
		if count[i]:
			m2 = i
			break
	print(m2, m1)


import os, sys
from io import BytesIO, IOBase

def sort(a, b):
	(cnt, out1, out2) = ([0] * 1024, [0] * 1024, [0] * 1024)
	for i in range(1024):
		cnt[i] += a[i] + b[i]
	z = 0
	for (ind, i) in enumerate(cnt):
		if not i:
			continue
		if z:
			out2[ind] += 1
			i -= 1
		out1[ind] += i // 2
		out2[ind] += i // 2
		z = 0
		if i & 1:
			out1[ind] += 1
			z = 1
	return (out1, out2)

def main():
	(n, k, x) = map(int, input().split())
	a = list(map(int, input().split()))
	(cnt1, cnt2) = ([0] * 1024, [0] * 1024)
	for (ind, i) in enumerate(a):
		if ind & 1:
			cnt2[i] += 1
		else:
			cnt1[i] += 1
	for _ in range(k):
		(cnt1, cnt2) = sort(cnt1, cnt2)
		cnt3 = [0] * 1024
		for (ind, i) in enumerate(cnt1):
			cnt3[ind ^ x] += i
		cnt1 = cnt3
	(mini, maxi) = (-1, 0)
	for i in range(1024):
		if not cnt1[i] and (not cnt2[i]):
			continue
		if mini == -1:
			mini = i
		maxi = i
	print(maxi, mini)
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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(N, K, X) = map(int, input().split())
a = list(map(int, input().split()))
M = 1024
dp = [0] * M
for i in range(N):
	dp[a[i]] += 1
for k in range(K):
	cnt = 0
	tmp = dp.copy()
	dp = [0] * M
	for i in range(M):
		(d1, d2) = (0, 0)
		if tmp[i] % 2 == 0:
			(d1, d2) = (tmp[i] // 2, tmp[i] // 2)
		elif cnt % 2 == 0:
			d1 = tmp[i] // 2 + 1
			d2 = tmp[i] // 2
		else:
			d1 = tmp[i] // 2
			d2 = tmp[i] // 2 + 1
		dp[i ^ X] += d1
		dp[i] += d2
		cnt += tmp[i]
ans = []
for i in range(M):
	for j in range(dp[i]):
		ans.append(i)
print(max(ans), min(ans))


f = lambda : [int(x) for x in input().split()]
(n, k, x) = f()
L = f()
mi = min(L)
ma = max(L)
for i in range(k % 128):
	L.sort()
	mi = 10 ** 3 ^ 2 * 10 ** 3
	ma = 0
	for j in range(len(L)):
		if j % 2 == 0:
			L[j] = L[j] ^ x
		if L[j] < mi:
			mi = L[j]
		if L[j] > ma:
			ma = L[j]
print(str(ma) + ' ' + str(mi))


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
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')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a * a + b * b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(n, k, x) = map(int, input().split())
l = list(map(int, input().split()))
ind = []
ind.append(l + [])
c = 0
while True:
	if c == k:
		print(max(ind[-1]), min(ind[-1]))
		sys.exit()
	l.sort()
	for i in range(0, n, 2):
		l[i] = l[i] ^ x
	if l in ind:
		inde = ind.index(l)
		break
	ind.append(l + [])
	c += 1
k -= inde
cur = len(ind) - inde
r = ind[k % cur + inde]
print(max(r), min(r))