#!/usr/bin/python
from pwn import *
from pythonds.basic.stack import Stack

HOST = "2018shell2.picoctf.com"
PORT = 8672

def parChecker(symbolString):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol == "(":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                s.pop()

        index = index + 1

    if balanced and s.isEmpty():
        return True
    else:
        return False


def solve(lhs, rhs) :
    lhs = str(lhs)
    rhs = str(rhs)
    
    pin_point = len(lhs) - 1
    res = lhs + rhs

    while pin_point > -1 :
        cur_idx = pin_point
        while cur_idx < len(res) - 1 and parChecker(res[:cur_idx] + res[cur_idx + 1] + res[cur_idx] + res[cur_idx + 2:]) : 
            if res[cur_idx] == '(' : 
                cur_idx += 1
                continue
            res = res[:cur_idx] + res[cur_idx + 1] + res[cur_idx] + res[cur_idx + 2:]
            cur_idx += 1

        pin_point -= 1

    return res


def main() :
    conn = remote(HOST, PORT)

    while True :
        try :
            prob_input =  conn.readuntil("???").splitlines()[-1].split(" =")[0]
            term_list = prob_input.split(" + ")
            print "input is : "
            print term_list

            cur_idx = 1
            ans = solve(term_list[0], term_list[1])
            while cur_idx < len(term_list) - 1 :
                ans = solve(ans, term_list[cur_idx])
                cur_idx += 1
            print "result is : " + ans

            conn.sendline(ans)
        except :
            break

    conn.interactive()

main()