#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define pb push_back
#define all(x) begin(x), end(x)
#define umap unordered_map
#define space " "
#define TEST_CASES int a; cin >> a; for (int i = 0; i < a; i++) {solve(); cout << endl;}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
struct cow {
int c; ll f, w;
};
bool cmp(const cow &x, const cow &y) {
if (x.f != y.f) {
return x.f > y.f;
}
if (x.w != y.w) {
return x.w > y.w;
}
return x.c > y.c;
}
void solve() {
int n; cin >> n;
vector<cow> vec(n); int tot = 0;
for (int i = 0; i < n; i++) {
cin >> vec[i].c >> vec[i].f >> vec[i].w;
tot += vec[i].c;
}
sort(all(vec), cmp);
int m; cin >> m;
for (int i = 0; i < m; i++) {
cow x{};
cin >> x.c >> x.f >> x.w;
x.w *= -1;
vec.pb(x);
}
sort(all(vec), cmp);
vector<ll> dp(tot + 1, LLONG_MIN);
dp[0] = 0;
for (int i = 0; i < n + m; i++) {
vector<ll> ndp = dp;
for (int j = 0; j <= tot; j++) {
if (dp[j] == LLONG_MIN) {
continue;
}
if (vec[i].w > 0) {
if (j + vec[i].c <= tot) {
ndp[j + vec[i].c] = max(ndp[j + vec[i].c], dp[j] - vec[i].w);
}
}
else {
if (j - vec[i].c >= 0) {
ndp[j - vec[i].c] = max(ndp[j - vec[i].c], dp[j] - vec[i].w);
}
}
}
swap(dp, ndp);
}
cout << *max_element(all(dp));
}
int main() {
FAST_IO;
//freopen("mooriokart.in", "r", stdin);
//freopen("mooriokart.out", "w", stdout);
//TEST_CASES;
solve(); cout << endl;
/*int a; cin >> a;
for (int i = 1; i <= a; i++){
cout << "Case #" << i << ": ";
solve();
cout << endl;
}*/
}
| # | 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... |