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;
const int N = 2e5 + 17;
int n, m, sz, ti;
long long z[N], ans;
struct cpt
{
    int c, f, v, t;
} com[N];
bool cmp (cpt x, cpt y)
{
    if (x.f != y.f)
    {
        return x.f > y.f;
    }
    return x.t < y.t;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1, c, f, v; i <= n; ++i)
    {
        cin >> c >> f >> v;
        com[++sz] = {c, f, v, -1};
    }
    cin >> m;
    for (int i = 1, c, f, v; i <= m; ++i)
    {
        cin >> c >> f >> v;
        com[++sz] = {c, f, v, 1};
    }
    sort (com + 1, com + sz + 1, cmp);
    fill (z + 1, z + N / 2, -1e18);
    for (int i = 1; i <= sz; ++i)
    {
        auto [c, f, v, t] = com[i];
        if (t < 0)
        {
            for (int i = ti; i >= 0; --i)
            {
                z[i + c] = max (z[i + c], z[i] - v);
            }
            ti += c;
            continue;
        }
        for (int i = c; i <= ti; ++i)
        {
            z[i - c] = max (z[i - c], z[i] + v);
            ans = max (ans, z[i - c]);
        }
    }
    cout << ans;
}
| # | 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... |