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 <bits/stdc++.h>
using namespace std;
#define N 2010
#define M 200010
#define int long long
#define INF 4611686018427387904LL
inline int read() {
    int x=0;
    char ch=getchar();
    while (!isdigit(ch)) ch=getchar();
    while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x;
}
struct Point {
    int x,y,val;
}p[N];
struct opt {
    char ch; int x,y;
}a[N];
int tot=1;
int fir[N],nex[M],got[M],tak[M],cst[M];
inline void AddEdge(int x,int y,int z,int w) { 
    nex[++tot]=fir[x],fir[x]=tot,got[tot]=y,tak[tot]=z,cst[tot]=w;
}
inline void Add(int x,int y,int z,int w) {
    AddEdge(x,y,z,w),AddEdge(y,x,0,-w);
}
int xl[N],xr[N],yl[N],yr[N];
int vis[N],dis[N],pre[N],que[N];
inline int spfa(int s,int t) {
    int l=1,r=1;
    for (int i=1;i<=t;i++) vis[i]=false,dis[i]=-INF,pre[i]=0;
    que[1]=s,dis[s]=0;
    while (l<=r) {
        int x=que[l];
        for (int i=fir[x];i;i=nex[i]) {
            int y=got[i],w=cst[i];
            if (!tak[i] || dis[x]+w<=dis[y]) continue;
            dis[y]=dis[x]+w,pre[y]=i;
            if (!vis[y]) vis[y]=true,que[++r]=y;
        }
        ++l,vis[x]=false;
    }
    return dis[t]!=-INF;
}
inline int getflow(int s,int t) {
    int flow=INF;
    for (int i=t;i!=s;i=got[pre[i]^1]) {
        flow=min(flow,tak[pre[i]]);
    }
    return flow;
}
inline void update(int s,int t,int flow,int &Maxflow,int &Mincost) {
    Maxflow+=flow,Mincost+=flow*dis[t];
    for (int i=t;i!=s;i=got[pre[i]^1]) {
        tak[pre[i]]-=flow,tak[pre[i]^1]+=flow;
    }
}
inline int solve(int s,int t) {
    int Maxflow=0,Mincost=0;
    while (spfa(s,t)) {
        update(s,t,getflow(s,t),Maxflow,Mincost);
    }
    return Mincost;
}
signed main() {
    int n=read();
    for (int i=1;i<=n;i++)
        p[i].x=read(),p[i].y=read(),p[i].val=read();
    int m=read(),res=0;
    for (int i=1;i<=m;i++) {
        char ch=getchar();
        while (ch!='U' && ch!='D' && ch!='L' && ch!='R') ch=getchar();
        a[i].ch=ch,a[i].x=read(),a[i].y=read();
    }
    for (int k=1;k<=n;k++) {
        tot=1,memset(fir,0,sizeof(fir));
        int s=(k<<1)+(n<<1)+1,t=s+1;
        for (int i=1;i<=k;i++) Add(s,i,1,0);
        for (int i=k+2*n+1;i<=2*k+2*n;i++) Add(i,t,1,0);
        for (int i=k+1;i<=k+n;i++) Add(i,i+n,1,p[i-k].val);
        for (int i=1;i<=k;i++) xl[i]=yl[i]=0,xr[i]=yr[i]=INF;
        for (int i=1;i<=m;i++) {
            if (a[i].ch=='U') for (int j=1;j<=k-a[i].y;j++) yr[j]=min(yr[j],a[i].x-1);
            if (a[i].ch=='D') for (int j=a[i].y+1;j<=k;j++) yl[j]=max(yl[j],a[i].x+1);
            if (a[i].ch=='L') for (int j=a[i].y+1;j<=k;j++) xl[j]=max(xl[j],a[i].x+1);
            if (a[i].ch=='R') for (int j=1;j<=k-a[i].y;j++) xr[j]=min(xr[j],a[i].x-1);
        }
        for (int i=1;i<=k;i++) for (int j=1;j<=n;j++)
            if (xl[i]<=p[j].x && p[j].x<=xr[i]) Add(i,j+k,1,0);
        for (int i=1;i<=k;i++) for (int j=1;j<=n;j++)
            if (yl[i]<=p[j].y && p[j].y<=yr[i]) Add(j+k+n,i+k+2*n,1,0);
        res=max(res,solve(s,t));
    }
    cout<<res<<endl;
    return 0;
}