#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
vector<vector<int>> v;
const int mxN = 5000;
long long dp[mxN][mxN];
long long f(int idx, int cores){
if(idx == (int) v.size()) return 0;
if(dp[idx][cores] != -1) return dp[idx][cores];
long long not_take = f(idx + 1, cores), take = 0;
if(v[idx][1] == 1){
take = (long long) f(idx + 1, cores + v[idx][2]) - v[idx][3];
}else{
if(v[idx][2] <= cores){
take = (long long) f(idx + 1, cores - v[idx][2]) + v[idx][3];
}
}
return dp[idx][cores] = max(take, not_take);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
int n, m; cin >> n;
memset(dp, -1, sizeof(dp));
for(int i = 0, a, b, c; i < n; ++i){
cin >> a >> b >> c;
v.push_back({b, 1, a, c});
}
cin >> m;
for(int i = 0, a, b, c; i < m; ++i){
cin >> a >> b >> c;
v.push_back({b, 0, a, c});
}
sort(all(v), greater<>());
cout << f(0, 0) << "\n";
return 0;
}
| # | 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... |