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 MV = 815;
const int ME = 1200006;
const ll oo = 1e18;

template <typename T> void cmin(T &x, const T &y)
{
	if(y < x) x = y;
}

template <typename T> void cmax(T &x, const T &y)
{
	if(y > x) x = y;
}

template <typename T> void read(T &x)
{
	x = 0; char c = getchar(); bool f = 0;
	while(!isdigit(c) && c!='-') c = getchar();
	if(c == '-') f = 1, c = getchar();
	while(isdigit(c)) x = x*10+c-'0', c = getchar();
	if(f) x = -x;
}

struct edge
{
	int u, v, f;
	ll c;
	int n;

	edge (const int &u0 = 0, const int &v0 = 0, const ll &f0 = 0, const ll &c0 = 0, const int &n0 = 0) : u(u0), v(v0), f(f0), c(c0), n(n0) {}
};

struct GRAPH
{
	edge e[ME];
	int fst[MV], lnum;

	void init() {memset(fst, 0xff, sizeof(fst)); lnum = -1;}
	
	GRAPH () {init();}
	
	void addeg(int nu, int nv, int nc, int nf)
	{
		e[++lnum] = edge(nu, nv, nf, nc, fst[nu]), fst[nu] = lnum;
		e[++lnum] = edge(nv, nu, 0, -nc, fst[nv]), fst[nv] = lnum;
	}
	
	int que[MV], inq[MV], cur[MV];
	ll dis[MV];
	
	bool spfa(int frm, int tar)
	{
		int h = 1, t = 1, x, y;
		memset(dis, 0x9f, sizeof(dis));
		dis[frm] = 0;
		que[h] = frm;
		inq[frm] = 1;
		while(h >= t)
		{
			x = que[(t++)%MV];
			inq[x] = 0;
			for(int i=fst[x]; ~i; i=e[i].n)
			{
				y = e[i].v;
				if(e[i].f && dis[y]<dis[x]+e[i].c)
				{
					dis[y] = dis[x] + e[i].c;
					if(!inq[y]) que[(++h)%MV] = y, inq[y] = 1;
				}
			}
		}
		return (dis[tar] > -oo);
	}
	
	int dinic(int x, int t, int f)
	{
		if(x == t) return f;
		int a, y, now = 0;
		inq[x] = 1;
		for(int &i=cur[x]; ~i; i=e[i].n)
		{
			y = e[i].v;
			if(dis[y]==dis[x]+e[i].c && e[i].f && !inq[y])
			{
				a = dinic(y, t, min(e[i].f, f-now));
				now += a;
				e[i].f -= a;
				e[i^1].f += a;
				if(now == f) break;
			}
		}
		inq[x] = 0;
		return now;
	}
	
	pair<int, ll> costf(int s, int t)
	{
		ll cost = 0;
		int flow = 0;
		while(spfa(s, t))
		{
			memmove(cur, fst, sizeof(cur));
			int new_flow = dinic(s, t, ME);
			cost += dis[t] * new_flow;
			flow += new_flow;
		}
		return make_pair(flow, cost);
	}
} G;

int n, m;
int lx[MV], rx[MV];
int ly[MV], ry[MV];
int px[MV], py[MV];
ll pv[MV];
char qc[MV];
int qa[MV], qb[MV];
int global_s, global_t;

void input()
{
	read(n);
	for(int i=1; i<=n; i++) read(px[i]), read(py[i]), read(pv[i]);
	read(m);
	for(int i=1; i<=n; i++) lx[i] = 1, rx[i] = 100, ly[i] = 1, ry[i] = 100;
	for(int i=1; i<=m; i++)
	{
		qc[i] = getchar();
		while(!isalpha(qc[i])) qc[i] = getchar();
		read(qa[i]), read(qb[i]);
	}
}

void build(int num)
{
	for(int i=1; i<=num; i++) lx[i] = ly[i] = 1, rx[i] = ry[i] = 100;
	for(int i=1; i<=m; i++)
	{
		if(qb[i] > num) continue;
		if(qc[i] == 'L') cmax(lx[qb[i]+1], qa[i]+1);
		if(qc[i] == 'R') cmin(rx[num-qb[i]], qa[i]-1);
		if(qc[i] == 'D') cmax(ly[qb[i]+1], qa[i]+1);
		if(qc[i] == 'U') cmin(ry[num-qb[i]], qa[i]-1);
	}
	for(int i=1; i<=num; i++) cmax(lx[i], lx[i-1]), cmax(ly[i], ly[i-1]);
	for(int i=num; i>=1; i--) cmin(rx[i], rx[i+1]), cmin(ry[i], ry[i+1]);
	G.init();
	for(int i=1; i<=num; i++)
	{
		for(int j=1; j<=n; j++)
		{
			if(lx[i]<=px[j] && px[j]<=rx[i]) G.addeg(i, num*2+j, 0, 1);
			if(ly[i]<=py[j] && py[j]<=ry[i]) G.addeg(num*2+n+j, num+i, 0, 1);
		}
	}
	for(int i=1; i<=n; i++) G.addeg(num*2+i, num*2+n+i, pv[i], 1);
	global_s = num*2+n*2+1, global_t = global_s+1;
	for(int i=1; i<=num; i++) G.addeg(global_s, i, 0, 1), G.addeg(num+i, global_t, 0, 1);
}

void work()
{
	ll ans = 0;
	for(int i=1; i<=n; i++)
	{
		build(i);
		auto ret = G.costf(global_s, global_t);
		if(ret.first != i) break;
		else cmax(ans, ret.second);
	}
	printf("%lld\n", ans);
}

int main()
{
	input();
	work();
	return 0;
}