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;
// Macros
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define up(v) upper_bound(v)
#define low(v) lower_bound(v)
// Read and Print
#define read(a, n) for(ll i = 0; i < n; ++i) cin >> a[i];
#define print(a, n) for(ll i = 0; i < n; ++i){ cout << a[i] << " ";} cout << "\n";
#define endl "\n"
#define sp " "
// Typedefs
typedef long long ll;
typedef long double ld;
typedef long long int lli;
typedef pair<ll, ll> pll;
typedef pair<lli, lli> plli;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vector<lli>> vvi;
using vec = vector<int>;
// Constants
const ll mxn = 1e6 + 5;
const ll mod = 1e9 + 7;
// Solve
const ll N = 1e5 + 5;
const ll INF = 1e18 + 6;
void solve() {
ll n, m, sum = 0;
vector <vector<ll>> a;
cin >> n;
for (ll i = 1; i <= n; i++) {
ll c, f, p;
cin >> c >> f >> p;
a.pb({c, f, -p});
sum += c;
}
cin >> m;
for (ll j = 0; j < m; j++) {
ll c, f, p;
cin >> c >> f >> p;
a.pb({ -c, f, p});
}
vector<vi> dp(sum + 1, vi(2, 0));
for (int i = 1; i <= sum; i++) dp[i][0] = -INF;
auto comp = [&](vector<ll> &a, vector<ll> &b) ->bool{
return a[1] != b[1] ? a[1] > b[1] : a[2] < b[2];
};
sort(all(a), comp);
for (ll i = 0; i < a.size(); i++) {
for (ll j = 0; j <= sum; j++)
dp[j][1] = dp[j][0];
for (ll j = 0; j <= sum; j++) {
ll pre = j - a[i][0];
if (pre >= 0 && pre <= sum && dp[pre][0] != -INF)
dp[j][1] = max(dp[j][1], dp[pre][0] + a[i][2]);
}
for (ll j = 0; j <= sum; j++)
dp[j][0] = dp[j][1];
}
ll ans = -INF;
for (ll i = 0; i <= sum; i++)
ans = max(ans, dp[i][0]);
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
Compilation message (stderr)
clo.cpp: In function 'void solve()':
clo.cpp:68:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (ll i = 0; i < a.size(); 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... |