Submission #1172334

#TimeUsernameProblemLanguageResultExecution timeMemory
1172334dbekarysKnapsack (NOI18_knapsack)C++20
37 / 100
114 ms195624 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1e4 + 7;

int dp[2000][N];

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int S, N;
    cin >> S >> N;

    vector<int> weights, values;

    for (int i = 0; i < N; i++) {
        int V, W, K;
        cin >> V >> W >> K;
        for (int j = 0; j < K; j++) { // Split items into individual instances
            weights.push_back(W);
            values.push_back(V);
        }
    }

    int totalItems = weights.size();

    // Properly initialize the dp array
    memset(dp, 0, sizeof(dp));

    // Knapsack DP
    for (int i = 1; i <= totalItems; i++) {
        for (int j = 0; j <= S; j++) {
            dp[i][j] = dp[i - 1][j]; // Not taking the current item
            if (weights[i - 1] <= j) {
                dp[i][j] = max(dp[i][j], dp[i - 1][j - weights[i - 1]] + values[i - 1]);
            }
        }
    }

    cout << dp[totalItems][S] << 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...