#include <bits/stdc++.h>
using namespace std;
#define int long long
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pp pop_back
#define endl '\n'
#define ff first
#define ss second
#define stop exit(0)
#define sz(x) (int)x.size()
#define pause system("pause")
#define all(x) (x).begin(), (x).end()
#define deb(x) cout << #x << "-" << x << endl
typedef char chr;
typedef string str;
typedef long long ll;
typedef vector<int> vii;
typedef pair<int, int> pii;
const long long INF = LLONG_MAX;
const int inf = INT_MAX;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
const double PI = 2 * acos(0.0);
const int N = 2e3 + 5;
int n,m,a,b,c;
vector <array<int,4>> v;
int dp[N*55];
int ans;
inline void test_case() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a >> b >> c;
        v.eb(array<int,4>{b, 1, -a, -c});
    }
    cin >> m;
    for (int i = 1; i <= m; i++) {
        cin >> a >> b >> c;
        v.eb(array<int,4>{b, 0, a, c});
    }
    sort(all(v));
    reverse(all(v));
    dp[0] = 0;
    for (int i = 1; i <= 50*N; i++) {
        dp[i] = -INF;
    }
    for (auto [b, t, a, c] : v) {
        if (c <= 0 && a <= 0) {
            for (int i = N * 50; i >= -a; i--) {
                dp[i] = max(dp[i], dp[i + a] + c);
            }
        } else {
            for (int i = 0; i + a <= N * 50; i++) {
                dp[i] = max(dp[i], dp[i + a] + c);
            }
        }
    }
    for (int i = 0 ; i <= 55*n; i++)  {
        // if (x == inf) continue;
        ans = max(dp[i],ans);
    }
    cout << ans << endl;
 
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) {
        test_case();
    }
    return 0;
}
| # | 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... |