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 <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>

using namespace std;

typedef long long ll;

typedef long double dbl;
const int INF = 1.01e9;
 
const long double EPS = 1e-18;
bool eq(long double x, long double y) {
    return fabs(x - y) < EPS;
}
bool ls(long double x, long double y) {
    return x < y && !eq(x, y);
}
 
vector<long double> simplex(vector<vector<long double> > 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);
    auto pivot = [&](int x, int y) {
        swap(left[x], up[y]);
        long double 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) assert(0); // infeasible
        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<long double> 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];
    return ans;
}

#ifdef ONPC
  mt19937 rnd(228);
#else
  mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

int main()
{
#ifdef ONPC
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector <int> x(n), y(n);
  vector <ll> ok;
  vector <long double> v;
  vector <pair <ll, int> > e;
  for (int i = 0; i < n; i++)
  {
    cin >> x[i] >> y[i];
    ll x;
    cin >> x;
    ok.push_back(x);
    v.push_back(x);
    e.push_back({x, i});
  }
  v.insert(v.begin(), 0);
  int m;
  cin >> m;
  vector <vector <long double> > mat;
  mat.push_back(v);
  for (int i = 0; i < n; i++)
  {
    vector <long double> kek(n + 1);
    kek[0] = kek[i + 1] = 1;
    mat.push_back(kek);
  }
  vector <vector <int> > simp;
  for (int i = 0; i < m; i++)
  {
    char c;
    cin >> c;
    int a;
    int b;
    cin >> a >> b;
    vector <long double> arr = {(long double) b};
    vector <int> kek = {b};
    for (int j = 0; j < n; j++)
    {
      int grab = 0;
      if (c == 'L' && x[j] <= a) grab = 1;
      if (c == 'R' && x[j] >= a) grab = 1;
      if (c == 'D' && y[j] <= a) grab = 1;
      if (c == 'U' && y[j] >= a) grab = 1;
      arr.push_back(grab);
      kek.push_back(grab);
    }
    mat.push_back(arr);
    simp.push_back(kek);
  }
  auto good = [&] (vector <int> grab)
  {
    for (auto c : simp)
    {
      int sum = 0;
      for (int i = 1; i <= n; i++) sum += grab[i - 1] * c[i];
      if (sum > c[0])
      {
        return false;
      }
    }
    return true;
  };
  auto f = [&] (vector <int> grab)
  {
    ll sum = 0;
    for (int i = 0; i < n; i++)
    {
      if (grab[i]) sum += ok[i];
    }
    return sum;
  };
  vector <int> grab;
  for (int i = 0; i < n; i++) grab.push_back(0);
  cout.precision(20);
  auto solve = [&] ()
  {
    auto ret = simplex(mat);
    sort(e.begin(), e.end());
    vector <int> me(n);
    int kek = 0;
    for (int i = (int) e.size() - 1; i >= 0; i--)
    {
      if (!grab[e[i].second] && ret[e[i].second + 1] > EPS)
      {
        kek++;
        me[e[i].second] = true;
        grab[e[i].second] = true;
        if (!good(grab)) grab[e[i].second] = false, me[e[i].second] = false;
      }
    }
    int kekos = 0;
    for (auto &c : mat)
    {
      kekos++;
      if (kekos > n + 1)
      for (int i = 1; i <= n; i++)
      {
        if (me[i - 1] && c[i] > EPS) c[i]--, c[0]--;
      }
    }
    for (int i = 1; i <= n; i++)
    {
      if (me[i - 1])
      {
        mat[i][0] = 0;
      }
    }
    return kek;
  };
  while (solve());
  bool ch = true;
  while (ch)
  {
    ch = false;
    for (int i = (int) e.size() - 1; i >= 0; i--)
    {
      if (!grab[e[i].second])
      {
        grab[e[i].second] = true;
        if (good(grab))
        {
          ch = true;
        }
        else
        {
          grab[e[i].second] = false;
        }
      }
    }
    if (!ch)
    {
      for (int i = 0; i < (int) e.size(); i++)
      {
        for (int j = i + 1; j < (int) e.size(); j++)
        {
          if (e[i].first < e[j].first && grab[e[i].second] && !grab[e[j].second])
          {
            auto x = grab;
            swap(x[e[i].second], x[e[j].second]);
            if (good(x))
            {
              grab = x;
              ch = true;
            }
          }
        }
      }
    }
  }
  ll best = f(grab);
  cout << best << endl;
}