Python 代码实现:

while True:
    try:
        alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        num = 0
        n = int(input())
        dict_a = {}
        for  i in range(n):
            dict_a[alpha[i]] = list(map(int,input().split()))
        rules = input()
        stack = []
        for j in rules:
            if j != ')':
                stack.append(j)
            else:
                C,B = stack.pop(),stack.pop()
                stack.pop()
                num += dict_a[B][0] * dict_a[B][1] * dict_a[C][1]
                dict_a[B] = [dict_a[B][0],dict_a[C][1]]
                stack.append(B)
        print(num)
    except:
        break