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;
};
ll sf(st a, st b){
return a.f > b.f;
}
ll n, m;
st a[2009];
st b[2009];
ll dp[2000*50];
ll nxt[109][1009];
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);
sort(b, b+m, sf);
for(ll i = 1; i <= 2000*50; ++i)
dp[i] = -inf;
ll i = 0, j = 0;
while(i < n || j < m){
if(i < n && (j == m || a[i].f >= b[j].f)){
for(ll k = 2000*50-a[i].c; k >= 0; --k)
dp[k+a[i].c] = max(dp[k+a[i].c], dp[k]-a[i].v);
++i;
}
else{
for(ll k = b[j].c; k <= 2000*50; ++k)
dp[k-b[j].c] = max(dp[k-b[j].c], dp[k]+b[j].v);
++j;
}
}
ll ans = 0;
for(ll i = 0; i <= 2000*50; ++i)
ans = max(ans, dp[i]);
cout << ans << '\n';
}
Compilation message (stderr)
clo.cpp: In function 'int main()':
clo.cpp:46:15: warning: iteration 99999 invokes undefined behavior [-Waggressive-loop-optimizations]
46 | dp[i] = -inf;
| ~~~~~~^~~~~~
clo.cpp:45:21: note: within this loop
45 | for(ll i = 1; i <= 2000*50; ++i)
| ~~^~~~~~~~~~
# | 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... |