제출 #1101822

#제출 시각아이디문제언어결과실행 시간메모리
1101822bozocodeKnapsack (NOI18_knapsack)C++14
73 / 100
188 ms262144 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
int main() {
    cin.tie(0) -> sync_with_stdio(0);
    int S, N; cin >> S >> N;
    vector<vector<ll>> value(N + 1, vector<ll>(S + 1, -1));
    //O(SN) works, however the tricky part is that we have a ton of the same item
    //let us not try to optimize for memory first, just time
    //i think we have no need to optimize memory
    value[0][0] = 0;
    for (int i = 0; i < N; i++) {
        int v, w, c; cin >> v >> w >> c;  //value, weight, copies
        //there are w distinct modulos that we can iterate over
        //to cover every single value of 0... S
        for (int j = 0; j < S + 1; j++) {
            value[i + 1][j] = max(value[i + 1][j], value[i][j]);
        }
        for (int j = 0; j < w; j++) {  //the mod in question
            //for every value within this mod
            //we want to set value[i + 1][k + w]
            //to be the max of previous mods within c - 1 of it i think
            //we should probably do this with multiset.. as i was doing...
            multiset<ll> ms;  //attempt 2
            for (int k = j; k < S - w + 1; k += w) {
                int idx = (k - j) / w;
                // cout << k << "\n";
                // cout << ms.size() << "\n";
                if (value[i][k] != -1) { ms.insert(value[i][k] - idx * v); }
                // cout << ms.size() << "\n";
                //now what
                //erase idx - c
                if (idx >= c) {
                    //we can't be erasing 0 every time bruh this is stupid
                    //
                    int oidx = k - (c) * w;
                    int bruh = (oidx - j) / w;

                    // cout << oidx << endl;
                    // cout << value[i][oidx] << endl;
                    // cout << *ms.begin() << endl;
                    // cout << value[i][oidx] - bruh * v << endl;
                    // cout << endl;
                    // cout << "erasing: " << value[i][oidx] - bruh * v << "\n";
                    if (ms.find(value[i][oidx] - bruh * v) != ms.end()) {
                        ms.erase(ms.find(value[i][oidx] - bruh * v));
                    }
                }
                if (ms.empty()) { continue; }
                ll best = *ms.rbegin();
                // cout << best << endl;
                value[i + 1][k + w] = max(value[i + 1][k + w], best + (idx + 1) * v);
            }
        }
        // cout << "\n";
    }
    ll ans = 0;
    // for (int i = 0; i < N + 1; i++) {
    //     for (int j = 0; j < S; j++) {
    //         cout << value[i][j] << " ";
    //     }
    //     cout << "\n";
    // }
    for (int i = 0; i < S + 1; i++) {
        ans = max(value[N][i], ans);
    }
    cout << ans << "\n";
}
#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...