//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, b, a) for (int i = b; i >= a; --i)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define lsb(x) (x)&(-x)
void setIO(string name = "") {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
ll fexp(ll a, ll b, ll m) {
if (b == 0) return 1LL;
ll p = a;
ll ans = 1;
while (b > 0) {
if (b % 2 != 0) ans = (ans*p)%m;
p = (p*p)%m;
b >>= 1;
}
return ans;
}
const int MAXN = 4e3 + 10;
const int INF = 1e18+5;
const int MOD = 1e9+7;
int dp[2][50*MAXN];
array<int,3> a[MAXN];
void solve() {
int n; cin>>n;
rep(i,1,n) {
int c,f,p;cin >> c >> f >> p;
a[i] = {c,f,-p};
}
int m;cin >> m;
rep(i,n+1,n+m) {
int c,f,p;cin >> c >> f >> p;
a[i] = {-c,f,p};
}
n = n+m;
sort(a+1,a+n+1, [](array<int,3> &x, array<int,3> &y) {
if (x[1]==y[1]) return x[2]<y[2];
return x[1]>y[1];
});
int ans = 0;
rep(i,1,50*n) dp[0][i] = dp[1][i] = -INF;
rep(i,1,n) {
//cout << a[i][0] << ' ' << a[i][1] << ' ' << a[i][2] << '\n';
rep(c,0,50*n) {
if (c-a[i][0] >= 0 && c-a[i][0] <= 50*n&& dp[0][c-a[i][0]] !=-INF) dp[1][c] = max(dp[0][c], dp[0][c-a[i][0]] + a[i][2]);
ans = max(ans,dp[1][c]);
}
rep(c,0,50*n) dp[0][c] = dp[1][c];
}
cout << ans << '\n';
}
//cout << " \n"[i==n];
int32_t main() {
setIO();
int tt = 1;
//cin >> tt;
while (tt-- > 0) solve();
}
Compilation message (stderr)
clo.cpp: In function 'void setIO(std::string)':
clo.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clo.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |