#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<pair<ll, pair<ll, ll>>> a;
    for (int i = 0; i < n; i++) {
        ll c, f, v;
        cin >> c >> f >> v;
        a.push_back({f, {c, -v}});
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        ll c, f, v;
        cin >> c >> f >> v;
        a.push_back({f, {-c, v}});
    }
    sort(a.begin(), a.end(), greater<pair<ll, pair<ll ,ll>>>());
    vector<ll> dp ((1e5)+100, -(ll)1e18);
    dp[0] = 0;
    for (auto x : a) {
        if (x.second.first > 0) {
            for (int i = (1e5)-1; i >= 0; i--) {
                if ((dp[i] != -(ll)1e18) && i+x.second.first >= 0) {
                    dp[i+x.second.first] = max(dp[i+x.second.first], dp[i]+x.second.second);
                }
            }
        }
        else {
            for (int i = 0; i <1e5; i++) {
                if ((dp[i] != -(ll)1e18) && i+x.second.first >= 0) {
                    dp[i+x.second.first] = max(dp[i+x.second.first], dp[i]+x.second.second);
                }
            }
        }
    }
    ll w = 0;
    for (ll i : dp) {
        w = max(w, i);
    }
    cout << w << '\n';
}
| # | 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... |