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>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 100005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 + c - '0';
	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
struct node {
    int to,next,cap;int64 val;
}E[1000005];
int sumE,head[505],a[85],b[85],x[85],y[85],N,M;
int L[2][85],R[2][85],S,T,f;
int64 val[85],res,ans,ex;
bool vis[505];
char s[330][2];
void add(int u,int v,int c,int64 a) {
    E[++sumE].to = v;
    E[sumE].next = head[u];
    E[sumE].cap = c;
    E[sumE].val = a;
    head[u] = sumE;
}
void addtwo(int u,int v,int c,int64 a) {
    add(u,v,c,a);
    add(v,u,0,-a);
}
void build(int K) {
    sumE = 1;memset(head,0,sizeof(head));
    for(int i = 1 ; i <= N ; ++i) L[0][i] = L[1][i] = 1,R[0][i] = R[1][i] = 100;
    for(int i = 1 ; i <= M ; ++i) {
        if(b[i] >= K) continue;
        if(s[i][0] == 'L') {
            L[0][b[i] + 1] = max(L[0][b[i] + 1],a[i] + 1);
        }
        else if(s[i][0] == 'R') {
            R[0][K - b[i]] = min(R[0][K - b[i]],a[i] - 1);
        }
        else if(s[i][0] == 'D') {
            L[1][b[i] + 1] = max(L[1][b[i] + 1],a[i] + 1);
        }
        else if(s[i][0] == 'U') {
            R[1][K - b[i]] = min(R[1][K - b[i]],a[i] - 1);
        }
    }
    for(int i = 2 ; i <= K ; ++i) {
    	L[0][i] = max(L[0][i - 1],L[0][i]);
    	L[1][i] = max(L[1][i - 1],L[1][i]);
    }
    for(int i = K - 1 ; i >= 1 ; --i) {
    	R[0][i] = min(R[0][i + 1],R[0][i]);
    	R[1][i] = min(R[1][i + 1],R[1][i]);
    }
    S = 1;T = 2 * K + 2 * N + 2;
    for(int i = 1 ; i <= K ; ++i) {
    	addtwo(S,i + 1,1,0);
    	addtwo(i + K + 2 * N + 1,T,1,0);
    	for(int j = 1 ; j <= N ; ++j) {
    	    if(x[j] >= L[0][i] && x[j] <= R[0][i]) {
        		addtwo(i + 1,K + j + 1,1,0);
    	    }
    	    if(y[j] >= L[1][i] && y[j] <= R[1][i]) {
        		addtwo(K + N + j + 1,i + K + 2 * N + 1,1,0);
    	    }
    	}
    }
    for(int i = 1 ; i <= N ; ++i) {
        addtwo(K + i + 1,K + N + i + 1,1,val[i]);
    }

}
int maxflow(int u,int aug) {
    if(u == T) {
    	res += ex * aug;
        f += aug;
    	return aug;
    }
    int flow = 0;
    vis[u] = 1;
    for(int i = head[u] ; i ; i = E[i].next) {
        int v = E[i].to;
    	if(E[i].cap > 0 && !vis[v] && E[i].val == 0) {
    	    int t = maxflow(v,min(E[i].cap,aug - flow));
    	    flow += t;
    	    E[i].cap -= t;
    	    E[i ^ 1].cap += t;
    	}
    }
    return flow;
}
bool modlabel() {
    int64 p = -1e18;
    for(int u = S ; u <= T ; ++u) {
    	if(vis[u]) {
    	    for(int i = head[u] ; i ; i = E[i].next) {
        		int v = E[i].to;
        		if(!vis[v] && E[i].cap > 0 && E[i].val > p) p = E[i].val;
    	    }
    	}
    }
    if(p == -1e18) return false;
    ex += p;
    for(int u = S ; u <= T ; ++u) {
    	if(vis[u]) {
    	    for(int i = head[u] ; i ; i = E[i].next) {
        		E[i].val -= p;
        		E[i ^ 1].val += p;
    	    }
    	}
    }
    return true;
}
void Init() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
    	read(x[i]);read(y[i]);read(val[i]);
    }
    read(M);
    for(int i = 1 ; i <= M ; ++i) {
    	scanf("%s",s[i]);read(a[i]);read(b[i]);
    }
}
void Solve() {
    ans = 0;
    for(int i = 1 ; i <= N ; ++i) {
    	build(i);
    	res = 0;ex = 0;
        f = 0;
    	do {
    	    do {
                memset(vis,0,sizeof(vis));
    	    }while(maxflow(1,i));
    	}while(modlabel());
        if(f == i) ans = max(ans,res);
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Init();
    Solve();
}