n = int(input())
a = [0] * n
b = [0] * n

for i in range(n):
    a[i], b[i] = map(int, input().split(" "))

s = input()

ans = 0
stack = []
for ch in s:
    if ch == ')':
        b1, b2 = stack.pop()
        a1, a2 = stack.pop()
        ans += a1 * a2 * b2
        stack.append([a1, b2])
    elif ch != '(':
        idx = ord(ch) - ord('A')
        stack.append([a[idx], b[idx]])
print(ans)