Submission #1312436

#TimeUsernameProblemLanguageResultExecution timeMemory
1312436hynmjCloud Computing (CEOI18_clo)C++20
100 / 100
358 ms2268 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long N = 100000;
const long long neg = -1e18;
int a[N];
int dp[N + N];
struct pc
{
    int cores, freq, price;
    bool isorder;
};
bool comp(pc a, pc b)
{
    if (a.freq != b.freq)
        return a.freq > b.freq;
    return !a.isorder;
}
void solve()
{
    int n, m;
    cin >> n;
    vector<pc> computers;
    pc spare;
    spare.isorder = 0;
    for (int i = 0; i < n; i++)
    {

        cin >> spare.cores >> spare.freq >> spare.price;
        computers.push_back(spare);
    }
    cin >> m;

    pc order;
    order.isorder = 1;
    for (int i = 0; i < m; i++)
    {
        cin >> order.cores >> order.freq >> order.price;
        computers.push_back(order);
    }

    sort(computers.begin(), computers.end(), comp);

    fill(dp, dp + N + N, -1e18);

    dp[0] = 0;

    for (auto &machine : computers)
    {
        int c = machine.cores;
        int v = machine.price;
        // our next speed but previous faster cores can be used here
        if (machine.isorder)
        {
            for (int i = 0; i + c <= N; i++)
            {
                // we are getting v to sell c cores
                if (dp[i + c] != -1e18)
                    dp[i] = max(dp[i], dp[i + c] + v);
            }
        }
        else
        {
            // we are adding more cores of current frequency
            // but obviously previous ones can be used too here
            
            // we are paying v to get c cores
            for (int i = N; i >= c; i--)
            {
                if (dp[i - c] != -1e18)
                    dp[i] = max(dp[i], dp[i - c] - v);
            }
        }
    }

    int ans = 0;
    for (int i = 0; i <= 10000; i++)
    {
        ans = max(ans, dp[i]);
        // cout << ans << ' ';
    }
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++)
    {
        // cout << "Case #" << i << ':' << ' ';
        solve();
        cout << endl;
    }
    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...