#include <bits/stdc++.h>
#define task "BriantheCrab"
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define szf sizeof
#define sz(s) (int)((s).size())
using namespace std;
template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}
const int maxN = 1e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
struct Que {
int c, f, v, t;
};
bool cmp (Que A, Que B) {
if (A.f == B.f) {
return A.t < B.t;
}
return A.f > B.f;
}
vector <Que> all;
int dp[2][maxN];
void solve () {
int n;
cin >> n;
for (int i = 1; i <= n; i ++) {
int c, f, v;
cin >> c >> f >> v;
all.push_back ({c, f, v, 1});
}
int m;
cin >> m;
for (int i = 1; i <= m; i ++) {
int c, f, v;
cin >> c >> f >> v;
all.push_back ({c, f, v, 2});
}
int res = 0;
for (int j = 0; j <= 50 * n; j ++) {
dp[0][j] = dp[1][j] = -inf;
}
dp[0][0] = dp[1][0] = 0;
sort (all.begin (), all.end (), cmp);
for (int i = 0; i < sz (all); i ++) {
int cur = i % 2;
int prev = (i & 1) ^ 1;
for (int j = 0; j <= 50 * n; j ++) {
if (all[i].t == 1) {
maxi (dp[cur][j + all[i].c], dp[prev][j] - all[i].v);
}
else if (j >= all[i].c) {
maxi (dp[cur][j - all[i].c], dp[prev][j] + all[i].v);
}
}
for (int j = 0; j <= 50 * n; j ++) {
maxi (dp[cur][j], dp[prev][j]);
dp[prev][j] = -inf;
//cout << i << ' ' << j << ' ' << dp[cur][j] << '\n';
maxi (res, dp[cur][j]);
}
}
cout << res;
return;
}
signed main () {
cin.tie (nullptr) -> sync_with_stdio (false);
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t --) {
solve ();
}
return 0;
}
// thfdgb
Compilation message (stderr)
clo.cpp: In function 'int main()':
clo.cpp:81:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
clo.cpp:82:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen (task".out", "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... |