Submission #1268788

#TimeUsernameProblemLanguageResultExecution timeMemory
1268788MinhKienCloud Computing (CEOI18_clo)C++20
54 / 100
1146 ms3636 KiB
#include <algorithm>
#include <iostream>
#include <cstring>

using namespace std;

#define ll long long
const int N = 4010;
const int M = 50 * N;
const ll INF = 1e17;

struct object {
    int room, f;
    ll cost;

    bool operator < (const object &o) const {
        if (f == o.f) return cost > o.cost;
        return f > o.f;
    }
} a[N];

int n, m;
ll dp[2][M];

int main() {
    // #define tst "DATPHONG"
    // freopen(tst".INP", "r", stdin);
    // freopen(tst".OUT", "w", stdout);

    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(false);

    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].room >> a[i].f >> a[i].cost;
        a[i].cost *= -1;
    }

    cin >> m;
    for (int i = 1; i <= m; ++i) {
        cin >> a[n + i].room >> a[n + i].f >> a[n + i].cost;
    }

    n += m;
    sort(a + 1, a + n + 1);

    fill_n(dp[0], M, -INF);
    dp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
        int b = i & 1;
        fill_n(dp[b], M, -INF);
        
        if (a[i].cost < 0) {
            for (int j = 0; j < M; ++j) {
                if (j + a[i].room < M && dp[b ^ 1][j] != -INF) {
                    dp[b][j + a[i].room] = max(dp[b ^ 1][j] + a[i].cost, dp[b][j + a[i].room]);
                }
                dp[b][j] = max(dp[b][j], dp[b ^ 1][j]);
            }
        } else {
            for (int j = 0; j < M; ++j) {
                dp[b][j] = max(dp[b ^ 1][j], dp[b][j]);
                if (j >= a[i].room && dp[b ^ 1][j] != INF) {
                    dp[b][j - a[i].room] = max(dp[b][j - a[i].room], dp[b ^ 1][j] + a[i].cost);
                }
            }
        }
    }

    ll ans = 0;
    for (int i = 0; i < M; ++i) {
        ans = max(ans, dp[n & 1][i]);
    }
    cout << ans << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...