#include <bits/stdc++.h>
#include <climits>
using namespace std;
typedef long long ll;
#define al3 array<ll, 3>
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;
}
# | 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... |