N = int(eval(input()))

S = eval(input())



A1, A2 = [0 for _ in range(N)], [0 for _ in range(N)]

for i, s in enumerate(S):

  if i == 0:

    if s == '(': A1[i] = 1

    else: A2[i] = 1

  else:

    if s == '(':

      A1[i] = A1[i-1] + 1

      A2[i] = A2[i-1] 

    else:

      A1[i] = A1[i-1] 

      A2[i] = A2[i-1] + 1



A3, A4 = [0 for _ in range(N)], [0 for _ in range(N)]

for i in range(N-1, -1, -1):

  if i == N-1:

    if S[i] == '(': A3[i] = 1

    else: A4[i] = 1

  else:

    if S[i] == '(':

      A3[i] = A3[i+1] + 1

      A4[i] = A4[i+1] 

    else:

      A3[i] = A3[i+1] 

      A4[i] = A4[i+1] + 1



v1 = max(max([a2-a1 for a1, a2 in zip(A1,A2)]),0)

v2 = max(max([a3-a4 for a3, a4 in zip(A3,A4)]),0)

print(('('*v1+S+')'*v2))
from itertools import groupby



N = int(eval(input()))

S = eval(input())



cnt_l = 0

cnt_r = 0

ans = ''

for key, elems in groupby(S):

    elems = list(elems)

    if key == '(':

        cnt_l += len(elems)

        ans += '(' * len(elems)

    else:

        cnt_r += len(elems)

        ans += ')' * len(elems)

        if cnt_l < cnt_r:

            ans = '(' * (cnt_r - cnt_l) + ans

            cnt_l += (cnt_r - cnt_l)



print((ans + ')' * (cnt_l - cnt_r)))

N = int(eval(input()))
S = eval(input())
A1, A2 = [0 for _ in range(N)], [0 for _ in range(N)]
for i, s in enumerate(S):
    if i == 0:
        if s == "(":
            A1[i] = 1
        else:
            A2[i] = 1
    else:
        if s == "(":
            A1[i] = A1[i - 1] + 1
            A2[i] = A2[i - 1]
        else:
            A1[i] = A1[i - 1]
            A2[i] = A2[i - 1] + 1
A3, A4 = [0 for _ in range(N)], [0 for _ in range(N)]
for i in range(N - 1, -1, -1):
    if i == N - 1:
        if S[i] == "(":
            A3[i] = 1
        else:
            A4[i] = 1
    else:
        if S[i] == "(":
            A3[i] = A3[i + 1] + 1
            A4[i] = A4[i + 1]
        else:
            A3[i] = A3[i + 1]
            A4[i] = A4[i + 1] + 1
v1 = max(max([a2 - a1 for a1, a2 in zip(A1, A2)]), 0)
v2 = max(max([a3 - a4 for a3, a4 in zip(A3, A4)]), 0)
print(("(" * v1 + S + ")" * v2))

from itertools import groupby

N = int(eval(input()))
S = eval(input())
cnt_l = 0
cnt_r = 0
ans = ""
for key, elems in groupby(S):
    elems = list(elems)
    if key == "(":
        cnt_l += len(elems)
        ans += "(" * len(elems)
    else:
        cnt_r += len(elems)
        ans += ")" * len(elems)
        if cnt_l < cnt_r:
            ans = "(" * (cnt_r - cnt_l) + ans
            cnt_l += cnt_r - cnt_l
print((ans + ")" * (cnt_l - cnt_r)))

+from itertools import groupby + -A1, A2 = [0 for _ in range(N)], [0 for _ in range(N)] -for i, s in enumerate(S): -    if i == 0: -        if s == "(": -            A1[i] = 1 -        else: -            A2[i] = 1 +cnt_l = 0 +cnt_r = 0 +ans = "" +for key, elems in groupby(S): +    elems = list(elems) +    if key == "(": +        cnt_l += len(elems) +        ans += "(" * len(elems) -        if s == "(": -            A1[i] = A1[i - 1] + 1 -            A2[i] = A2[i - 1] -        else: -            A1[i] = A1[i - 1] -            A2[i] = A2[i - 1] + 1 -A3, A4 = [0 for _ in range(N)], [0 for _ in range(N)] -for i in range(N - 1, -1, -1): -    if i == N - 1: -        if S[i] == "(": -            A3[i] = 1 -        else: -            A4[i] = 1 -    else: -        if S[i] == "(": -            A3[i] = A3[i + 1] + 1 -            A4[i] = A4[i + 1] -        else: -            A3[i] = A3[i + 1] -            A4[i] = A4[i + 1] + 1 -v1 = max(max([a2 - a1 for a1, a2 in zip(A1, A2)]), 0) -v2 = max(max([a3 - a4 for a3, a4 in zip(A3, A4)]), 0) -print(("(" * v1 + S + ")" * v2)) +        cnt_r += len(elems) +        ans += ")" * len(elems) +        if cnt_l < cnt_r: +            ans = "(" * (cnt_r - cnt_l) + ans +            cnt_l += cnt_r - cnt_l +print((ans + ")" * (cnt_l - cnt_r)))