program sample
    implicit none
    character(100)::s,sub
    character(1000)::ans
    integer(8) :: i,j,m,n,a,b,l
    integer(8),allocatable :: x(:),y(:)
  
    read(*,*) n
    read(*,*)s
    a=0
    b=0
    ans=''
    sub=''
    l=0
    do i=1,n
        if(s(i:i) .eq. '('.and. b==0)then
            a=a+1
            b=0
            !m=len_trim(sub)
            sub=sub(1:l)//'('
            l=l+1
        elseif(s(i:i) .eq. '(')then
            do j=1,a
                sub=sub(1:l)//')'
                l=l+1
            end do
            sub=sub(1:l)//'('
            l=l+1
            b=0
            a=1
        elseif(s(i:i) .eq. ')')then
            a=a-1
            b=1
            
            sub=sub(1:l)//')'
            l=l+1
            if(a<0)then
                sub='('//sub(1:l)
                l=l+1
                a=0
            end if
        end if
    end do
    if(a>0)then
        do i=1,a
            sub=sub(1:l)//')'
            l=l+1
        end do
    end if
    write(*,'(a)') sub
  
    stop
end program sample
  

