#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b ? (a = b) == b : false;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b ? (a = b) == b : false;
}
const int inf = 1e18;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n; cin >> n;
vector<array<int, 4>> vec;
for (int i = 0; i < n; ++i) {
int c, f, v; cin >> c >> f >> v;
vec.push_back({f, 1, v, c});
}
int m; cin >> m;
for (int i = 0; i < m; ++i) {
int c, f, v; cin >> c >> f >> v;
vec.push_back({f, 0, v, c});
}
sort(all(vec));
vector<int> dp(50 * n + 1, -inf);
dp[0] = 0;
while (!vec.empty()) {
auto [C, V] = make_pair(vec.back()[3], vec.back()[2]);
if (vec.back()[1]) {
for (int i = 50 * n - C; i >= 0; --i) {
chmax(dp[i + C], dp[i] - V);
}
} else {
for (int i = C; i <= 50 * n; ++i) {
chmax(dp[i - C], dp[i] + V);
}
}
vec.pop_back();
}
cout << *max_element(all(dp));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
580 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
39 ms |
860 KB |
Output is correct |
6 |
Correct |
39 ms |
860 KB |
Output is correct |
7 |
Correct |
197 ms |
1372 KB |
Output is correct |
8 |
Correct |
208 ms |
1372 KB |
Output is correct |
9 |
Correct |
163 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
580 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
8 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
105 ms |
1116 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
341 ms |
1372 KB |
Output is correct |
6 |
Correct |
362 ms |
1368 KB |
Output is correct |
7 |
Correct |
435 ms |
1372 KB |
Output is correct |
8 |
Correct |
378 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
22 ms |
604 KB |
Output is correct |
4 |
Correct |
73 ms |
860 KB |
Output is correct |
5 |
Correct |
385 ms |
1372 KB |
Output is correct |
6 |
Correct |
377 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
580 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
39 ms |
860 KB |
Output is correct |
14 |
Correct |
39 ms |
860 KB |
Output is correct |
15 |
Correct |
197 ms |
1372 KB |
Output is correct |
16 |
Correct |
208 ms |
1372 KB |
Output is correct |
17 |
Correct |
163 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
5 ms |
580 KB |
Output is correct |
23 |
Correct |
4 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
348 KB |
Output is correct |
25 |
Correct |
8 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
105 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
341 ms |
1372 KB |
Output is correct |
31 |
Correct |
362 ms |
1368 KB |
Output is correct |
32 |
Correct |
435 ms |
1372 KB |
Output is correct |
33 |
Correct |
378 ms |
1368 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
22 ms |
604 KB |
Output is correct |
37 |
Correct |
73 ms |
860 KB |
Output is correct |
38 |
Correct |
385 ms |
1372 KB |
Output is correct |
39 |
Correct |
377 ms |
1368 KB |
Output is correct |
40 |
Correct |
24 ms |
604 KB |
Output is correct |
41 |
Correct |
63 ms |
860 KB |
Output is correct |
42 |
Correct |
207 ms |
1112 KB |
Output is correct |
43 |
Correct |
346 ms |
1372 KB |
Output is correct |
44 |
Correct |
353 ms |
1372 KB |
Output is correct |
45 |
Correct |
338 ms |
1368 KB |
Output is correct |
46 |
Correct |
85 ms |
856 KB |
Output is correct |
47 |
Correct |
178 ms |
1112 KB |
Output is correct |
48 |
Correct |
176 ms |
1116 KB |
Output is correct |