✅ 정답 공개
a = [int(x) for x in input().strip().split(',')]
b = [int(x) for x in input().strip().split(',')]
k = int(input())
def pick_max(nums, count):
drop = len(nums) - count
stack = []
for n in nums:
while drop > 0 and stack and stack[-1] < n:
stack.pop()
drop -= 1
stack.append(n)
return stack[:count]
def greater(left, i, right, j):
while i < len(left) and j < len(right) and left[i] == right[j]:
i += 1
j += 1
if j == len(right):
return True
if i == len(left):
return False
return left[i] > right[j]
def merge(left, right):
i = 0
j = 0
result = []
while i < len(left) or j < len(right):
if greater(left, i, right, j):
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
return result
best = []
start = max(0, k - len(b))
end = min(k, len(a))
for take_a in range(start, end + 1):
take_b = k - take_a
cand = merge(pick_max(a, take_a), pick_max(b, take_b))
if greater(cand, 0, best, 0):
best = cand
text = ''
for n in best:
text += str(n)
print(text)