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: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=505,M=50005,R=100;
const ll inf=0x3f3f3f3f3f3f3f3fll;
int read(){
	int f=1,g=0;
	char ch=getchar();
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
	return f*g;
}
void chkmax(int &x,int y){if (x<y) x=y;}
void chkmin(int &x,int y){if (x>y) x=y;}
int n,m;
struct jew{int x,y;ll w;}a[N];
struct node{char typ;int x,r;}b[N];
ll cost,ans;
int S,T,cnt,fir[N],fr[N],siz;
struct edge{int u,v;ll w;int fl,nex;}e[M];
void add(int u,int v,ll w,int fl){
	e[++siz]=(edge){u,v,w,fl,fir[u]};fir[u]=siz;
	e[++siz]=(edge){v,u,-w,0,fir[v]};fir[v]=siz;
}
ll dist[N];int que[N*50];bool inq[N];
void dec(int x,int fl){e[x].fl-=fl;e[x^1].fl+=fl;}
bool spfa(){
	memset(dist,0xc0,8*(cnt+1));
	memset(inq,0,cnt+1);
	int l=1,r=1;que[l]=S;dist[S]=0;inq[S]=1;
	while (l<=r){
		int x=que[l];
		for (int i=fir[x];i;i=e[i].nex)
		if ((e[i].fl)&&(dist[e[i].v]<dist[x]+e[i].w)){
			if (!inq[e[i].v]) {que[++r]=e[i].v;inq[e[i].v]=1;}
			dist[e[i].v]=dist[x]+e[i].w;fr[e[i].v]=i;
		}
		inq[x]=0;l++;
	}
	return dist[T]>(ll)0xc0c0c0c0c0c0c0c0ll;
}
int update(){
	int t=0x3f3f3f3f;
	for (int i=fr[T];i;i=fr[e[i].u]) t=min(t,e[i].fl);
	cost+=t*dist[T];
	for (int i=fr[T];i;i=fr[e[i].u]) dec(i,t);
	return t;
}
int dinic(){
	int flow=0;
	while (spfa()) flow+=update();
	return flow;
}
int ml[N],mr[N],mu[N],md[N];
void calc(int L){
	memset(fir,0,sizeof(fir));siz=1;
	S=0;T=cnt=L*2+R*2+1;
	for (int i=1;i<=n;i++) add(a[i].x,a[i].y+R,a[i].w,1);
	for (int i=1;i<=L;i++) {ml[i]=mu[i]=1,mr[i]=md[i]=R;add(S,R*2+i,0,1);add(R*2+L+i,T,0,1);}
	for (int i=1;i<=m;i++)
	if (b[i].r>=L) continue;
	else if (b[i].typ=='L') chkmax(ml[b[i].r+1],b[i].x+1);
	else if (b[i].typ=='R') chkmin(mr[L-b[i].r],b[i].x-1);
	else if (b[i].typ=='D') chkmax(mu[b[i].r+1],b[i].x+1);
	else if (b[i].typ=='U') chkmin(md[L-b[i].r],b[i].x-1);
	for (int i=2;i<=L;i++) chkmax(ml[i],ml[i-1]),chkmax(mu[i],mu[i-1]);
	for (int i=L-1;i;i--)  chkmin(md[i],md[i+1]),chkmin(md[i],md[i+1]);
	for (int i=1;i<=L;i++){
		for (int j=ml[i];j<=mr[i];j++) add(R*2+i,j,0,1);
		for (int j=mu[i];j<=md[i];j++) add(j+R,R*2+L+i,0,1);
	}
	cost=0;
	if (dinic()==L) ans=max(ans,cost);
//	for (int i=1;i<=n;i++) if (!e[i*2].fl) printf("%d ",i);
//	printf("\n");
}
int main(){
	n=read();
	for (int i=1;i<=n;i++) scanf(" %d %d %lld",&a[i].x,&a[i].y,&a[i].w);
	m=read();
	for (int i=1;i<=m;i++) scanf(" %c %d %d",&b[i].typ,&b[i].x,&b[i].r);
	for (int i=1;i<=n;i++) calc(i);
	printf("%lld\n",ans);
	return 0;
}