A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget. Given price lists for keyboards and USB drives and a budget, find the cost to buy them.  If it is not possible to buy both items, return $-1$.

Example 

$\boldsymbol{b=60}$ 

$keyboards=[40,50,60]$ 

$drives=[5,8,12]$  

The person can buy a $40\ \text{keyboard}\ +12\:\text{USB drive}\ =52$, or a $50\:\text{keyboard}\:+8\:\text{USB drive}\:=58$.  Choose the latter as the more expensive option and return $58$.  

Function Description  

Complete the getMoneySpent function in the editor below.  

getMoneySpent has the following parameter(s):  

int keyboards[n]: the keyboard prices  
int drives[m]:  the drive prices  
int b: the budget  

Returns  

int: the maximum that can be spent, or $-1$ if it is not possible to buy both items

Input Format

The first line contains three space-separated integers $\boldsymbol{b}$, $n$, and $m$, the budget, the number of keyboard models and the number of USB drive models. 

The second line contains $n$ space-separated integers $\textit{keyboard[i]}$, the prices of each keyboard model. 

The third line contains $m$ space-separated integers $\textbf{drives}$, the prices of the USB drives.

Constraints

$1\leq n,m\leq1000$
$1\leq b\leq10^{6}$
The price of each item is in the inclusive range $[1,10^{6}]$.

Sample Input 0
10 2 3
3 1
5 2 8

Sample Output 0
9

Explanation 0

Buy the $2^{nd}$ keyboard and the $3^{rd}$ USB drive for a total cost of $8+1=9$.

Sample Input 1
5 1 1
4
5

Sample Output 1
-1

Explanation 1

There is no way to buy one keyboard and one USB drive because $4+5>5$, so return $-1$.

Difficulty: EASY
Tags: Implementation

Starter code:

Solutions:
import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
a = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
b = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
ans = -1
for x in a:
	for y in b:
		if x + y <= s:
			ans = max(ans, x + y)
print(ans)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
k = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
p = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
ans = -1
for i in k:
	for j in p:
		if i + j <= s and i + j >= ans:
			ans = i + j
print(ans)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max_spent = -1
for i in range(n):
	for j in range(m):
		total = keyboards[i] + pendrives[j]
		if total <= s and total > max_spent:
			max_spent = total
print(max_spent)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
sums = []
for i in keyboards:
	for j in pendrives:
		sums.append(i + j)
sums = sorted(sums)
if sums[0] > s:
	print(-1)
else:
	res = 0
	for item in sums:
		if item <= s:
			res = item
	print(res)


def solve(k, p, s):
	sol = -1
	for x in k:
		for y in p:
			if sol < x + y <= s:
				sol = x + y
				if sol == s:
					return sol
	return sol
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
print(solve(keyboards, pendrives, s))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
k = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
p = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
x = -1
for i in k:
	for j in p:
		if i + j <= s and i + j > x:
			x = i + j
print(x)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
keyboards = sorted(keyboards)
pendrives = sorted(pendrives)
ans = -1
for kk in keyboards:
	for pp in pendrives:
		if kk + pp > s:
			break
		if ans < kk + pp:
			ans = kk + pp
print(ans)


import sys
(s, n, m) = [int(x) for x in input().strip().split(' ')]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
total = -1
for x in keyboards:
	for y in pendrives:
		z = x + y
		if z > total and z <= s:
			total = z
print(total)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max_s = 0
for k in keyboards:
	for p in pendrives:
		temp_s = k + p
		if temp_s > max_s and temp_s <= s:
			max_s = temp_s
if max_s == 0:
	print(-1)
else:
	print(max_s)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
spent = -1
for kb in keyboards:
	for pen in pendrives:
		if spent < kb + pen <= s:
			spent = kb + pen
print(spent)


(s, n, m) = map(int, input().strip().split())
k = list(map(int, input().strip().split()))
d = list(map(int, input().strip().split()))
ans = -1
for keyboard in k:
	for driver in d:
		if ans < keyboard + driver <= s:
			ans = keyboard + driver
print(ans)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
keyboards.sort()
pendrives.sort()
max = -1
for i in reversed(keyboards):
	for j in reversed(pendrives):
		if i + j <= s and max < i + j:
			max = i + j
print(max)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
ans = -1
for kb in keyboards:
	for pd in pendrives:
		tot = kb + pd
		if tot <= s:
			if tot > ans:
				ans = tot
				if ans == s:
					break
print(ans)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
tmp = []
for i in range(n):
	for j in range(m):
		if keyboards[i] + pendrives[j] <= s:
			tmp.append(keyboards[i] + pendrives[j])
if tmp == []:
	print(-1)
else:
	print(max(tmp))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
totalCost = []
for keyboard in keyboards:
	for pendrive in pendrives:
		totalCost.append(keyboard + pendrive)
totalCost.sort()
minCost = min(totalCost)
maxCost = max(totalCost)
if s < minCost:
	print(-1)
elif s > maxCost:
	print(maxCost)
else:
	for i in range(len(totalCost) - 1):
		if s < totalCost[i + 1]:
			print(totalCost[i])
			break


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
import itertools
a = [keyboards, pendrives]
x = list(itertools.product(*a))
sumi = []
for i in x:
	sumi.append(sum(i))
sumi = sorted(sumi)
flag = False
if sumi[0] > s:
	print(-1)
else:
	for i in range(len(sumi)):
		if sumi[i] == s:
			print(sumi[i])
			break
		elif sumi[i] > s:
			print(sumi[i - 1])
			break
if flag:
	print(sumi[-1])


import sys
import itertools
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
l = [keyboards, pendrives]
sum = -1
for pair in itertools.product(*l):
	x = pair[0] + pair[1]
	if x <= s and x > sum:
		sum = x
print(sum)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
keyboards.sort()
pendrives.sort()
sm = [keyboards[i] + pendrives[j] for i in range(n) for j in range(m)]
sm.sort()
if sm[0] > s:
	print('-1')
else:
	mx = sm[0]
	i = 1
	while i < len(sm):
		if sm[i] > mx and sm[i] <= s:
			mx = sm[i]
		else:
			i = i + 1
	print(mx)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max = -1
for key in keyboards:
	for usb in pendrives:
		summ = key + usb
		if summ > max and summ <= s:
			max = summ
print(max)


import sys
from itertools import product

def max_money_spend(s, keyboards, pendrives):
	if min(keyboards) + min(pendrives) > s:
		return -1
	x = [sum(i) for i in list(product(keyboards, pendrives)) if sum(i) <= s]
	return max(x)
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
print(max_money_spend(s, keyboards, pendrives))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
r = 0
for i in range(n):
	for j in range(m):
		if keyboards[i] + pendrives[j] > r and keyboards[i] + pendrives[j] <= s:
			r = keyboards[i] + pendrives[j]
if r == 0:
	print(-1)
else:
	print(r)


import sys
import itertools
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
sumList = []
for (n, m) in itertools.product(keyboards, pendrives):
	sumList.append(n + m)
sumList.sort(reverse=True)
found = False
for sums in sumList:
	if sums <= s:
		print(sums)
		found = True
		break
if found == False:
	print('-1')


(s, n, m) = map(int, input().strip().split())
kN = list(map(int, input().strip().split()))
uM = list(map(int, input().strip().split()))
ans = -1
for i in range(0, n):
	for j in range(0, m):
		c = kN[i] + uM[j]
		if c > ans and c <= s:
			ans = c
print(ans)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]

def findGreatestTotal(keyboards, pendrives, money):
	max = -1
	for keyboard in keyboards:
		for pendrive in pendrives:
			total_cost = keyboard + pendrive
			if total_cost <= money and total_cost > max:
				max = total_cost
	return max
print(findGreatestTotal(keyboards, pendrives, s))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
money = -1
for i in range(0, n):
	for j in range(0, m):
		temp = keyboards[i] + pendrives[j]
		if temp <= s and temp > money:
			money = temp
print(money)


(s, n, m) = [int(num) for num in input().split()]
N = [int(num) for num in input().split()]
M = [int(num) for num in input().split()]

def solution():
	ans = -1
	for i in range(n):
		for j in range(m):
			tmp = N[i] + M[j]
			if tmp > s:
				continue
			elif tmp == s:
				return tmp
			elif tmp > ans:
				ans = tmp
	return ans
print(solution())


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
possible = list()
if min(keyboards) + min(pendrives) > s:
	print(-1)
else:
	for i in range(len(keyboards)):
		for j in range(len(pendrives)):
			total = keyboards[i] + pendrives[j]
			if total <= s:
				possible.append(total)
	print(max(possible))


def main():
	(s, n, m) = [int(temp) for temp in input().strip().split(' ')]
	k = [int(temp) for temp in input().strip().split(' ')]
	u = [int(temp) for temp in input().strip().split(' ')]
	b = []
	for i in range(n):
		for j in range(m):
			b.append(k[i] + u[j])
	b.sort()
	r = -1
	for i in range(len(b)):
		if b[i] <= s:
			r = b[i]
		else:
			break
	print(r)
main()


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
l = ((x, y) for x in keyboards for y in pendrives)
l3 = (x for x in l if x[0] + x[1] <= s)
maximum = -1
for x in l3:
	if x[0] + x[1] > maximum:
		maximum = x[0] + x[1]
else:
	print(maximum)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max = 0
for i in range(len(keyboards)):
	for j in range(len(pendrives)):
		curr = keyboards[i] + pendrives[j]
		if curr > max and curr <= s:
			max = curr
if max > 0:
	print(max)
elif max == 0:
	print(-1)


(s, m, n) = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
maxs = -1
for i in range(m):
	for j in range(n):
		s1 = a[i] + b[j]
		if s1 == s:
			print(s1)
			exit(None)
		elif s1 < s and s1 > maxs:
			maxs = s1
print(maxs)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
arrK = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
arrUSB = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
arrUSB.sort()
runMax = 0
possK = sorted((x for x in arrK if x < s))
for k in possK:
	if runMax == s:
		break
	possUSB = sorted((x for x in arrUSB if x <= s - k))
	for i in possUSB:
		checkV = i + k
		if runMax == s:
			break
		if checkV > runMax and checkV <= s:
			runMax = checkV
if runMax == 0:
	print(-1)
else:
	print(runMax)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
answer = -1
for i in range(n):
	for j in range(m):
		if keyboards[i] + pendrives[j] > answer and keyboards[i] + pendrives[j] <= s:
			answer = keyboards[i] + pendrives[j]
print(answer)


__author__ = 'bishal'
import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
p = 0
max1 = []
for i in range(0, n):
	for j in range(0, m):
		if keyboards[i] + pendrives[j] <= s:
			p = 1
			max1.insert(0, keyboards[i] + pendrives[j])
if p == 0:
	max1.insert(0, -1)
print(max(max1))


import itertools
(s, n, m) = [int(i) for i in input().strip().split(' ')]
kb = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pd = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
kb = sorted(kb)
pd = sorted(pd)
if min(kb) + min(pd) > s:
	print(-1)
else:
	all_sums = [x[0] + x[1] for x in itertools.product(kb, pd)]
	all_sums = sorted(all_sums)
	print(max([i for i in all_sums if i <= s]))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]

def checkConditions(s, n, m):
	if (s >= 1 and s <= 10 ** 6) and (n >= 1 and n <= 1000) and (m >= 1 and m <= 1000):
		return True
	else:
		return False

def printMaxSpent(s, n, m):
	max_spent = None
	for i in range(n):
		for a in range(m):
			total = keyboards[i] + pendrives[a]
			if max_spent == None and total <= s:
				max_spent = total
			elif max_spent != None and total > max_spent and (total <= s):
				max_spent = total
	if max_spent == None:
		print(-1)
	else:
		print(max_spent)
conditions = checkConditions(s, n, m)
if conditions:
	printMaxSpent(s, n, m)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
keyboards.sort(reverse=True)
pendrives.sort(reverse=True)
spend = -1
maxExp = 1000001
maxSpend = -1
table = [[pendrives[i] + keyboards[j] for j in range(n)] for i in range(m)]
for usb in range(m):
	for key in range(n):
		if table[usb][key] <= s and table[usb][key] >= maxSpend:
			maxSpend = table[usb][key]
		elif table[usb][key] < maxSpend:
			continue
print(maxSpend)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
maxi = -1
for i in keyboards:
	for j in pendrives:
		maxint = i + j
		if maxint <= s:
			if maxint > maxi:
				maxi = maxint
print(maxi)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
total_cost = []
sorted_keyboards = sorted(keyboards, reverse=True)
sorted_pendrives = sorted(pendrives, reverse=True)
for i in range(len(sorted_keyboards)):
	for j in range(len(sorted_pendrives)):
		sum = sorted_keyboards[i] + sorted_pendrives[j]
		if sum <= s:
			total_cost.append(sum)
if total_cost:
	print(max(total_cost))
else:
	print(-1)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
k = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
p = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max = 0
for i in k:
	for j in p:
		if i + j > max and i + j <= s:
			max = i + j
if max == 0:
	print('-1')
else:
	print(max)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
total = -1
for i in range(len(keyboards)):
	for j in range(len(pendrives)):
		new_total = keyboards[i] + pendrives[j]
		if new_total <= s and new_total > total:
			total = new_total
print(total)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
price = -1
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
for i in keyboards:
	priceKey = i
	for j in pendrives:
		pricePen = j
		if priceKey + pricePen <= s and priceKey + pricePen > price:
			price = priceKey + pricePen
print(price)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
l = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
m = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
maxr = -1
for x in l:
	for y in m:
		if x + y > maxr:
			if s >= x + y:
				maxr = x + y
print(maxr)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
k = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
p = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
maxim = 0
for i in range(n):
	for j in range(m):
		if k[i] + p[j] <= s:
			if k[i] + p[j] > maxim:
				maxim = k[i] + p[j]
if maxim == 0:
	print(-1)
else:
	print(maxim)


import sys
from bisect import bisect
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = sorted([int(keyboards_temp) for keyboards_temp in input().strip().split(' ')])
pendrives = sorted([int(pendrives_temp) for pendrives_temp in input().strip().split(' ')])
total = -1
for k in keyboards:
	p = s - k
	ind = bisect(pendrives, p)
	if ind > 0:
		t = pendrives[ind - 1] + k
		total = max(total, t)
	else:
		break
print(total)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max = -1
for k in keyboards:
	for p in pendrives:
		current = k + p
		if current <= s and (max < 0 or current > max):
			max = current
print(max)


import sys
[s, n, m] = [int(x) for x in input().strip().split(' ')]
k = [int(x) for x in input().strip().split(' ')]
u = [int(x) for x in input().strip().split(' ')]
max = -1
for x in range(n):
	for y in range(m):
		su = k[x] + u[y]
		if su > max and su <= s:
			max = su
print(max)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max_val = 0
for i in range(len(keyboards)):
	for j in range(len(pendrives)):
		if keyboards[i] + pendrives[j] > max_val and keyboards[i] + pendrives[j] <= s:
			max_val = keyboards[i] + pendrives[j]
if max_val == 0:
	print('-1')
else:
	print(max_val)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
sum_list = []
for i in range(0, n):
	for j in range(0, m):
		sum_list.append(keyboards[i] + pendrives[j])
max_s = -1
for i in range(0, len(sum_list)):
	if sum_list[i] > max_s and sum_list[i] <= s:
		max_s = sum_list[i]
print(str(max_s))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max = -1
sum = 0
for k in keyboards:
	for p in pendrives:
		sum = p + k
		if sum > max and sum <= s:
			max = sum
print(max)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
keyboards.sort()
keyboards.reverse()
pendrives.sort()
pendrives.reverse()
max_so_far = -1
for i in keyboards:
	for j in pendrives:
		if i + j > max_so_far and i + j <= s:
			max_so_far = i + j
print(max_so_far)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max = 0
for x in range(n):
	for i in range(m):
		if s >= keyboards[x] + pendrives[i] and max < keyboards[x] + pendrives[i]:
			max = keyboards[x] + pendrives[i]
if max == 0 or n == 0 or m == 0:
	print(-1)
else:
	print(max)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
purchases = -1
for k in keyboards:
	for p in pendrives:
		if k + p <= s and k + p > purchases:
			purchases = k + p
print(purchases)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
ans = -1
for one in keyboards:
	for two in pendrives:
		if one + two <= s:
			if one + two > ans:
				ans = one + two
print(ans)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]

def findMaxPrice(s, keyboards, pendrives):
	max_so_far = -1
	for i in keyboards:
		for j in pendrives:
			if i + j <= s and i + j > max_so_far:
				max_so_far = i + j
	return max_so_far
print(findMaxPrice(s, keyboards, pendrives))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
sum_list = []
for i in keyboards:
	for j in pendrives:
		sum_list.append(i + j)
sum_list = sorted(sum_list, reverse=True)
check = True
for i in sum_list:
	if i <= s:
		print(i)
		check = False
		break
if check:
	print(-1)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
sum = -1
for i in range(len(keyboards)):
	for j in range(len(pendrives)):
		if keyboards[i] + pendrives[j] <= s and keyboards[i] + pendrives[j] > sum:
			sum = keyboards[i] + pendrives[j]
print(sum)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
prev = 0
pres = 0
for i in keyboards:
	for j in pendrives:
		pres = i + j
		if pres <= s and pres > prev:
			prev = pres
if prev <= s and prev != 0:
	print(prev)
else:
	print('-1')


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
L = []
for i in keyboards:
	for j in pendrives:
		if i + j <= s:
			L.append(i + j)
if len(L) == 0:
	print(-1)
else:
	print(max(L))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
amount_of_money_to_spend = 0
for keyboard in keyboards:
	for pendrive in pendrives:
		purchase_amount = keyboard + pendrive
		if purchase_amount > amount_of_money_to_spend and purchase_amount <= s:
			amount_of_money_to_spend = purchase_amount
amount_of_money_to_spend = amount_of_money_to_spend if amount_of_money_to_spend > 0 else -1
print(amount_of_money_to_spend)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
k = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
u = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
maxspend = -1
for keyboard in k:
	for usb in u:
		if keyboard + usb > maxspend and keyboard + usb <= s:
			maxspend = keyboard + usb
print(maxspend)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
result = -1
for keyboard in keyboards:
	for pendrive in pendrives:
		total = keyboard + pendrive
		if total > result and total <= s:
			result = max(result, total)
print(result)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = sorted([int(keyboards_temp) for keyboards_temp in input().strip().split(' ') if int(keyboards_temp) <= s], reverse=True)
pendrives = sorted([int(pendrives_temp) for pendrives_temp in input().strip().split(' ') if int(pendrives_temp) <= s], reverse=True)
total = -1
for key in keyboards:
	for pen in pendrives:
		temp = key + pen
		if temp <= s and temp > total:
			total = temp
print(total)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
maxsum = -1
for key in keyboards:
	for pen in pendrives:
		if key + pen > maxsum and key + pen <= s:
			maxsum = key + pen
print(maxsum)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
total = -1
for k in keyboards:
	for p in pendrives:
		if k + p > total and k + p <= s:
			total = k + p
print(total)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
soma = []
for keyb in keyboards:
	for pend in pendrives:
		total = keyb + pend
		if total <= s:
			soma.append(total)
if len(soma) is 0:
	print(-1)
else:
	print(max(soma))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]

def max_amount():
	lst = []
	for k in keyboards:
		for u in pendrives:
			if k + u <= s:
				lst.append(k + u)
	if len(lst):
		return max(lst)
	else:
		return -1
print(max_amount())


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max_total = 0
total = 0
for k in keyboards:
	for p in pendrives:
		if k + p <= s:
			total = k + p
			if max_total < total:
				max_total = total
if max_total == 0:
	print(-1)
else:
	print(max_total)


import sys

def maxValue(s, n, m, keyboards, pendrives):
	max_value = -1
	keyboards.sort(reverse=True)
	pendrives.sort(reverse=True)
	for x in keyboards:
		for y in pendrives:
			if x + y > s:
				continue
			if x + y < max_value:
				break
			elif x + y >= max_value:
				max_value = x + y
				break
	return max_value
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
print(maxValue(s, n, m, keyboards, pendrives))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
key = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
usb = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
left = s
for key_p in key:
	for usb_p in usb:
		left_p = s - (key_p + usb_p)
		if left_p < left and left_p >= 0:
			left = left_p
if left < s:
	print(s - left)
else:
	print(-1)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
valMax = -1
for key in keyboards:
	for mouse in pendrives:
		total = key + mouse
		if total > valMax and total <= s:
			valMax = total
print(valMax)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]

def buy():
	if n < 1 or n > 1000:
		return -1
	if m < 1 or m > 1000:
		return -1
	if s < 1 or s > 1000000:
		return -1
	max_list = []
	for y in keyboards:
		for z in pendrives:
			if y + z <= s:
				max_list.append(y + z)
	if not max_list:
		return -1
	else:
		return max(max_list)
print(buy())


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max_key = max(keyboards)
max_pen = max(pendrives)
val1 = -1
for i in keyboards:
	for j in pendrives:
		if val1 < i + j and s >= i + j:
			val1 = i + j
print(val1)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]

def totalCost(keyboards, pendrives, limit):
	keyboards_new = sorted(keyboards, reverse=True)
	pendrives_new = sorted(pendrives, reverse=True)
	costs = [-1]
	for i in range(0, len(keyboards_new)):
		for j in range(0, len(pendrives_new)):
			if pendrives_new[j] <= limit - keyboards_new[i]:
				costs.append(keyboards_new[i] + pendrives_new[j])
				break
	return max(costs)
amountspend = totalCost(keyboards, pendrives, s)
print(amountspend)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max = -1
for i in range(n):
	for j in range(m):
		if keyboards[i] + pendrives[j] > max and keyboards[i] + pendrives[j] <= s:
			max = keyboards[i] + pendrives[j]
print(max)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
spending = min(keyboards) + min(pendrives)
if spending < s:
	for k in keyboards:
		for p in pendrives:
			temp = k + p
			if temp <= s and temp > spending:
				spending = temp
elif spending > s:
	spending = -1
print(spending)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
purch_total = []
sort_purch_total = []
for i in keyboards:
	for j in pendrives:
		purch_total.append(i + j)
sort_purch_total = sorted(purch_total)
max_purch = 0
if s < min(purch_total):
	print(-1)
elif s >= max(purch_total):
	print(max(purch_total))
else:
	for k in sort_purch_total:
		if s >= k and k >= max_purch:
			max_purch = k
	print(max_purch)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
possible_buy = []
for i in keyboards:
	for j in pendrives:
		if i + j <= s:
			possible_buy.append(i + j)
if not possible_buy:
	print(-1)
else:
	print(max(possible_buy))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
spend = -1
for i in range(n):
	for j in range(m):
		if keyboards[i] + pendrives[j] <= s:
			spend = max(spend, keyboards[i] + pendrives[j])
print(spend)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
value = 0
for i in keyboards:
	for j in pendrives:
		if i + j <= s and value < i + j:
			value = i + j
if value == 0:
	print(-1)
else:
	print(value)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
sums = []
for k in keyboards:
	for p in pendrives:
		sums.append(int(k + p))
a = sorted(sums)[::-1]
price = 0
for i in range(len(a)):
	if a[i] > s:
		price = -1
		continue
	elif a[i] <= s:
		price = a[i]
		break
	else:
		price = -1
print(price)


ans = input().split(' ')
s = int(ans[0])
n = int(ans[1])
m = int(ans[2])
pk = input().split(' ')
pu = input().split(' ')
amt = -1
for j in range(n):
	for i in range(m):
		if int(pk[j]) + int(pu[i]) > amt and int(pk[j]) + int(pu[i]) <= s:
			amt = int(pk[j]) + int(pu[i])
print(amt)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
num = -1
for x in range(len(keyboards)):
	for y in range(len(pendrives)):
		n = keyboards[x] + pendrives[y]
		if n <= s and n > num:
			num = n
print(num)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
a = [-1]
for i in range(n):
	for j in range(m):
		sum1 = keyboards[i] + pendrives[j]
		if sum1 <= s:
			a.append(sum1)
print(max(a))


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
keyboards.sort(reverse=True)
pendrives.sort(reverse=True)
maxAmount = -1
for k in keyboards:
	for p in pendrives:
		total = k + p
		if total > maxAmount and total <= s:
			maxAmount = total
print(maxAmount)


(money, k, u) = map(int, input().split(' '))
keyboard = list(map(int, input().split(' ')))
usb = list(map(int, input().split(' ')))
answer = -1
keyboard.sort()
keyboard = keyboard[::-1]
usb.sort()
usb = usb[::-1]
for k in keyboard:
	for u in usb:
		price = k + u
		if price <= money and price > answer:
			answer = price
print(answer)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
val = -1
for I in range(0, n):
	for J in range(0, m):
		temp = keyboards[I] + pendrives[J]
		if temp >= val and temp <= s:
			val = temp
print(val)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
keyboards = sorted(keyboards)
pendrives = sorted(pendrives)
answer = 0
if keyboards[0] + pendrives[0] > s:
	print(-1)
elif keyboards[-1] + pendrives[-1] < s:
	print(keyboards[-1] + pendrives[-1])
else:
	for x in range(len(keyboards)):
		sum = keyboards[x]
		for y in range(len(pendrives)):
			sum = keyboards[x] + pendrives[y]
			if sum > s:
				break
			elif sum == s:
				answer = s
				break
			elif s - answer > s - sum:
				answer = sum
	print(answer)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]

def solve():
	max_spend = -1
	for k in keyboards:
		for p in pendrives:
			spend = k + p
			if spend > s:
				continue
			if spend > max_spend:
				max_spend = spend
	return max_spend
print(solve())


import sys
import itertools
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
allposs = list(itertools.product(keyboards, pendrives))
total = 0
for i in allposs:
	if i[0] + i[1] > total and i[0] + i[1] <= s:
		total = i[0] + i[1]
if total == 0:
	print(-1)
else:
	print(total)


import sys
import itertools

def main():
	(s, n, m) = [int(s) for s in input().strip().split()]
	keyboards = [int(k) for k in input().strip().split()]
	pendrives = [int(p) for p in input().strip().split()]
	sums = [sum(pair) for pair in list(itertools.product(keyboards, pendrives)) if sum(pair) <= s]
	print(max(sums) if len(sums) > 0 else -1)
main()


from itertools import product
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max_c = -1
for i in list(product(keyboards, pendrives)):
	cost = i[0] + i[1]
	if cost <= s and cost > max_c:
		max_c = cost
		if max_c == s:
			break
print(max_c)


import sys
maxSoFar = -1
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
for keyB in keyboards:
	for penD in pendrives:
		total = keyB + penD
		if total > maxSoFar and total <= s:
			maxSoFar = total
print(maxSoFar)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
k = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
p = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
k.sort()
p.sort()
i = n - 1
j = 0
sm = -1
while i >= 0 and j < m:
	while j < m and p[j] + k[i] <= s:
		if sm < p[j] + k[i]:
			sm = p[j] + k[i]
		j += 1
	i -= 1
if sm == -1:
	print(-1)
else:
	print(sm)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
maxx = -1
for i in range(n):
	for j in range(m):
		if keyboards[i] + pendrives[j] <= s and keyboards[i] + pendrives[j] > maxx:
			maxx = keyboards[i] + pendrives[j]
print(maxx)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = sorted(list(set([int(keyboards_temp) for keyboards_temp in input().strip().split(' ')])))
pendrives = sorted(list(set([int(pendrives_temp) for pendrives_temp in input().strip().split(' ')])))
temp = 0
final = 0
for i in keyboards:
	for j in pendrives:
		if i + j <= s:
			temp = i + j
			if temp > final:
				final = temp
if final != 0:
	print(final)
else:
	print(-1)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
if s in keyboards + pendrives:
	print(-1)
elif max(keyboards) + max(pendrives) <= s:
	print(max(keyboards) + max(pendrives))
else:
	cost = 0
	for price in range(n):
		tmp_k = keyboards[price]
		for price2 in range(m):
			tmp_p = pendrives[price2]
			if tmp_p + tmp_k > cost and tmp_p + tmp_k <= s:
				cost = tmp_p + tmp_k
	if cost == 0:
		cost = -1
	print(cost)


import sys
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = sorted([int(keyboards_temp) for keyboards_temp in input().strip().split(' ') if int(keyboards_temp) < s])
pendrives = sorted([int(pendrives_temp) for pendrives_temp in input().strip().split(' ') if int(pendrives_temp) < s])

def spendmoney(s, keyboards, pendrives):
	spend = -1
	keyboards = keyboards[::-1]
	pendrives = pendrives[::-1]
	for i in range(len(keyboards)):
		for j in range(len(pendrives)):
			if keyboards[i] + pendrives[j] <= s:
				spend = max(spend, keyboards[i] + pendrives[j])
	return spend
print(spendmoney(s, keyboards, pendrives))


import sys
import itertools
(s, n, m) = input().strip().split(' ')
(s, n, m) = [int(s), int(n), int(m)]
keyboards = [int(keyboards_temp) for keyboards_temp in input().strip().split(' ')]
pendrives = [int(pendrives_temp) for pendrives_temp in input().strip().split(' ')]
max_cost = -1
keyboard_X_pendrive = itertools.product(keyboards, pendrives)
for item in keyboard_X_pendrive:
	if sum(item) <= s and sum(item) > max_cost:
		max_cost = sum(item)
print(max_cost)