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 ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e5+5;
const ll inf=1e18;
int n, m;
int c[mxn];
ll f[mxn], v[mxn];
int main() {
cin >> n;
int tot = 0;
for(int i = 1; i <= n; i++) {
cin >> c[i] >> f[i] >> v[i];
tot += c[i];
}
int m;
cin >> m;
for(int i = n + 1; i <= n + m; i++) {
cin >> c[i] >> f[i] >> v[i];
}
vector<int> ord(n + m);
iota(ord.begin(), ord.end(), 1);
sort(ord.begin(), ord.end(), [](int a, int b) {
if(f[a] == f[b]) return a < b;
return f[a] > f[b];
});
ll dp[tot + 55] = {};
ll cur[tot + 55] = {};
for(int j = 0; j < tot + 55; j++) dp[j] = cur[j] = -inf;
dp[0] = 0;
ll ans = 0;
int cc = 0;
for(int i = 1; i <= n + m; i++) {
int x = ord[i - 1];
if(x <= n) cc += c[x];
for(int j = 0; j <= cc; j++) cur[j] = -inf;
for(int j = 0;j <= cc; j++) {
if(x <= n) {
cur[j] = dp[j];
if(j >= c[x]) cur[j] = max(cur[j], dp[j - c[x]] - v[x]);
} else {
cur[j] = max(dp[j], dp[j + c[x]] + v[x]);
}
ans = max(ans, cur[j]);
}
for(int j = cc; j >= 0; j--) cur[j] = max(cur[j], cur[j + 1]);
for(int j = 0; j <= cc; j++) dp[j] = cur[j];
}
cout<<ans<<endl;
}
# | 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... |