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>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll inf = 2e18;
const ll B = 320;
const ll N = 2e5+8;
struct node{
ll x,y,z,t;
} a[N];
ll n,m;
ll dp[N];
void solve(){
cin >> n;
ll bound = 0;
for(ll i = 1; i <= n; i++){
ll x,y,z;cin >> x >> y >> z;
a[i] = {x,y,z,0};
bound += x;
}
cin >> m;
fill(dp,dp+N,-inf);
for(ll i = n+1; i <= n+m; i++){
ll x,y,z;cin >> x >> y >> z;
a[i] = {x,y,z,1};
}
sort(a+1,a+n+m+1,[&](node a, node b){
return a.y > b.y || (a.y == b.y && a.t < b.t);
});
dp[0] = 0;
for(ll i = 1; i <= n+m; i++){
if(a[i].t == 0){
for(ll j = bound; j >= a[i].x; j--){
dp[j] = max(dp[j],dp[j-a[i].x]-a[i].z);
}
}
else{
for(ll j = 0; j <= bound-a[i].x; j++){
dp[j] = max(dp[j],dp[j+a[i].x]+a[i].z);
}
}
}
ll ans = *max_element(dp,dp+bound+1);
cout << ans;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if (fopen("test.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll T = 1;
// cin >> T;
for (ll i = 1; i <= T; i++) {
solve();
cout << '\n';
}
}
Compilation message (stderr)
clo.cpp: In function 'int main()':
clo.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
clo.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("test.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... |