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 all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
const ll inf = 1e17;
struct st{
ll c, f, v;
};
int sfst = 0;
ll sf(st a, st b){
return a.f > b.f;
}
ll n, m;
st a[2009];
st b[2009];
ll dp[2001][109];
ll nxt[2001][109];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n;
for(ll i = 0; i < n; ++i)
cin >> a[i].c >> a[i].f >> a[i].v;
cin >> m;
for(ll i = 0; i < m; ++i)
cin >> b[i].c >> b[i].f >> b[i].v;
sort(a, a+n, sf); sfst = 1;
sort(b, b+m, sf);
for(ll j = 0; j <= m; ++j)
for(ll k = 0; k <= 55; ++k)
dp[j][k] = nxt[j][k] = -inf;
dp[0][0] = 0;
ll ans = 0;
for(ll i = 0; i < n; ++i){
for(ll j = 0; j < m; ++j)
for(ll k = 55; k >= 0; --k){
nxt[j][k] = max(nxt[j][k], dp[j][k]);
dp[j+1][k] = max(dp[j+1][k], dp[j][k]);
if(k >= b[j].c)
dp[j+1][k-b[j].c] = max(dp[j+1][k-b[j].c], dp[j][k]+b[j].v);
if(a[i].f >= b[j].f){
if(k+a[i].c <= 55){
nxt[j][k+a[i].c] = max(nxt[j][k+a[i].c], dp[j][k]-a[i].v);
}
if(k+a[i].c-b[j].c >= 0 && k+a[i].c-b[j].c <= 55)
nxt[j+1][k+a[i].c-b[j].c] = max(nxt[j+1][k+a[i].c-b[j].c], dp[j][k]-a[i].v+b[j].v);
}
}
for(ll j = 0; j <= m; ++j)
for(ll k = 0; k <= 55; ++k){
ans = max(ans, nxt[j][k]);
ans = max(ans, dp[j][k]);
dp[j][k] = nxt[j][k];
nxt[j][k] = -inf;
}
}
assert(ans < 2000LL*(1e9+7));
cout << ans << '\n';
}
# | 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... |