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>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
const ll INF = 1e15;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e3 + 5;
const ll LOG = 31;
#define vll vector <ll>
#define pll pair <ll, ll>
#define fi first
#define se second
#define endl '\n'
ll n, m, d;
pll a [MAXN], b [MAXN];
ll dp [3][50*MAXN];
vector <array <ll, 3>> ab;
void solve(){
cin >> n;
ab.push_back({-INF, 0, 0});
for(ll i = 1; i <= n; i++){
ll c, f, v; cin >> c >> f >> v;
a[i] = {c, v};
ab.push_back({-f, 0, i});
}
cin >> m;
for(ll i = 1; i <= m; i++){
ll c, f, v; cin >> c >> f >> v;
b[i] = {c, v};
ab.push_back({-f, 1, i});
}
sort(ab.begin(), ab.end());
for(ll j = 0; j < 50*MAXN; j++) dp[1][j] = -INF;
dp[1][0] = 0;
ll ans = -INF;
// for(ll i = 1; i <= n+m; i++){
// cout << ab[i][0] << " " << ab[i][1] << " " << ab[i][2] << endl;
// }
for(ll i = 1; i <= n+m; i++){
for(ll j = 0; j < 50*MAXN; j++){
dp[2][j] = dp[1][j];
if(ab[i][1]){
ll c = b[ab[i][2]].fi, v = b[ab[i][2]].se;
if(j+c < 50*MAXN) dp[2][j] = max(dp[2][j], dp[1][j+c] + v);
}
else{
ll c = a[ab[i][2]].fi, v = a[ab[i][2]].se;
if(j-c >= 0) dp[2][j] = max(dp[2][j], dp[1][j-c] - v);
}
ans = max(ans, dp[2][j]);
}
for(ll j = 0; j < 50*MAXN; j++) dp[1][j] = dp[2][j];
// for(ll j = 0; j <= 40; j++){
// cout << dp[2][j] << " ";
// }
// cout << endl;
}
cout << ans << endl;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// ll t; cin >> t;
// while(t--){
solve();
// }
}
# | 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... |