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>

#ifdef HOME
#define db(x) cerr << #x << " = " << x << endl
#define db3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", " << z << ")" << endl
#define db2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define dbv(a) cerr << #a << " = "; for (auto xxx: a) cerr << xxx  << " "; cerr << endl
#else
#define db(x) ;
#define db3(x, y, z) ;
#define db2(x, y) ;
#define dbv(a) ;
#endif

using namespace std;
typedef long long ll;
typedef long double dbl;


const int INF = 1.01e9;
const int MOD = (int)1e9 + 7;

struct Item {
    int x, y;
    ll val;
};

struct Cond {
    char type;
    int a, b;

    bool cover(Item it) {
        if (type == 'L') {
            return it.x <= a;
        }
        if (type == 'R') {
            return it.x >= a;
        }
        if (type == 'D') {
            return it.y <= a;
        }
        if (type == 'U') {
            return it.y >= a;
        }
        assert(0);
    }
};

ll slow(vector<Item> a, vector<Cond> b) {
    int n = a.size(), m = b.size();
    ll ans = 0;
    for (int mask = 0; mask < (1 << n); mask++) {
        vector<int> cnt(m);
        ll sum = 0;
        for (int i = 0; i < n; i++) {
            if (!(mask & (1 << i))) continue;
            sum += a[i].val;
            for (int j = 0; j < m; j++) {
                if (b[j].cover(a[i])) {
                    cnt[j]++;
                }
            }
        }
        bool bad = 0;
        for (int i = 0; i < m; i++) {
            if (cnt[i] > b[i].b) {
                bad = 1;
            }
        }
        if (bad) continue;
        ans = max(ans, sum);
    }
    return ans;
}


bool eq(dbl a, dbl b) {
    return fabs(a - b) < 1e-9;
}

bool ls(dbl a, dbl b) {
    return !eq(a, b) && a < b;
}

vector<dbl> simplex(vector<vector<dbl> > a) {
    int n = a.size() - 1;
    int m = a[0].size() - 1;
    vector<int> left(n + 1), up(m + 1);
    iota(up.begin(), up.end(), 0);
    iota(left.begin(), left.end(), m);
    while (1) {
        auto pivot = [&](int x, int y) {
            swap(left[x], up[y]);
            dbl k = a[x][y];
            a[x][y] = 1;
            vector<int> vct;
            for (int j = 0; j <= m; j++) {
                a[x][j] /= k;
                if (!eq(a[x][j], 0)) vct.push_back(j);
            }
            for (int i = 0; i <= n; i++) {
                if (eq(a[i][y], 0) || i == x) continue;
                k = a[i][y];
                a[i][y] = 0;
                for (int j : vct) a[i][j] -= k * a[x][j];
            }
        };
        while (1) {
            int x = -1;
            for (int i = 1; i <= n; i++) if (ls(a[i][0], 0) && (x == -1 || a[i][0] < a[x][0])) x = i;
            if (x == -1) break;
            int y = -1;
            for (int j = 1; j <= m; j++) if (ls(a[x][j], 0) && (y == -1 || a[x][j] < a[x][y])) y = j;
            if (y == -1) return vector<dbl>(m, 0);
            pivot(x, y);
        }
        while (1) {
            int y = -1;
            for (int j = 1; j <= m; j++) if (ls(0, a[0][j]) && (y == -1 || a[0][j] > a[0][y])) y = j;
            if (y == -1) break;
            int x = -1;
            for (int i = 1; i <= n; i++)
                if (ls(0, a[i][y]) && (x == -1 || a[i][0] / a[i][y] < a[x][0] / a[x][y]))
                    x = i;
            if (x == -1) assert(0); // unbounded
            pivot(x, y);
        }

        vector<int> v;
        vector<dbl> ans(m + 1);
        for (int i = 1; i <= n; i++) if (left[i] <= m) ans[left[i]] = a[i][0];
        ans[0] = -a[0][0];
        for (int i = 1; i <= m; i++) {
            if (!eq(ans[i], floor(ans[i])) && !eq(ans[i], floor(ans[i]) + 1)) {
                v.push_back(i);
            }
        }
        if (v.empty()) {
            return ans;
        }
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j < (int)v.size(); j++) {
                a[i].push_back(0);
            }
        }
        for (int i = 0; i < (int)v.size(); i++) {
            vector<dbl> cur(a[0].size());
            cur[m + 1 + i] = 1;
            for (int j = 0; j <= m; j++) {
                cur[j] = floor(a[v[i]][j]) - a[v[i]][j];
            }
            a.push_back(cur);
        }
        m += v.size();
        n += v.size();
        up.resize(up.size() + v.size());
        left.resize(left.size() + v.size());
    }
}
// j=1..m: x[j]>=0
// i=1..n: sum(j=1..m) A[i][j]*x[j] <= A[i][0]
// max sum(j=1..m) A[0][j]*x[j]
// res[0] is answer
// res[1..m] is certificate

ll fast(vector<Item> a, vector<Cond> b) {
    int n = a.size(), m = b.size();
    vector<vector<dbl>> c(m + 1, vector<dbl>(n + 1));
    for (int i = 0; i < n; i++) {
        c[0][1 + i] = a[i].val;
    }
    for (int i = 0; i < m; i++) {
        c[1 + i][0] = b[i].b;
        for (int j = 0; j < n; j++) {
            if (b[i].cover(a[j])) {
                c[1 + i][1 + j] = 1;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        vector<dbl> cur(n + 1);
        cur[0] = 1;
        cur[1 + i] = 1;
        c.push_back(cur);
    }
    ll res = 0;
    for (int cnt = 1; cnt <= n; cnt++) {
        auto cc = c;
        vector<dbl> cur(n + 1, 1);
        cur[0] = cnt;
        cc.push_back(cur);
        for (auto &x : cur) x *= -1;
        cc.push_back(cur);
        res = max(res, (ll)round(simplex(cc)[0]));
    }
    return res;
}

void stress() {
    for (int it =0;; it++) {
        mt19937_64 rnd(it);
        db(it);


        int n = rnd() % 10 + 1;
        vector<Item> a(n);
        for (int i =0 ; i < n; i++) {
            a[i].x = rnd() % 100;
            a[i].y = rnd() % 100;
            a[i].val = rnd() % (ll)1e15;
        }
        int m = rnd() % 10 + 1;
        vector<Cond> b(m);
        for (int i = 0; i < m; i++) {
            b[i].a = rnd() % 100;
            b[i].b = rnd() % (n + 1);
            b[i].type = "ULDR"[rnd() % 4];
        }

        auto ans1 = fast(a, b);
        auto ans2 = slow(a, b);
        if (ans1 != ans2) {
            cout << ans1 << " instead of " << ans2 << endl;
            exit(0);
        }
    }
}

int main() {
#ifdef HOME
    assert(freopen("in", "r", stdin));
    stress();
#endif

    int n;
    while (scanf("%d", &n) == 1) {
        vector<Item> a(n);
        for (int i = 0; i < n; i++) {
            scanf("%d%d%lld", &a[i].x, &a[i].y, &a[i].val);
        }
        int m;
        scanf("%d", &m);
        vector<Cond> b(m);
        for (int i = 0; i < m; i++) {
            scanf(" %c%d%d", &b[i].type, &b[i].a, &b[i].b);
        }

//        printf("%lld\n", slow(a, b));
        printf("%lld\n", fast(a, b));
    }

#ifdef HOME
    cerr << "time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
}