// UUID: 5aa102eb-e44c-48bc-bbef-2201e44cf45b
#include <bits/stdc++.h>
#include <climits>
using namespace std;
typedef long long ll;
#define al3 array<ll, 3>
//subtask 4. memory limit exceeded
/*
int main() {
int n, m;
cin >> n;
vector<al3> g(n + 1);
for (int i = 1; i <= n; i ++) cin >> g[i][0] >> g[i][1] >> g[i][2];
cin >> m;
vector<al3> a(m + 1);
for (int i = 1; i <= m; i ++) cin >> a[i][0] >> a[i][1] >> a[i][2];
vector<vector<ll>> dp1(n + 1, vector<ll> (max(m, n) * 50 + 1, INT_MAX));
vector<vector<ll>> dp2(m + 1, vector<ll> (max(m, n) * 50 + 1));
for (int i = 1; i <= n; i ++) {
for (int j = n * 50; j >= 0; j --) {
if (g[i][0] < j) {
if (dp1[i - 1][j - g[i][0]] != INT_MAX)
dp1[i][j] = min(dp1[i - 1][j], dp1[i - 1][j - g[i][0]] + g[i][2]);
}
else dp1[i][j] = min(dp1[i - 1][j], g[i][2]);
}
}
for (int i = 1; i <= m; i ++) {
for (int j = m * 50; j >= 0; j --) {
if (a[i][0] <= j) {
if (dp2[i - 1][j - a[i][0]] != 0 || a[i][0] == j)
dp2[i][j] = max(dp2[i - 1][j], dp2[i - 1][j - a[i][0]] + a[i][2]);
}
else dp2[i][j] = dp2[i - 1][j];
}
}
ll ans = 0;
for (int i = 1; i <= max(m, n) * 50; i ++) ans = max(dp2[m][i] - dp1[n][i], ans);
cout << ans;
}
*/
const ll inf = LLONG_MIN;
int main() {
int n, m;
cin >> n;
vector<al3> v;
for (int i = 1; i <= n; i ++) {
ll a, b, c;
cin >> a >> b >> c;
v.push_back({b, a, (-1) * c});
}
cin >> m;
for (int i = 1; i <= m; i ++) {
ll a, b, c;
cin >> a >> b >> c;
v.push_back({b, a * (-1), c});
}
sort(v.rbegin(), v.rend());
vector<vector<ll>> dp(n + m + 1, vector<ll> (n * 50 + 1, inf));
for (int i = 1; i <= n + m; i ++) {
dp[i - 1][0] = max(dp[i - 1][0], ll(0));
for (int j = n * 50; j >= 0; j --) {
if (j - v[i - 1][1] >= 0 && j - v[i - 1][1] <= n * 50 && dp[i - 1][j - v[i - 1][1]] != inf)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - v[i - 1][1]] + v[i - 1][2]);
else dp[i][j] = dp[i - 1][j];
}
}
ll ans = 0;
for (int i = 0; i <= n * 50; i ++) ans = max(dp[n + m][i], ans);
cout << ans << "\n";
/*
for (int i = 0; i <= n + m; i ++) {
for (int j = 0; j <= 30; j ++) cout << dp[i][j] << " ";
cout << "\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... |