Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run time error issue #617

Open
namanmuktha opened this issue Jul 31, 2024 · 2 comments
Open

Run time error issue #617

namanmuktha opened this issue Jul 31, 2024 · 2 comments

Comments

@namanmuktha
Copy link

I am experiencing a runtime error while solving knapsack2 problem from Atcoder
Soln : https://github.com/namanmuktha/atcoderdp/blob/main/knapsacktabulation.cpp

@charann29
Copy link
Owner

The runtime error in your code could be due to the use of the vector type for the w (weights) and p (profits) vectors while using vector<vector> for the dp table, where ll is a typedef for long long. If the input values for weights or profits are large, they might not fit into the int data type, causing issues when accessing elements.

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
    ll n, W;
    cin >> n >> W;
    vector<ll> w(n); // Change int to ll
    vector<ll> p(n); // Change int to ll

    for (ll i = 0; i < n; i++) {
        cin >> w[i];
        cin >> p[i];
    }

    vector<vector<ll>> dp(n + 1, vector<ll>(W + 1, 0));

    for (ll i = 1; i <= n; i++) {
        for (ll j = 0; j <= W; j++) {
            ll nottaken = dp[i - 1][j];
            ll taken = -1e9;
            if (w[i - 1] <= j) {
                taken = dp[i - 1][j - w[i - 1]] + p[i - 1];
            }
            dp[i][j] = max(nottaken, taken);
        }
    }

    cout << dp[n][W] << endl;
    return 0;
}

@charann29
Copy link
Owner

update me on this..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants