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: Runtime Error)

#include<algorithm>/*{{{*/
#include<cctype>
#include<cassert>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
#define fi first
#define se second
#define pb push_back
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define ROF(i,a,b) for(int i=(a);i>=(b);--i)
namespace RA{
    int r(int p){return 1ll*rand()*rand()%p;}
    int r(int L,int R){return r(R-L+1)+L;}
}/*}}}*/
/******************heading******************/
typedef long long LL;
const LL LLINF=0x3f3f3f3f3f3f3f3f;
const int N=330,INF=0x3f3f3f3f;

int s,t;
struct qxx{int nex,t,v;LL c;};
qxx e[N*N*4];
LL h[N],hh[N],le=1;
void add_path(int f,int t,int v,LL c){e[++le]=(qxx){h[f],t,v,c},h[f]=le;}
void add_flow(int f,int t,int v,LL c){
    //printf("add_flow(%d,%d,%d,%lld)\n",f,t,v,c);
    add_path(f,t,v,c),add_path(t,f,0,-c);}
#define FORe(i,_u,_v,_w,_c)     for(LL i=h[_u],_v,_w,_c;_v=e[i].t,_w=e[i].v,_c=e[i].c,i;i=e[i].nex)
#define FORflowe(i,_u,_v,_w,_c) for(LL &i=hh[_u],_v,_w,_c;_v=e[i].t,_w=e[i].v,_c=e[i].c,i;i=e[i].nex)

int n,m;
int x[N],y[N];
LL val[N],ans=-LLINF;
int xL[N],xR[N],yL[N],yR[N];

vector<pii> L,R,U,D;

queue<int> q;
bool vising[N];
LL d[N];
bool spfa(){
    memset(d,0x3f,sizeof(d));
    q.push(s),d[s]=0;
    while(!q.empty()){
        int u=q.front();q.pop();
        vising[u]=0;
        FORe(i,u,v,w,c){
            if(!w||d[v]<=d[u]+c)continue;
            d[v]=d[u]+c;
            if(!vising[v])q.push(v),vising[v]=1;
        }
    }
    return d[t]!=LLINF;
}
LL mincost;
bool vis[N];
int dfs(int u,int flow){
    //printf("dfs(%d,%d)\n",u,flow);
    vis[u]=1;
    if(u==t)return flow;
    int rest=flow;
    FORflowe(i,u,v,w,c){
        if(!rest)break;
        if(vis[v]&&v!=t||!w||d[v]!=d[u]+c)continue;
        int k=dfs(v,min(w,1ll*rest));
        if(k)mincost+=k*c,e[i].v-=k,e[i^1].v+=k,rest-=k;
        else d[v]=0;
    }
    return flow-rest;
}
LL go(){
    int maxflow=0;
    while(spfa()){
        //puts("GG");
        memcpy(hh,h,sizeof(h));
        for(int i;memset(vis,0,sizeof(vis)),(i=dfs(s,INF))&&vis[t];)maxflow+=i;
    }
    //printf("maxflow=%d,mincost=%lld\n",maxflow,mincost);
    return mincost;
}
void init(){
    memset(h,0,sizeof(h));
    le=1,mincost=0;
}
void calc(int k){
    //printf("calc(%d)\n",k);
    for(pii x:L)xL[x.se+1]=x.fi+1; //x[b+1] >= a+1
    for(pii x:R)xR[k-x.se]=x.fi-1; //x[k-b] <= a-1
    for(pii x:D)yL[x.se+1]=x.fi+1;
    for(pii x:U)yR[k-x.se]=x.fi-1;
    xR[k+1]=INF;
    FOR(i,1,k)if(!xL[i])xL[i]=xL[i-1];
    ROF(i,k,1)if(!xR[i])xR[i]=xR[i+1];
    yR[k+1]=INF;
    FOR(i,1,k)if(!yL[i])yL[i]=yL[i-1];
    ROF(i,k,1)if(!yR[i])yR[i]=yR[i+1];
    //FOR(i,1,k)printf("[%d,%d]%c",xL[i],xR[i]," \n"[i==k]);
    //FOR(i,1,k)printf("[%d,%d]%c",yL[i],yR[i]," \n"[i==k]);

    // item: 1 - n, n+1 - 2n
    // x : 2n+1 - 2n+k
    // y : 2n+k+1 - 2n+2k
    // s : 0
    // t : 2n+2k+1
    init();
    s=0,t=2*n+2*k+1;
    //printf("s=%d,t=%d\n",s,t);
    FOR(i,1,k)add_flow(s,2*n+i,1,0);
    FOR(i,1,k)FOR(j,1,n)if(xL[i]<=x[j]&&x[j]<=xR[i])add_flow(2*n+i,j,INF,0);
    FOR(i,1,n)add_flow(i,i+n,1,-val[i]);
    FOR(i,1,k)FOR(j,1,n)if(yL[i]<=y[j]&&y[j]<=yR[i])add_flow(j+n,2*n+k+i,INF,0);
    FOR(i,1,k)add_flow(2*n+k+i,t,1,0);

    LL res=go();
    ans=max(ans,-res);
}

int main(){
    scanf("%d",&n);
    FOR(i,1,n)scanf("%d%d%lld",&x[i],&y[i],&val[i]);
    scanf("%d",&m);
    FOR(i,1,m){
        char t[10];
        int a,b;
        scanf("%s%d%d",t,&a,&b);
        if(t[0]=='L')L.pb({a,b});
        else if(t[0]=='R')R.pb({a,b});
        else if(t[0]=='U')U.pb({a,b});
        else D.pb({a,b});
    }
    FOR(i,1,n)calc(i);
    printf("%lld\n",ans);
    return 0;
}