#include<bits/stdc++.h>
using namespace std;
// define
#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
#define ll long long
#define ld double
#define ii pair<int,int>
#define se second
#define fi first
#define iii pair<int,ii>
#define all(v) v.begin(),v.end()
#define bit(x,i) (((x)>>(i))&1LL)
#define flip(x,i) ((x)^(1LL<<(i)))
#define ms(d,x) memset(d,x,sizeof(d))
#define sitingfake 1
#define orz 1
//constant
const ll mod = 1e9 + 7;
const long long linf = 4557430888798830399;
const int inf = 1061109567;
const int maxarr = 1e6 + 5;
const int dx[] = {0,1,-1,0};
const int dy[] = {1,0,0,-1};
template<typename T> bool maximize(T &a, const T &b)
{
if(a < b) {a = b; return 1;}
return 0;
}
template<typename T> bool minimize(T &a, const T &b)
{
if(a > b) {a = b; return 1;}
return 0;
}
inline void Plus(ll & a ,ll b)
{
b %= mod;
a += b;
if(a >= mod) a -= mod;
if(a < 0) a += mod;
return;
}
inline void Mul(ll & a, ll b)
{
a %= mod; b %= mod;
a *= b;
a %= mod;
return;
}
//code
struct Computer
{
int c;
int f;
ll val;
bool type;
bool operator < (Computer & other)
{
if(f == other.f) return type < other.type;
return f > other.f;
}
};
ll dp[100005];
vector <Computer> P;
int n , m;
int Lim;
void solve(void)
{
cin >> n;
for(int i = 1; i <= n; i++)
{
Computer tmp;
cin >> tmp.c >> tmp.f >> tmp.val;
tmp.val = -tmp.val;
tmp.type = 0;
P.push_back(tmp);
Lim += tmp.c;
}
cin >> m;
for(int i = 1; i <= m; i++)
{
Computer tmp;
cin >> tmp.c >> tmp.f >> tmp.val;
tmp.type = 1;
tmp.c = -tmp.c;
P.push_back(tmp);
}
sort(all(P));
ms(dp , -0x3f);
dp[0] = 0;
for(Computer i : P)
{
if(i.c < 0)
{
for(int j = 0; j <= Lim; j++)
{
if(j + abs(i.c) > Lim) break;
if(dp[j - i.c] == -linf) continue;
maximize(dp[j] , dp[j - i.c] + i.val);
}
}
else
{
for(int j = Lim ; j >= i.c; j--)
{
if(dp[j - i.c] == -linf) continue;
maximize(dp[j] , dp[j - i.c] + i.val);
}
}
}
ll ans = 0;
for(int i = 0; i <= Lim; i++) maximize(ans , dp[i]);
cout << ans;
}
/**
**/
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int tc; tc = 1;
while(tc--) solve();
//execute;
}
Compilation message (stderr)
clo.cpp: In function 'int main()':
clo.cpp:151:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
clo.cpp:152:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | freopen(task".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... |