import collections
import re


def token(formula):
    for match in re.finditer(r'[A-Z][a-z]*|\(|\)|[0-9]+', formula):
        token = match.group()
        if token.isnumeric():
            type = 'NUMBER'
            token = int(token)
        elif token in '()':
            type = 'PARENTHESES'
        else:
            type = 'ATOM'
        yield token, type


def calc(tokens):
    stack = [collections.defaultdict(int)]
    for token, type in tokens:
        current = stack[-1]
        if type == 'ATOM':
            last = token
            current[token] += 1
        elif type == 'NUMBER':
            if isinstance(last, str):
                current[last] += token - 1
            else:
                for element, count in last.items():
                    current[element] += count * token
        elif type == 'PARENTHESES':
            if token == '(':
                stack.append(collections.defaultdict(int))
            elif token == ')':
                last = stack.pop()
    assert len(stack) == 1
    return stack[0]


def format(counter):

    def format_atom(atom, count):
        if count > 1:
            return f'{atom}{count}'
        else:
            return atom

    return ''.join(
        format_atom(atom, counter[atom])
        for atom in sorted(counter.keys())
    )


if __name__ == '__main__':
    print(format(calc(token(
        "(((U42Se42Fe10Mc31Rh49Pu49Sb49)49V39Tm50Zr44Og6)33((W2Ga48Tm14Eu46Mt1"
        "2)23(RuRnMn11)7(Yb15Lu34Ra19CuTb2)47(Md38BhCu48Db15Hf12Ir40)7CdNi21(D"
        "b40Zr24Tc27SrBk46Es41DsI37Np9Lu16)46(Zn49Ho19RhClF9Tb30SiCuYb16)15)37"
        "(Cr48(Ni31)25(La8Ti17Rn6Ce35)36(Sg42Ts32Ca)37Tl6Nb47Rh32NdGa18Cm10Pt4"
        "9(Ar37RuSb30Cm32Rf28B39Re7F36In19Zn50)46)38(Rh19Md23No22PoTl35Pd35Hg)"
        "41)50")
    )))

import collections
import re


def token(formula):
    for match in re.finditer(r'[A-Z][a-z]*|\(|\)|[0-9]+', formula):
        token = match.group()
        if token.isnumeric():
            type = 'NUMBER'
            token = int(token)
        elif token in '()':
            type = 'PARENTHESES'
        else:
            type = 'ATOM'
        yield token, type


def calc(tokens):
    stack = [collections.defaultdict(int)]
    for token, type in tokens:
        current = stack[-1]
        if type == 'ATOM':
            last = token
            current[token] += 1
        elif type == 'NUMBER':
            if isinstance(last, str):
                current[last] += token - 1
            else:
                for element, count in last.items():
                    current[element] += count * token
        elif type == 'PARENTHESES':
            if token == '(':
                stack.append(collections.defaultdict(int))
            elif token == ')':
                last = stack.pop()
    assert len(stack) == 1
    return stack[0]


def format(counter):

    def format_atom(atom, count):
        if count > 1:
            return f'{atom}{count}'
        else:
            return atom

    return ''.join(
        format_atom(atom, counter[atom])
        for atom in sorted(counter.keys())
    )


if __name__ == '__main__':
    print(format(calc(token(
        "(((U42Se42Fe10Mc31Rh49Pu49Sb49)49V39Tm50Zr44Og6)33((W2Ga48Tm14Eu46Mt1"
        "2)23(RuRnMn11)7(Yb15Lu34Ra19CuTb2)47(Md38BhCu48Db15Hf12Ir40)7CdNi21(D"
        "b40Zr24Tc27SrBk46Es41DsI37Np9Lu16)46(Zn49Ho19RhClF9Tb30SiCuYb16)15)37"
        "(Cr48(Ni31)25(La8Ti17Rn6Ce35)36(Sg42Ts32Ca)37Tl6Nb47Rh32NdGa18Cm10Pt4"
        "9(Ar37RuSb30Cm32Rf28B39Re7F36In19Zn50)46)38(Rh19Md23No22PoTl35Pd35Hg)"
        "41)50")
    )))
