#include <bits/stdc++.h>
using namespace std;
struct T {
int c, f;
long long v;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector<T> a;
int maxc = 0;
for (int i = 0; i < n; i++) {
T t;
cin >> t.c >> t.f >> t.v;
t.v = -t.v;
a.push_back(t);
maxc += t.c;
}
int m; cin >> m;
for (int i = 0; i < m; i++) {
T t;
cin >> t.c >> t.f >> t.v;
t.c = -t.c;
a.push_back(t);
}
sort(a.begin(), a.end(), [](const T &x, const T &y) {
return (x.f != y.f) ? x.f > y.f : x.v < y.v;
});
const long long NEG = -4e18;
int sz = (int)a.size();
vector<vector<long long>> dp(sz + 1, vector<long long>(maxc + 1, NEG));
dp[0][0] = 0;
for (int i = 1; i <= sz; i++) {
for (int c = 0; c <= maxc; c++) {
dp[i][c] = dp[i-1][c];
int p = c - a[i-1].c;
if (p >= 0 && p <= maxc && dp[i-1][p] != NEG) {
dp[i][c] = max(dp[i][c], dp[i-1][p] + a[i-1].v);
}
}
}
cout << *max_element(dp[sz].begin(), dp[sz].end()) << "\n";
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... |