#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define FOR(i, m, n) for(int i=(m); i<(n); ++i)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define Cout(x) cout << (x) << endl
#define Cout2(x, y) cout << (x) << " " << (y) << endl
#define dump(x) cerr << #x << " = " << (x) << endl;
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
const int inf = (int)1e9;
const double pi = acos(-1.0);

int main() {
	int d; cin >> d;
	int x, y;
	VI p1, p2;
	cin >> x;
	rep(i, x) {
		string A; int P;
		cin >> A >> P;
		if (A == "D")p1.pb(P);
	}
	cin >> y;
	rep(i, y) {
		string A; int P;
		cin >> A >> P;
		if (A == "DD")p2.pb(P);
	}

	sort(all(p1), greater<int>());
	sort(all(p2), greater<int>());

	int ans = 0;
	for (int bit = 0; bit < (1 << sz(p2)); bit++) {
		int cnt = 0, sum = 0;

		rep(i, sz(p2)) {
			if (bit & (1 << i)) {
				cnt += 2;
				sum += p2[i];
			}
		}

		if (cnt > d)continue;
		rep(i, min(sz(p1), d - cnt))sum += p1[i];
		ans = max(ans, sum);
	}
	Cout(ans);
}
