#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
struct event {
int c, f, v, flag;
} items[5010];
const int INF = 1e18;
int N, M, st, ans;
int dp[10010];
signed main() {
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> items[i].c >> items[i].f >> items[i].v;
items[i].flag = 1;
}
cin >> M;
for (int i = N + 1; i <= N + M; ++i) {
cin >> items[i].c >> items[i].f >> items[i].v;
items[i].flag = 0;
}
sort(items + 1, items + 1 + N + M, [](event a, event b) {
if (a.f == b.f) return a.flag > b.flag;
return a.f > b.f;
});
for (int j = 0; j <= 10010; ++j) dp[j] = -INF;
for (int i = 1; i <= N + M; ++i) {
if (items[i].flag) {
st = i;
break;
}
}
for (int i = st; i <= N + M; ++i) {
vector<int> new_dp(10010, -INF);
if (items[i].flag) new_dp[items[i].c] = max(dp[0], -items[i].v);
for (int j = 0; j <= 10000; ++j) {
new_dp[j] = max(dp[j], new_dp[j]);
if (items[i].flag) {
if (j >= items[i].c) new_dp[j] = max(new_dp[j], dp[j - items[i].c] - items[i].v);
} else {
new_dp[j] = max(new_dp[j], dp[j + items[i].c] + items[i].v);
ans = max(ans, new_dp[j]);
}
}
for (int j = 0; j <= 10000; ++j) dp[j] = new_dp[j];
}
cout << ans;
return 0;
}
Compilation message
clo.cpp: In function 'int main()':
clo.cpp:40:44: warning: iteration 10010 invokes undefined behavior [-Waggressive-loop-optimizations]
40 | for (int j = 0; j <= 10010; ++j) dp[j] = -INF;
| ~~~~~~^~~~~~
clo.cpp:40:23: note: within this loop
40 | for (int j = 0; j <= 10010; ++j) dp[j] = -INF;
| ~~^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Incorrect |
34 ms |
508 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |