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 fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pill pair<int,ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define name "test"
using namespace std;
const int N = 1e3 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
struct item{
int c, f, v;
bool operator<(const item &other) const {
return f > other.f;
}
};
int n;
int m;
item a[N];
item b[N];
void load(){
cin.tie(0)->sync_with_stdio(0);
if (fopen(name".inp", "r")){
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
}
void input(){
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i].c >> a[i].f >> a[i].v;
cin >> m;
for (int i = 1; i <= m; i++)
cin >> b[i].c >> b[i].f >> b[i].v;
}
namespace subtask3{
ll f[2][N][N];
void solve(){
ll ans = 0;
sort(a + 1, a + n + 1);
sort(b + 1, b + m + 1);
int sum = 0;
mem(f, -0x3f);
f[0][m][0] = f[0][m + 1][0] = 0;
for (int i = 1; i <= n; i++){
bool cur = i & 1;
bool pre = cur ^ 1;
for (int j = m + 1; j >= 1; j--){
if (b[j].f > a[i].f) break;
for (int k = 0; k <= sum; k++){
maxi(f[cur][j][k], f[pre][j][k]);
maxi(f[cur][j][k], f[cur][j + 1][k]);
maxi(f[cur][j][k + a[i].c], f[pre][j][k] - a[i].v);
maxi(ans, f[cur][j][k]);
}
}
sum += a[i].c;
for (int j = m; j >= 1; j--){
if (b[j].f > a[i].f) break;
for (int k = 0; k <= sum; k++){
if (k + b[j].c <= sum){
maxi(f[cur][j][k], f[cur][j + 1][k + b[j].c] + b[j].v);
}
maxi(ans, f[cur][j][k]);
}
}
// if (i == 4){
// cout << f[cur][m - 1][1] << '\n';
// }
}
cout << ans;
}
}
int main(){
load();
input();
if (max(n, m) <= 250)
subtask3::solve();
}
Compilation message (stderr)
clo.cpp: In function 'void load()':
clo.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
clo.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen(name".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... |