// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 2000;
const int MAX_M = 2000;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
struct Cloud {
    int c, f, v;
    Cloud() {};
    Cloud(int c, int f, int v) : c(c), f(f), v(v) {};
};
int n, m;
Cloud comp[MAX_N + 5], order[MAX_M + 5];
namespace SUBTASK_4 {
    const int N = MAX_N;
    const int M = MAX_M;
    const int F = 1;
    const int NUM_CORES = 1e5;
    ll dpSell[NUM_CORES + 5], dpBuy[NUM_CORES + 5];
    bool checkSubtask() {
        for (int i = 1; i <= n; i++)
            if (comp[i].f != F) return false;
        for (int i = 1; i <= m; i++)
            if (order[i].f != F) return false;
        return true;
    };
    void Solve() {
        for (int i = 0; i <= NUM_CORES; i++) {
            dpSell[i] = -INF;
            dpBuy[i] = INF;
        };
        dpBuy[0] = 0;
        for (int i = 1, totalCores = 0; i <= n; i++) {
            totalCores += comp[i].c;
            for (int j = totalCores; j >= comp[i].c; j--) {
                dpBuy[j] = min(dpBuy[j], dpBuy[j - comp[i].c] + (ll)comp[i].c * comp[i].v);
            };
        };
        dpSell[0] = 0;
        for (int i = 1, totalCores = 0; i <= m; i++) {
            totalCores += order[i].c;
            for (int j = totalCores; j >= order[i].c; j--) {
                dpSell[j] = max(dpSell[j], dpSell[j - order[i].c] + order[i].v);
            };
        };
        ll res = 0;
        for (int i = NUM_CORES - 1; i >= 0; i--) dpBuy[i] = min(dpBuy[i], dpBuy[i + 1]);
        for (int i = 0; i <= NUM_CORES; i++) res = max(res, dpSell[i] - dpBuy[i]);
        cout << res << '\n';
    };
};  // namespace SUBTASK_4
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen("MAIN.INP", "r")) {
        freopen("MAIN.INP", "r", stdin);
        freopen("MAIN.OUT", "w", stdout);
    };
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int c, f, v;
        cin >> c >> f >> v;
        comp[i] = Cloud(c, f, v);
    };
    cin >> m;
    for (int i = 1; i <= m; i++) {
        int C, F, V;
        cin >> C >> F >> V;
        order[i] = Cloud(C, F, V);
    };
    SUBTASK_4::Solve();
};
컴파일 시 표준 에러 (stderr) 메시지
clo.cpp: In function 'int main()':
clo.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen("MAIN.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
clo.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("MAIN.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |