#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 2005;
const ll LLINF = 1e18;
ll dp[N*50], dp2[N*50], tsum[N];
struct Com { int c, f, v; } a[N], q[N];
vector<int> com, v[N], vq[N];
int main(void)
{ IO
int n, i, Q, sum = 0, pre = 0;
ll ans = -LLINF;
cin >> n;
for (i=1; i <= n; ++i)
cin >> a[i].c >> a[i].f >> a[i].v, com.pb(a[i].f);
cin >> Q;
for (i=1; i <= Q; ++i) cin >> q[i].c >> q[i].f >> q[i].v;
sort(all(com));
com.resize(unique(all(com))-com.begin());
for (i=1; i <= n; ++i)
{
int j = lower_bound(all(com), a[i].f) - com.begin() + 1;
v[j].pb(i), tsum[j] += a[i].c, sum += a[i].c;
}
for (i=1; i <= Q; ++i)
{
int j = lower_bound(all(com), q[i].f) - com.begin() + 1;
vq[j].pb(i);
}
for (i=1; i <= sum; ++i) dp[i] = -LLINF, dp2[i] = LLINF;
for (i=SZ(com); i >= 1; --i)
{
for (int j : vq[i])
{
for (int k=sum; k >= q[j].c; --k)
{
if (dp[k - q[j].c] == -LLINF) continue;
dp[k] = max(dp[k], dp[k - q[j].c] + q[j].v);
}
}
for (int j : v[i])
{
for (int k=sum; k >= a[j].c; --k)
{
if (dp2[k - a[j].c] == LLINF) continue;
dp2[k] = min(dp2[k], dp2[k - a[j].c] + a[j].v);
}
}
/*debug(sum), endl;
for (int j=1; j <= sum; ++j) bug(dp[j]); endl;
for (int j=1; j <= sum; ++j) bug(dp2[j]); endl;*/
for (int j=1; j <= sum; ++j)
{
//if (dp[j]-dp2[j] > ans) debug(j), debug(dp[j]), debug(dp2[j]), endl;
ans = max(ans, dp[j] - dp2[j]);
}
}
cout << ans << '\n';
return 0;
}
Compilation message
clo.cpp: In function 'int main()':
clo.cpp:25:27: warning: unused variable 'pre' [-Wunused-variable]
25 | int n, i, Q, sum = 0, pre = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |