You are given two bracket sequences (not necessarily regular) s and t consisting only of characters '(' and ')'. You want to construct the shortest regular bracket sequence that contains both given bracket sequences as subsequences (not necessarily contiguous).

Recall what is the regular bracket sequence:

  * () is the regular bracket sequence; 
  * if S is the regular bracket sequence, then (S) is a regular bracket sequence; 
  * if S and T regular bracket sequences, then ST (concatenation of S and T) is a regular bracket sequence. 



Recall that the subsequence of the string s is such string t that can be obtained from s by removing some (possibly, zero) amount of characters. For example, "coder", "force", "cf" and "cores" are subsequences of "codeforces", but "fed" and "z" are not.

Input

The first line of the input contains one bracket sequence s consisting of no more than 200 characters '(' and ')'.

The second line of the input contains one bracket sequence t consisting of no more than 200 characters '(' and ')'.

Output

Print one line — the shortest regular bracket sequence that contains both given bracket sequences as subsequences (not necessarily contiguous). If there are several answers, you can print any.

Examples

Input


(())(()
()))()


Output


(())()()


Input


)
((


Output


(())


Input


)
)))


Output


((()))


Input


())
(()(()(()(


Output


(()()()(()()))

Difficulty: HARD
Tags: String algorithms

Dynamic programming

Amortized analysis
Skill types: Dynamic programming

Amortized analysis

Starter code:

Solutions:
def scs(str1, str2):
	INF = 10 ** 9
	dp = [[[INF] * 210 for _ in range(210)] for _ in range(210)]
	dp[0][0][0] = 0
	prv = [[[None] * 210 for _ in range(210)] for _ in range(210)]
	len_str1 = len(str1)
	len_str2 = len(str2)
	str1 += '#'
	str2 += '#'
	for i in range(len_str1 + 1):
		for j in range(len_str2 + 1):
			for k in range(201):
				k2 = k + 1
				i2 = i + (str1[i] == '(')
				j2 = j + (str2[j] == '(')
				if dp[i2][j2][k2] > dp[i][j][k] + 1:
					dp[i2][j2][k2] = dp[i][j][k] + 1
					prv[i2][j2][k2] = (i, j, k)
			for k in range(1, 201):
				k2 = k - 1
				i2 = i + (str1[i] == ')')
				j2 = j + (str2[j] == ')')
				if dp[i2][j2][k2] > dp[i][j][k] + 1:
					dp[i2][j2][k2] = dp[i][j][k] + 1
					prv[i2][j2][k2] = (i, j, k)
	ans = INF
	cnt = 0
	for (c, val) in enumerate(dp[len_str1][len_str2]):
		if c + val < ans + cnt:
			ans = val
			cnt = c
	res = []
	(i, j, k) = (len_str1, len_str2, cnt)
	while i > 0 or j > 0 or k > 0:
		(prv_i, prv_j, prv_k) = prv[i][j][k]
		if prv_k < k:
			res.append('(')
		else:
			res.append(')')
		(i, j, k) = (prv_i, prv_j, prv_k)
	return '(' * (ans - len(res) - cnt) + ''.join(res[::-1]) + ')' * cnt
s = input()
t = input()
print(scs(s, t))


s1 = input()
s2 = input()
inf = 10 ** 8
n = len(s1)
m = len(s2)
d = [[[inf for x in range(n + m + 10)] for y in range(m + 10)] for z in range(n + 10)]
tata = [[[0 for x in range(n + m + 10)] for y in range(m + 10)] for z in range(n + 10)]
d[0][0][0] = 0
for i in range(n + 1):
	for j in range(m + 1):
		i1 = i + (i < n and s1[i] == '(')
		j1 = j + (j < m and s2[j] == '(')
		for k in range(n + m + 1):
			if d[i][j][k] == inf:
				continue
			if d[i][j][k] + 1 < d[i1][j1][k + 1]:
				d[i1][j1][k + 1] = d[i][j][k] + 1
				tata[i1][j1][k + 1] = 1
		i1 = i + (i < n and s1[i] == ')')
		j1 = j + (j < m and s2[j] == ')')
		for k in reversed(range(1, n + m + 1)):
			if d[i][j][k] == inf:
				continue
			if d[i][j][k] + 1 < d[i1][j1][k - 1]:
				d[i1][j1][k - 1] = d[i][j][k] + 1
				tata[i1][j1][k - 1] = -1
lmin = inf
pz = 0
for i in range(n + m + 1):
	if d[n][m][i] + i < lmin:
		lmin = d[n][m][i] + i
		pz = i
ans = ''
for i in range(pz):
	ans += ')'
i = n
j = m
k = pz
while i > 0 or j > 0 or k > 0:
	val = d[i][j][k] - 1
	if tata[i][j][k] == 1:
		c = '('
		k -= 1
	else:
		c = ')'
		k += 1
	ans += c
	i1 = i > 0 and s1[i - 1] == c
	j1 = j > 0 and s2[j - 1] == c
	if d[i][j][k] == val:
		continue
	elif i1 > 0 and d[i - 1][j][k] == val:
		i -= 1
	elif j1 > 0 and d[i][j - 1][k] == val:
		j -= 1
	else:
		i -= 1
		j -= 1
ans = ans[::-1]
print(ans)


(S, T) = (input(), input())
s1 = [1 if c == '(' else -1 for c in S]
s2 = [1 if c == '(' else -1 for c in T]
(len1, len2) = (len(s1), len(s2))
inf = 10 ** 9
dp = [[[inf] * (len1 + len2 + 1) for _ in range(len2 + 1)] for _ in range(len1 + 1)]
dp[0][0][0] = 0
for i in range(len1 + 1):
	for j in range(len2 + 1):
		for k in range(len1 + len2):
			if i < len1:
				k1 = k + s1[i]
				if k1 < 0:
					dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k] + 2)
				else:
					dp[i + 1][j][k1] = min(dp[i + 1][j][k1], dp[i][j][k] + 1)
			if j < len2:
				k2 = k + s2[j]
				if k2 < 0:
					dp[i][j + 1][k] = min(dp[i][j + 1][k], dp[i][j][k] + 2)
				else:
					dp[i][j + 1][k2] = min(dp[i][j + 1][k2], dp[i][j][k] + 1)
			if i < len1 and j < len2:
				if s1[i] == s2[j]:
					if k1 < 0:
						dp[i + 1][j + 1][k] = min(dp[i + 1][j + 1][k], dp[i][j][k] + 2)
					else:
						dp[i + 1][j + 1][k1] = min(dp[i + 1][j + 1][k1], dp[i][j][k] + 1)
k = 0
length = inf
for i in range(len1 + len2 + 1):
	if dp[len1][len2][i] + i < length:
		length = dp[len1][len2][i] + 1
		k = i
(i, j) = (len1, len2)
ans = [')'] * k
while i > 0 or j > 0:
	if i > 0 and j > 0 and (s1[i - 1] == s2[j - 1]):
		if dp[i - 1][j - 1][k - s1[i - 1]] + 1 == dp[i][j][k]:
			ans.append(S[i - 1])
			k -= s1[i - 1]
			i -= 1
			j -= 1
			continue
		elif k == 0 and s1[i - 1] == -1 and (dp[i - 1][j - 1][k] + 2 == dp[i][j][k]):
			ans.extend((S[i - 1], '('))
			i -= 1
			j -= 1
			continue
	if i > 0:
		if dp[i - 1][j][k - s1[i - 1]] + 1 == dp[i][j][k]:
			ans.append(S[i - 1])
			k -= s1[i - 1]
			i -= 1
			continue
		elif k == 0 and s1[i - 1] == -1 and (dp[i - 1][j][k] + 2 == dp[i][j][k]):
			ans.extend((S[i - 1], '('))
			i -= 1
			continue
	if j > 0:
		if dp[i][j - 1][k - s2[j - 1]] + 1 == dp[i][j][k]:
			ans.append(T[j - 1])
			k -= s2[j - 1]
			j -= 1
			continue
		elif k == 0 and s2[j - 1] == -1 and (dp[i][j - 1][k] + 2 == dp[i][j][k]):
			ans.extend((T[j - 1], '('))
			j -= 1
			continue
	raise Exception(i, j, k, S[i - 1], T[j - 1])
print(*ans[::-1], sep='')


def limitbal(a, b):
	min_level = 0
	level = 0
	for aa in a:
		if aa == '(':
			level += 1
		else:
			level -= 1
			if level < min_level:
				min_level = level
	for aa in b:
		if aa == '(':
			level += 1
		else:
			level -= 1
			if level < min_level:
				min_level = level
	return -min_level + len(a) + len(b) + (level - min_level)

def go():
	a = input()
	b = input()
	lena = len(a)
	lenb = len(b)
	a += 'X'
	b += 'X'
	bal_lim = limitbal(a, b) // 2 + 3
	tab = [[[None] * (bal_lim + 2) for _ in range(lenb + 1)] for _ in range(lena + 1)]
	par = [[[None] * (bal_lim + 2) for _ in range(lenb + 1)] for _ in range(lena + 1)]
	tab[0][0][0] = 0
	que = [(0, 0, 0)]
	index = 0
	while tab[lena][lenb][0] is None:
		(i, j, bal) = que[index]
		ai = a[i]
		bj = b[j]
		if bal < bal_lim and (bal == 0 or not ai == bj == ')'):
			ii = i
			jj = j
			if ai == '(':
				ii = i + 1
			if bj == '(':
				jj = j + 1
			if tab[ii][jj][bal + 1] is None:
				tab[ii][jj][bal + 1] = tab[i][j][bal] + 1
				par[ii][jj][bal + 1] = (i, j, bal, '(')
				que.append((ii, jj, bal + 1))
		if bal > 0 and (not ai == bj == '('):
			ii = i
			jj = j
			if ai == ')':
				ii = i + 1
			if bj == ')':
				jj = j + 1
			if tab[ii][jj][bal - 1] is None:
				tab[ii][jj][bal - 1] = tab[i][j][bal] + 1
				par[ii][jj][bal - 1] = (i, j, bal, ')')
				que.append((ii, jj, bal - 1))
		index += 1
	i = lena
	j = lenb
	bal = 0
	answer = []
	while (i, j, bal) != (0, 0, 0):
		(i, j, bal, symb) = par[i][j][bal]
		answer.append(symb)
	print(''.join(reversed(answer)))
go()


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest
from copy import deepcopy
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
s = list(input())
n = len(s)
t = list(input())
m = len(t)
d = {'(': 1, ')': -1}
s = [1 if x == '(' else -1 for x in s]
t = [1 if x == '(' else -1 for x in t]
s.append(0)
t.append(0)
K = 210
dp = [[[INF] * K for _ in range(m + 2)] for i in range(n + 2)]
pa = [[[-1 for k in range(K)] for j in range(m + 1)] for i in range(n + 1)]
dp[0][0][0] = 0
for i in range(n + 1):
	for j in range(m + 1):
		for k in range(201):
			nk = k + 1
			ni = i + (s[i] == 1)
			nj = j + (t[j] == 1)
			if dp[ni][nj][nk] > dp[i][j][k] + 1:
				dp[ni][nj][nk] = dp[i][j][k] + 1
				pa[ni][nj][nk] = (i, j, k)
		for k in range(1, 201):
			nk = k - 1
			ni = i + (s[i] == -1)
			nj = j + (t[j] == -1)
			if dp[ni][nj][nk] > dp[i][j][k] + 1:
				dp[ni][nj][nk] = dp[i][j][k] + 1
				pa[ni][nj][nk] = (i, j, k)
rem = -1
mi = INF
for (k, num) in enumerate(dp[n][m]):
	if k + num < mi:
		rem = k
		mi = k + num
res = []
(i, j, k) = (n, m, rem)
while i > 0 or j > 0 or k > 0:
	(pi, pj, pk) = pa[i][j][k]
	if k > pk:
		res.append('(')
	else:
		res.append(')')
	(i, j, k) = (pi, pj, pk)
res = res[::-1]
for i in range(rem):
	res.append(')')
print(''.join(res))


import sys
import time

def play(s1, s2):
	n = len(s1)
	m = len(s2)
	maxBalance = 200
	dp = []
	p = []
	for i in range(0, n + 1):
		dp.append([])
		p.append([])
		for j in range(0, m + 1):
			dp[i].append([])
			p[i].append([])
			for a in range(0, maxBalance + 1):
				dp[i][j].append(sys.maxsize)
				p[i][j].append(None)
	dp[0][0][0] = 0
	for i in range(0, n + 1):
		for j in range(0, m + 1):
			for a in range(0, maxBalance + 1):
				if dp[i][j][a] == sys.maxsize:
					continue
				next_i = i + (i < n and s1[i] == '(')
				next_j = j + (j < m and s2[j] == '(')
				if a < maxBalance and dp[next_i][next_j][a + 1] > dp[i][j][a] + 1:
					dp[next_i][next_j][a + 1] = dp[i][j][a] + 1
					p[next_i][next_j][a + 1] = (i, j, a, '(')
				next_i = i + (i < n and s1[i] == ')')
				next_j = j + (j < m and s2[j] == ')')
				if a > 0 and dp[next_i][next_j][a - 1] > dp[i][j][a] + 1:
					dp[next_i][next_j][a - 1] = dp[i][j][a] + 1
					p[next_i][next_j][a - 1] = (i, j, a, ')')
	aa = 0
	nn = n
	mm = m
	for a in range(0, maxBalance + 1):
		if dp[n][m][a] + a < dp[n][m][aa] + aa:
			aa = a
	ret = ')' * aa
	while nn > 0 or mm > 0 or aa > 0:
		d = p[nn][mm][aa]
		nn = d[0]
		mm = d[1]
		aa = d[2]
		ret += d[3]
	return ret[::-1]

def main():
	print(play(input(), input()))
main()