Submission #1171706

#TimeUsernameProblemLanguageResultExecution timeMemory
1171706abcsedafaefKnapsack (NOI18_knapsack)C++20
12 / 100
0 ms328 KiB
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL)

void solve() {
    int x, n;
    cin >> x >> n;
    vi v_temp, c_temp, t_temp;
    map<int, int> myMap;
    
    for (int i = 0; i < n; i++) {
        int value, weight, amt;
        cin >> value >> weight >> amt;
        if (weight <= x && amt > 0) {
            if (myMap[weight] * weight > x) continue; 
            myMap[weight] += amt;
            v_temp.push_back(value);
            c_temp.push_back(weight);
            t_temp.push_back(amt);
        }
    }
    
    n = v_temp.size();
    if (n == 0) {
        cout << 0;
        return;
    }

    vi v = v_temp, c = c_temp, t = t_temp, new_t;
    for (int i = 0; i < n; i++) {
        new_t.push_back(min(t[i], x / c[i]));
    }

    vector<vi> dp(n + 1, vi(x + 1, 0));
    
    for (int i = n - 1; i >= 0; i--) {
        for (int j = 0; j <= x; j++) {
            int skip = dp[i + 1][j];
            dp[i][j] = skip;
            for (int k = 1; k <= new_t[i]; k++) {
                if (j - k * c[i] >= 0) {
                    int pick = k * v[i] + dp[i + 1][j - k * c[i]];
                    dp[i][j] = max(dp[i][j], pick);
                }
            }
        }
    }
    cout << dp[0][x];
}

int main() {
    fast_io;
    solve();
    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...