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>
#include <ext/pb_ds/assoc_container.hpp>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 998244353;
const int nax = 2e3;
const int MAX_VAL = 1e6+1;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
ll dp[nax][nax*50+5];
int main(){
optimise;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
int n;
cin>>n;
vector<pair<int,ii>> tab(4000);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < nax*50+5; ++j)
{
dp[i][j]=-1e16;
}
}
for (int i = 0; i < n; ++i)
{
cin>>tab[i].se.fi>>tab[i].fi>>tab[i].se.se;
tab[i].se.se=-tab[i].se.se;
}
int m;
cin>>m;
for (int i = 0; i < m; ++i)
{
cin>>tab[i+n].se.fi>>tab[i+n].fi>>tab[i+n].se.se;
}
tab.resize(n+m);
sort(tab.begin(),tab.end(),greater<>());
priority_queue<pair<ll,ii>> pq;
pq.push({0,{0,0}});
//dp[0][0]=0;
if(tab[0].se.se<0) {
pq.push({tab[0].se.se,{tab[0].se.fi,0}});
//dp[0][tab[0].c]=tab[0].v;
}
ll mx=0;
while(!pq.empty()){
ll v=pq.top().fi;
int c=pq.top().se.fi;
int idx=pq.top().se.se;
//cout <<v<<" "<<c<<" "<<idx<<endl;
pq.pop();
mx=max(mx,v);
if (idx+1==n+m) continue;
if (tab[idx+1].se.se<0){
if (dp[idx+1][c+tab[idx+1].se.fi]<v+tab[idx+1].se.se) {
pq.push({v+tab[idx+1].se.se,{c+tab[idx+1].se.fi,idx+1}});
dp[idx+1][c+tab[idx+1].se.fi]=v+tab[idx+1].se.se;
}
if (dp[idx+1][c]<v) {
pq.push({v,{c,idx+1}});
dp[idx+1][c]=v;
}
}else{
// cout <<dp[idx+1][c]<<endl;
if (dp[idx+1][c]<v) {
//cout <<"hey"<<endl;
pq.push({v,{c,idx+1}});
dp[idx+1][c]=v;
}
//cout<<"hey"<<endl;
if (c<tab[idx+1].se.fi) continue;
if (dp[idx+1][c-tab[idx+1].se.fi]<v+tab[idx+1].se.se){
pq.push({v+tab[idx+1].se.se,{c-tab[idx+1].se.fi,idx+1}});
dp[idx+1][c-tab[idx+1].se.fi]=v+tab[idx+1].se.se;
}
}
}
cout <<mx<<endl;
}
Compilation message (stderr)
clo.cpp: In function 'void setIO(std::string)':
clo.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clo.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen((s + ".out").c_str(), "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... |