#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int>> C;
for (int i = 0; i < n; i++) {
int row, col;
cin >> row >> col;
C.push_back(make_pair(row, col));
}
stack<pair<int, int>> ch;
stack<char> op;
char c;
int sum = 0;
while (cin >> c) {
if (c == '(') {
op.push(c);
}
else if (c >= 'A' && c <= 'Z') {
ch.push(C[c - 'A']);
}
else {
auto i = ch.top();
ch.pop();
auto j = ch.top();
ch.pop();
sum += j.first * j.second * i.second;
ch.push(make_pair(j.first, i.second));
op.pop();
}
}
cout << sum << endl;
return 0;
}