제출 #1290880

#제출 시각아이디문제언어결과실행 시간메모리
1290880ssseulKnapsack (NOI18_knapsack)C++20
100 / 100
69 ms34404 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define el '\n'

const int maxN = 505;
const int base = 311;
const int MOD = 1e9+7;
const int INF = 1e15;
const int NEG_INF = -1e15;
const int MAX_DAYS = 1000;


int n, m, k, q;
string S, T, SS[maxN];
// int dp[maxN][maxN];
vector<int> g[maxN];

void run_test() {
    int limit;
    int type_num;
    std::cin >> limit >> type_num;

    std::map<int, vector<pair<int, int>>> by_weight;
    for (int t = 0; t < type_num; t++) {
        int value;
        int weight;
        int amt;
        std::cin >> value >> weight >> amt;
        if (weight <= limit && amt > 0) { by_weight[weight].push_back({value, amt}); }
    }

    /*
     * best[i][j] contains the most value we can
     * get using j weight and the first i weight types
     */
    vector<vector<long long>> best(by_weight.size() + 1,
                                   vector<long long>(limit + 1, NEG_INF));
    best[0][0] = 0;
    int at = 1;
    for (auto &[w, items] : by_weight) {
        // sort items in reverse order by value
        std::sort(items.begin(), items.end(), std::greater<pair<int, int>>());
        for (int i = 0; i <= limit; i++) {
            best[at][i] = best[at - 1][i];
            int copies = 0;
            int type_at = 0;
            int curr_used = 0;
            long long profit = 0;
            // go through as many items until we run out of items or usable
            // weight
            while ((copies + 1) * w <= i && type_at < items.size()) {
                copies++;
                profit += items[type_at].first;
                if (best[at - 1][i - copies * w] != NEG_INF) {
                    best[at][i] =
                        std::max(best[at][i], best[at - 1][i - copies * w] + profit);
                }

                curr_used++;
                if (curr_used == items[type_at].second) {
                    curr_used = 0;
                    type_at++;
                }
            }
        }
        at++;
    }
    cout << *std::max_element(best.back().begin(), best.back().end()) << endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // freopen("snakes.in", "r", stdin); 
    // freopen("snakes.out", "w", stdout);
    int test = 1;
    // cin >> test;
    while (test-- > 0)
    {
        run_test();
    }
}
#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...