Submission #974845

#TimeUsernameProblemLanguageResultExecution timeMemory
974845vjudge1Knapsack (NOI18_knapsack)C++98
0 / 100
1041 ms508 KiB
#include <bits/stdc++.h>
using namespace std;

// A utility function that returns
// maximum of two integers

// Returns the maximum value that
// can be put in a knapsack of capacity W
int knapSack(int W, int wt[], int val[], int n)
{

    // Base Case
    if (n == 0 || W == 0)
        return 0;

    // If weight of the nth item is more
    // than Knapsack capacity W, then
    // this item cannot be included
    // in the optimal solution
    if (wt[n - 1] > W)
        return knapSack(W, wt, val, n - 1);

    // Return the maximum of two cases:
    // (1) nth item included
    // (2) not included
    else
        return max(
            val[n - 1]
                + knapSack(W - wt[n - 1], wt, val, n - 1),
            knapSack(W, wt, val, n - 1));
}

// Driver code
int main()
{
    int W;
    int n;
    cin>>W>>n;
    int v[n];
    int we[n];
    for (int i = 0; i < n; i++)
    {
        int x;
        cin>>v[i];
        cin>>we[i];
        cin>>x;
    }
    
    cout << knapSack(W, we, v, n);
    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...