#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <string>

int main() {
  size_t D;
  scanf("%zu", &D);

  size_t x;
  scanf("%zu", &x);

  std::vector<int> one;
  for (size_t i = 0; i < x; ++i) {
    char a;
    int p;
    scanf(" %c %d", &a, &p);
    if (a != 'D') continue;
    one.push_back(p);
  }

  size_t y;
  scanf("%zu", &y);
  std::vector<int> two;
  for (size_t i = 0; i < y; ++i) {
    char b, c;
    int q;
    scanf(" %c%c %d", &b, &c, &q);
    if (b != 'D' || c != 'D') continue;
    two.push_back(q);
  }

  std::sort(one.rbegin(), one.rend());
  std::sort(two.rbegin(), two.rend());

  one.resize(std::max(x, D), 0);
  two.resize(std::max(y, D), 0);

  int res = 0;
  for (size_t i = 0; i <= D; ++i) {
    int f1 = std::accumulate(one.begin(), one.begin()+i, 0);
    int f2 = std::accumulate(two.begin(), two.begin()+((D-i)/2), 0);
    res = std::max(res, f1+f2);
  }
  printf("%d\n", res);
}

