#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
using namespace std;

struct Info1{
	Info1(){
		value = 0;
	}

	Info1(int arg_value){
		value = arg_value;
	}

	bool operator<(const struct Info1 &arg) const{
		return value < arg.value;
	};

	int value;
};

struct Info2{
	Info2(){
		value = 0;
	}

	Info2(int arg_value){
		value = arg_value;
	}

	bool operator<(const struct Info2 &arg) const{
		return value < arg.value;
	};

	int value;
};



int main(){

	Info1 info1[30];
	Info2 info2[30];

	priority_queue<Info1> Q1;
	priority_queue<Info2> Q2;

	int D,one_num,two_num,index_1,index_2,value;
	scanf("%d",&D);

	scanf("%d",&one_num);

	index_1 = 0;
	char buf[3];

	for(int loop = 0; loop < one_num; loop++){
		scanf("%s %d",buf,&value);

		if(buf[0] == 'D'){
			Q1.push(Info1(value));
		}
	}

	while(!Q1.empty()){
		info1[index_1++].value = Q1.top().value;
		Q1.pop();
	}

	scanf("%d",&two_num);

	index_2 = 0;

	for(int loop = 0; loop < two_num; loop++){
		scanf("%s %d",buf,&value);

		if(buf[0] == 'D' && buf[1]== 'D'){
			Q2.push(Info2(value));
		}
	}

	while(!Q2.empty()){
		info2[index_2++].value = Q2.top().value;
		Q2.pop();
	}

	int limit_1 = min(D,index_1),limit_2 = min(D/2,index_2),maximum = 0,tmp_sum;

	for(int i = 0; i <= limit_1; i++){ //?§£????¬?1???????????¨?????°
		for(int k = 0; k <= limit_2; k++){ //?§£????¬?2???????????¨?????°
			if(i + 2*k > D)break;

			tmp_sum = 0;
			for(int p = 0; p < i; p++)tmp_sum += info1[p].value;
			for(int p = 0; p < k; p++)tmp_sum += info2[p].value;
			maximum = max(maximum,tmp_sum);
		}
	}

	printf("%d\n",maximum);

	return 0;
}