import sys #矩阵相乘的乘法数,和结果矩阵 def com_count(listA, listB): count = listA[0] * listA[1] * listB[1] listC = [listA[0], listB[1]] return count, listC while True: try: n = int(input()) #先将矩阵保存 mn_list = [] for i in range(n): m, n = map(int,input().split()) mn_list.append([m, n]) gz = input() count = 0 stack = [] #使用栈处理 for c in gz: if c == '(': continue elif c == ')':#对于')',连续出栈两个矩阵,计算出乘法次数和结果矩阵 if len(stack) <= 1: break addc, m = com_count(stack.pop(-2), stack.pop()) # print(addc, m) stack.append(m) count += addc else: stack.append(mn_list.pop(0)) #碰到字母就压栈 print(count) except: # print(sys.exc_info()) break # import sys # #矩阵相乘的乘法数,和结果矩阵 # def com_count(listA, listB): # count = listA[0] * listA[1] * listB[1] # listC = [listA[0], listB[1]] # return count, listC # while True: # try: # n = int(input()) # #按大写字母保存对应的矩阵 # mn_dic = {} # for i in range(n): # m, n = map(int,input().split()) # mn_dic[chr(i + 65)] = [m, n] # gz_list = list(input()) # # print(mn_dic) # count = 0 # i = 0 # while i < len(gz_list) - 1 and len(gz_list) >= 3: # #连续字母,直接相乘,把结果矩阵对应在左边字母 # if gz_list[i].isalpha() and gz_list[i + 1].isalpha(): # addc, mn_dic[gz_list[i]] = com_count(mn_dic[gz_list[i]], mn_dic[gz_list[i + 1]]) # count += addc # #删掉右边字母 # del gz_list[i + 1] # #如果左右都是括号,也删掉括号 # if gz_list[i - 1] == '(' and gz_list[i + 1] == ')': # del gz_list[i + 1] # del gz_list[i - 1] # i -= 2 # else: # i += 1 # print(count) # except: # # print(sys.exc_info()) # break