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;
#define task ""
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const long long INF = 1e18;
const int MAX = 4004;
int n, m;
long long dp[100100];
struct node {
int c, f, v, t;
node(int _c = 0, int _f = 0, int _v = 0, int _t = 0) {
c = _c; f = _f; v = _v; t = _t;
}
bool operator< (const node &other) const {
if (f != other.f) return f > other.f;
return t < other.t;
}
};
node a[MAX];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; ++i) {
int c, f, v; cin >> c >> f >> v;
a[i] = node(c, f, v, 0);
}
cin >> m;
for (int i = 1; i <= m; ++i) {
int c, f, v; cin >> c >> f >> v;
a[i + n] = node(c, f, v, 1);
}
for (int i = 1; i <= 100000; ++i) dp[i] = -INF;
sort(a + 1, a + 1 + n + m);
dp[0] = 0;
for (int i = 0, sum = 0; i < n + m; ++i) {
if (a[i].t == 0) sum += a[i].c;
if (a[i + 1].t == 0) {
for (int j = sum; j >= 0; --j)
maximize(dp[j + a[i + 1].c], dp[j] - a[i + 1].v);
} else {
for (int j = 0; j <= sum; ++j)
maximize(dp[j], dp[j + a[i + 1].c] + a[i + 1].v);
}
}
cout << *max_element(dp, dp + 100001);
return 0;
}
Compilation message (stderr)
clo.cpp: In function 'int main()':
clo.cpp:45:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
clo.cpp:46:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen(task".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... |