#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n, m;
int p[111111], tt[22];
pair<int, int> dp[1 << 20][20], ori;
bool vis[1 << 20][20];
char ty[22], s[22];
int cal(pair<int, int> x) { return x.first - x.second; }
pair<int, int> dfs(int x, int y) {
  if (vis[x][y]) return dp[x][y];
  vis[x][y] = true;
  pair<int, int> tem, re = make_pair(-1, -1);
  if (!x) return make_pair(0, 0);
  if (s[y] == 'b') {
    for (int i = 0; i < (int)(m); ++i)
      if ((1 << i) & x) {
        tem = dfs(x ^ (1 << i), y + 1);
        if (re.first < 0)
          re = tem;
        else {
          if (cal(re) < cal(tem)) re = tem;
        }
      }
  } else if (s[y] == 'd') {
    for (int i = 0; i < (int)(m); ++i)
      if ((1 << i) & x) {
        tem = dfs(x ^ (1 << i), y + 1);
        if (re.second < 0)
          re = tem;
        else {
          if (cal(re) > cal(tem)) re = tem;
        }
      }
  } else if (s[y] == 'p') {
    for (int i = 0; i < (int)(m); ++i)
      if ((1 << i) & x) {
        tem = dfs(x ^ (1 << i), y + 1);
        if (re.first < 0)
          re = make_pair(tem.first + p[i], tem.second);
        else {
          if (cal(make_pair(re.first + p[i], re.second)) <
              cal(make_pair(tem.first + p[i], tem.second)))
            re = make_pair(tem.first + p[i], tem.second);
        }
      }
  } else {
    for (int i = 0; i < (int)(m); ++i)
      if ((1 << i) & x) {
        tem = dfs(x ^ (1 << i), y + 1);
        if (re.second < 0)
          re = make_pair(tem.first, tem.second + p[i]);
        else {
          if (cal(make_pair(re.first, re.second + p[i])) >
              cal(make_pair(tem.first, tem.second + p[i])))
            re = make_pair(tem.first, tem.second + p[i]);
        }
      }
  }
  return dp[x][y] = re;
}
int main() {
  int i, j, k, x, y, z, ca = 1;
  scanf("%d", &(n));
  for (int i = 0; i < (int)(n); ++i) scanf("%d", &(p[i]));
  sort(p, p + n);
  reverse(p, p + n);
  scanf("%d", &(m));
  for (int i = 0; i < (int)(m); ++i) {
    scanf("%s %d", ty, tt + i);
    if (tt[i] == 1 && ty[0] == 'b')
      s[i] = 'b';
    else if (tt[i] == 1 && ty[0] == 'p')
      s[i] = 'p';
    else if (tt[i] == 2 && ty[0] == 'b')
      s[i] = 'd';
    else
      s[i] = 'q';
  }
  pair<int, int> re = dfs((1 << m) - 1, 0);
  printf("%d\n", (re.first - re.second));
  return 0;
}
