This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
void fast(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define int long long
const int INF=-1e18;
struct info{int x, y, z;};
bool cmp(info a, info b){
if(a.y!=b.y) return a.y>b.y;
return a.z<b.z;
}
signed main(){
fast();
vector<info> v;
int n, m=0; cin>>n;
for(int i=1; i<=n; i++){
int x, y, z; cin>>x>>y>>z;
v.push_back({x, y, -z});
m+=x;
}
cin>>n;
for(int i=1; i<=n; i++){
int x, y, z; cin>>x>>y>>z;
v.push_back({-x, y, z});
}
sort(v.begin(), v.end(), cmp);
vector<int> dp(m+1, INF);
dp[0]=0;
for(auto [x, y, z]:v){
vector<int> dp2=dp;
for(int i=0; i<=m; i++){
int prev=i-x;
if(prev>=0 && prev<=m && dp[prev]!=INF){
dp2[i]=max(dp2[i], dp[prev]+z);
}
}
dp=dp2;
}
cout<<*max_element(dp.begin(), dp.end());
}
/*
int main(){
fast();
int n; cin>>n;
vector<tuple<int,int>> a;
int s[n], t[n], w[n];
for(int i=0; i<n; i++){
cin>>s[i]>>t[i];
a.push_back({t[i], s[i], i});
}
sort(a.begin(), a.end());
}
*/
/*
int main(){
fast();
int n, m; cin>>n;
int a[n+1], b[n+1], c[n+1];
bool s3=true;
for(int i=1; i<=n; i++){
cin>>a[i]>>b[i]>>c[i];
}
cin>>m;
int x[m+1], y[m+1], z[m+1];
for(int i=1; i<=m; i++){
cin>>x[i]>>y[i]>>z[i];
}
}*/
# | 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... |