#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)x.size()
#define all(x) x.begin() , x.end()
const int N = 2000 * 50 + 5;;
const int inf = 1e18 + 7;
void solve(){
int n;
cin >> n;
array<int,3>shop[n];
for(int i = 0;i<n;i++)
for(int j = 0;j<3;j++)
cin >> shop[i][j];
int m;
cin >> m;
array<int,3>order[m];
for(int i = 0;i<m;i++)
for(int j = 0;j<3;j++)
cin >> order[i][j];
vector<int>comp;
for(int i = 0;i<n;i++){
comp.push_back(shop[i][1]);
}
for(int i = 0;i<m;i++){
comp.push_back(order[i][1]);
}
sort(all(comp));
comp.resize(unique(all(comp)) - comp.begin());
vector<pair<int,int>>add[sz(comp)],del[sz(comp)];
for(int i = 0;i<n;i++){
shop[i][1] = sz(comp) - (lower_bound(all(comp),shop[i][1]) - comp.begin()) - 1;
add[shop[i][1]].push_back({shop[i][0] , shop[i][2]});
}
for(int i = 0;i<m;i++){
order[i][1] = sz(comp) - (lower_bound(all(comp),order[i][1]) - comp.begin()) - 1;
del[order[i][1]].push_back({order[i][0] , order[i][2]});
}
vector<int>dp(N+1,-inf);
dp[0] = 0;
for(int i = 0;i<sz(comp);i++){
for(auto itr : add[i]){
vector<int>ndp = dp;
for(int j = N;j>=itr.first;j--){
if(dp[j-itr.first] == -inf)continue;
ndp[j] = max(ndp[j] , dp[j-itr.first] - itr.second);
}
dp.swap(ndp);
}
for(auto itr : del[i]){
vector<int>ndp = dp;
for(int j = 0;j<=N-itr.first;j++){
if(dp[j+itr.first] == -inf){
continue;
}
ndp[j] = max(ndp[j] , dp[j+itr.first] + itr.second);
}
dp.swap(ndp);
}
}
int ans = 0;
for(int i = 0;i<N;i++)
if(ans < dp[i])
ans = dp[i];
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase=1;//cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << 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... |