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;
#define gc getchar()
#define pc putchar
#define li long long
inline li read(){
	li x = 0,y = 0,c = gc;
	while(!isdigit(c)) y = c,c = gc;
	while(isdigit(c)) x = (x << 1) + (x << 3) + (c ^ '0'),c = gc;
	return y == '-' ? -x : x;
}
inline void print(li q){
	if(q < 0) pc('-'),q = -q;
	if(q >= 10) print(q / 10);
	pc(q % 10 + '0');
}
#define ldb long double
ldb p[510][510];
int n,m,cnt;
li x[510],y[510],a[510];
int main(){
	int i,j,k,l,u,v;
	n = read();for(i = 1;i <= n;++i) x[i] = read(),y[i] = read(),a[i] = read();cnt = n;
	m = read();
	char c;
	for(i = 1;i <= m;++i){
		c = gc;while(c < 'A' || c > 'Z') c = gc;
		u = read();v = read();
		p[i][0] = v;
		if(c == 'U'){
			for(j = 1;j <= n;++j) if(y[j] >= u) p[i][j] = 1;
		} 
		else if(c == 'D'){
			for(j = 1;j <= n;++j) if(y[j] <= u) p[i][j] = 1;
		} 
		else if(c == 'L'){
			for(j = 1;j <= n;++j) if(x[j] <= u) p[i][j] = 1;
		} 
		else{
			for(j = 1;j <= n;++j) if(x[j] >= u) p[i][j] = 1;
		} 
		p[i][++cnt] = 1;
	}
	for(i = 1;i <= n;++i) p[i + m][i] = p[i + m][++cnt] = p[i + m][0] = 1;
	for(i = 1;i <= n;++i) p[0][i] = a[i];
	/*for(i = 0;i <= n + m;++i){
		for(j = 0;j <= cnt;++j) cerr<<p[i][j]<<" ";cerr<<endl;
	}
	cerr<<endl;*/
	while(1){
		int fg = 0;
		for(i = 1;i <= cnt;++i) if(p[0][i] > 0 && (!fg || p[0][i] > p[0][fg])) fg = i;
		if(!fg) break;
		int tp = 0;
		for(i = 1;i <= n + m;++i) if(p[i][fg] > 0 && (!tp || p[i][0] / p[i][fg] < p[tp][0] / p[tp][fg])) tp = i;
		if(!tp) break;
		for(i = 0;i <= cnt;++i) if(i != fg) p[tp][i] /= p[tp][fg];p[tp][fg] = 1;
		for(i = 0;i <= n + m;++i) if(i != tp){
			for(j = 0;j <= cnt;++j) if(j != fg) p[i][j] -= p[i][fg] * p[tp][j];p[i][fg] = 0;
		} 
		/*cerr<<fg<<"*"<<tp<<endl;
		for(i = 0;i <= n + m;++i){
			if(i == 14) cerr<<"*";
			for(j = 0;j <= cnt;++j) cerr<<p[i][j]<<" ";cerr<<endl;
		}
		cerr<<endl;
		system("pause");*/
	}
	print((li)(-p[0][0] + 0.5l));
	return 0;
}