Problem:
Score : 1300 points

Problem Statement

A museum exhibits N jewels, Jewel 1, 2, ..., N.
The coordinates of Jewel i are (x_i, y_i) (the museum can be regarded as a two-dimensional plane), and the value of that jewel is v_i.

Snuke the thief will steal some of these jewels.

There are M conditions, Condition 1, 2, ..., M, that must be met when stealing jewels, or he will be caught by the detective.
Each condition has one of the following four forms:

(t_i =L, a_i, b_i) : Snuke can only steal at most b_i jewels whose x coordinates are a_i or smaller.

(t_i =R, a_i, b_i) : Snuke can only steal at most b_i jewels whose x coordinates are a_i or larger.

(t_i =D, a_i, b_i) : Snuke can only steal at most b_i jewels whose y coordinates are a_i or smaller.

(t_i =U, a_i, b_i) : Snuke can only steal at most b_i jewels whose y coordinates are a_i or larger.

Find the maximum sum of values of jewels that Snuke the thief can steal.

Constraints

1 \leq N \leq 80

1 \leq x_i, y_i \leq 100

1 \leq v_i \leq 10^{15}

1 \leq M \leq 320

t_i is L, R, U or D.

1 \leq a_i \leq 100

0 \leq b_i \leq N - 1

(x_i, y_i) are pairwise distinct.

(t_i, a_i) are pairwise distinct.

(t_i, b_i) are pairwise distinct.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 v_1
x_2 y_2 v_2
:
x_N y_N v_N
M
t_1 a_1 b_1
t_2 a_2 b_2
:
t_M a_M b_M

Output

Print the maximum sum of values of jewels that Snuke the thief can steal.

Sample Input 1

7
1 3 6
1 5 9
3 1 8
4 3 8
6 2 9
5 4 11
5 7 10
4
L 3 1
R 2 3
D 5 3
U 4 2

Sample Output 1

36

Stealing Jewel 1, 5, 6 and 7 results in the total value of 36.

Sample Input 2

3
1 2 3
4 5 6
7 8 9
1
L 100 0

Sample Output 2

0

Sample Input 3

4
1 1 10
1 2 11
2 1 12
2 2 13
3
L 8 3
L 9 2
L 10 1

Sample Output 3

13

Sample Input 4

10
66 47 71040136000
65 77 74799603000
80 53 91192869000
24 34 24931901000
91 78 49867703000
68 71 46108236000
46 73 74799603000
56 63 93122668000
32 51 71030136000
51 26 70912345000
21
L 51 1
L 7 0
U 47 4
R 92 0
R 91 1
D 53 2
R 65 3
D 13 0
U 63 3
L 68 3
D 47 1
L 91 5
R 32 4
L 66 2
L 80 4
D 77 4
U 73 1
D 78 5
U 26 5
R 80 2
R 24 5

Sample Output 4

305223377000

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <cstring>
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <list>  
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;  

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=500;
const int MAXGN=4*MAXN+2;
const int MAXGM=5*MAXN;

typedef struct MCMF {
	int n,m;
	vector<int> ghead,gnxt,gto,gcap; vector<ll> gcost;

	vector<ll> d; vector<int> bck; vector<ll> pot;

	//int ghead[MAXGN],gnxt[2*MAXGM],gto[2*MAXGM],gcap[2*MAXGM],gcost[2*MAXGM];
	//int d[MAXGN]; int bck[MAXGN];
	//int pot[MAXGN];

	void init(int _n) { n=_n,m=0; ghead=vector<int>(n,-1); gnxt.clear(),gto.clear(),gcap.clear(),gcost.clear(); d=vector<ll>(n); bck=vector<int>(n); pot=vector<ll>(n,0); }
	void addedge(int a,int b,int c,ll d) {
		gnxt.PB(ghead[a]); ghead[a]=m; gto.PB(b); gcap.PB(c); gcost.PB(+d); ++m;
		gnxt.PB(ghead[b]); ghead[b]=m; gto.PB(a); gcap.PB(0); gcost.PB(-d); ++m;
		//printf("adding edge %d-%d with cap %d and cost %lld\n",a,b,c,d);
	}
	pair<int,ll> solve(int s,int t) {
		//REP(i,n) { printf("%d:",i); for(int x=ghead[i];x!=-1;x=gnxt[x]) printf(" %d",gto[x]); puts(""); }

		REP(i,n) d[i]=LLONG_MAX,bck[i]=-1; d[s]=0,bck[s]=-2;
		while(true) {
			bool change=false;
			REP(at,n) if(d[at]!=LLONG_MAX) for(int x=ghead[at];x!=-1;x=gnxt[x]) if(gcap[x]>0) {
				int to=gto[x]; if(d[at]+gcost[x]>=d[to]) continue;
				d[to]=d[at]+gcost[x]; bck[to]=x^1; change=true;
			}
			if(!change) break;
		}
		if(d[t]==LLONG_MAX) return MP(0,0);
		REP(i,n) pot[i]=d[i];


		pair<int,ll> ret=MP(0,0LL);
		while(true) {
			REP(i,n) d[i]=LLONG_MAX,bck[i]=-1; d[s]=0,bck[s]=-2;
			priority_queue<pair<ll,int> > q; q.push(MP(-d[s],s));
			while(!q.empty()) {
				ll cost=-q.top().first; int at=q.top().second; q.pop();
				if(cost>d[at]) continue;
				for(int x=ghead[at];x!=-1;x=gnxt[x]) if(gcap[x]>0) {
					int to=gto[x]; ll rcost=gcost[x]-pot[at]+pot[to]; if(d[at]+rcost>=d[to]) continue;
					d[to]=d[at]+rcost; bck[to]=x^1; q.push(MP(-d[to],to));
				}
			}
			if(d[t]==LLONG_MAX) break;
			//printf("%d\n",d[t]-pot[t]);
			++ret.first,ret.second+=d[t]-pot[t];
			REP(i,n) if(d[i]!=LLONG_MAX) pot[i]-=d[i];

			for(int at=t;at!=s;at=gto[bck[at]]) {
				//printf("%d ",at);
				++gcap[bck[at]]; --gcap[bck[at]^1];
			}
			//printf("%d ",s);
			//puts("");
		}
		return ret;
	}
} MCMF;
MCMF g;


const int MAXP=80;
const int MAXCOND=320;
struct P { int x,y; ll val; };
struct Cond { int pos,lim; };
bool operator<(const Cond &a,const Cond &b) { if(a.pos!=b.pos) return a.pos<b.pos; return a.lim>b.lim; }

int np;
P p[MAXP];
int ncond;
vector<Cond> lcond,rcond,ucond,dcond;
Cond cond[MAXCOND];

vector<int> lx,hx,ly,hy;

vector<int> calcbound(int want,vector<Cond> cond,bool inv) {
	if(inv) REPSZ(i,cond) cond[i].pos=-cond[i].pos;
	sort(cond.begin(),cond.end());
	int nsz=0; REPSZ(i,cond) { while(nsz>0&&cond[nsz-1].lim>=cond[i].lim) --nsz; cond[nsz++]=cond[i]; } cond.resize(nsz);
	vector<int> ret(want); int at=0; REPSZ(i,ret) { while(at<SZ(cond)&&cond[at].lim<=i) ++at; ret[i]=at==0?INT_MIN:cond[at-1].pos+1; }
	if(inv) { reverse(ret.begin(),ret.end()); REPSZ(i,ret) if(ret[i]==INT_MIN) ret[i]=INT_MAX; else ret[i]=-ret[i]; }
	return ret;
}

ll calc(int want) {
	int sid,tid; vector<int> xcondid(want),ycondid(want),lpid(np),rpid(np);
	int nid=0; sid=nid++; tid=nid++; REP(i,want) xcondid[i]=nid++; REP(i,want) ycondid[i]=nid++; REP(i,np) lpid[i]=nid++,rpid[i]=nid++;
	g.init(nid);
	REP(i,want) g.addedge(sid,xcondid[i],1,0);
	REP(i,want) REP(j,np) if(lx[i]<=p[j].x&&p[j].x<=hx[i]) g.addedge(xcondid[i],lpid[j],1,0);
	REP(j,np) g.addedge(lpid[j],rpid[j],1,-p[j].val);
	REP(j,np) REP(i,want) if(ly[i]<=p[j].y&&p[j].y<=hy[i]) g.addedge(rpid[j],ycondid[i],1,0);
	REP(i,want) g.addedge(ycondid[i],tid,1,0);
	return -g.solve(sid,tid).second;
}

ll solve() {
	ll ret=0;
	FORE(want,1,np) {
		lx=calcbound(want,lcond,false);
		hx=calcbound(want,rcond,true);
		ly=calcbound(want,dcond,false);
		hy=calcbound(want,ucond,true);
		bool ok=true; REP(i,want) if(lx[i]>hx[i]||ly[i]>hy[i]) ok=false; if(!ok) continue;
		//printf("want=%d\n",want);
		//printf("lx:"); REPSZ(i,lx) printf(" %d",lx[i]); puts("");
		//printf("hx:"); REPSZ(i,hx) printf(" %d",hx[i]); puts("");
		//printf("ly:"); REPSZ(i,ly) printf(" %d",ly[i]); puts("");
		//printf("hy:"); REPSZ(i,hy) printf(" %d",hy[i]); puts("");
		ll cur=calc(want);
		//printf("->%lld\n",cur);
		ret=max(ret,cur);
	}
	return ret;
}

void run() {
	scanf("%d",&np);
	REP(i,np) scanf("%d%d%lld",&p[i].x,&p[i].y,&p[i].val);
	scanf("%d",&ncond);
	lcond.clear(),rcond.clear(),ucond.clear(),dcond.clear(); REP(i,ncond) { char kind; scanf(" %c",&kind); Cond cond; scanf(" %d%d",&cond.pos,&cond.lim); if(kind=='L') lcond.PB(cond); else if(kind=='R') rcond.PB(cond); else if(kind=='D') dcond.PB(cond); else if(kind=='U') ucond.PB(cond); else assert(false); }
	printf("%lld\n",solve());
}


int main() {
	run();
	return 0;
}