Skip to main content

Unbounded Knapsack

1. You are given a number n, representing the count of items.

2. You are given n numbers, representing the values of n items.

3. You are given n numbers, representing the weights of n items.

3. You are given a number "cap", which is the capacity of a bag you've.

4. You are required to calculate and print the maximum value that can be created in the bag without 

    overflowing it's capacity.

Note: Each item can be taken any number of times. You are allowed to put the same item again 

                  and again.

Input Format

A number n

v1 v2 .. n number of elements

w1 w2 .. n number of elements

A number cap

Output Format

A number representing the maximum value that can be created in the bag without overflowing it's capacity


Constraints

1 <= n <= 20

0 <= v1, v2, .. n elements <= 50

0 < w1, w2, .. n elements <= 10

0 < cap <= 10

Sample Input

5

15 14 10 45 30

2 5 1 3 4

7

Sample Output

100


Solution:

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int val[] = new int[n],wt[] = new int[n];
            for(int i=0;i<n;i++)
                val[i] = sc.nextInt();
            for(int i=0;i<n;i++)
                wt[i] = sc.nextInt();
            int cap = sc.nextInt();
            
            int[] dp = new int[cap+1];
            dp[0] = 0;
            
            for(int i=1;i<=cap;i++){
                int cost = 0;
                for(int j=0;j<n;j++){
                    if(wt[j]<=i){
                        cost = Math.max(cost, val[j]+dp[i-wt[j]]);
                    }
                }
                dp[i] = cost;
            }
            
            System.out.println(dp[cap]);
    }
}

Comments

Must Read:

Data Formats ( XML & JSON ) XML AND JSON XML AND JSON | Quiz 2 & 3

Data Formats ( XML & JSON )  XML AND JSON  XML AND JSON Quiz 2 & 3  

Programming using Java Hands On - Control Structures | Print Customer Details

Print Customer Details Help Mr.Ben who is a programmer, in developing a registration form on console. Customers will not just input data, but also view the details once he/she finishes the registration.  Sample Input 1: Enter your name:Jany Enter age:25 Enter gender:Female Hailing from:Mexico Sample Output 1: Welcome, Jany Age:25 Gender:Female City:Mexico Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ | 5 tests run/ 5 tests passed | +------------------------------+

Data Formats ( XML & JSON ) XML AND JSON | Pre-Quiz

Data Formats ( XML & JSON )  XML AND JSON  Pre-Quiz  

RDBMS Data Definition Language | Drop Mobile Specification table (parent)

  RDBMS  Data Definition Language  Drop Mobile Specification table (parent) Refer the following schema and drop Mobile Specification table. (Hint: To drop parent table all associate tables need to be dropped or Use 'cascade constraints' command) Evaluation Result: Result Description Summary of tests +------------------------------+ | 1 tests run / 1 test passed | +------------------------------+

UNIX Introduction to Unix

  Calendar1 Display the previous, current and next month calendar of the  year 1987 august Calendar2 Display and see what is the specialty of September 1752. Calendar 5 Write a command to display the previous, current and next month calendar of the year 2015 December.

Min Cost In Maze Traversal

1. You are given a number n, representing the number of rows. 2. You are given a number m, representing the number of columns. 3. You are given n*m numbers, representing elements of 2d array a, which represents a maze. 4. You are standing in top-left cell and are required to move to bottom-right cell. 5. You are allowed to move 1 cell right (h move) or 1 cell down (v move) in 1 motion. 6. Each cell has a value that will have to be paid to enter that cell (even for the top-left and bottom-       right cell). 7. You are required to traverse through the matrix and print the cost of path which is least costly. Input Format A number n A number m e11 e12.. e21 e22.. .. n * m number of elements Output Format The cost of least costly path. Constraints 1 <= n <= 10^2 1 <= m <= 10^2 0 <= e1, e2, .. n * m elements <= 1000 Sample Input 6 6 0 1 4 2 8 2 4 3 6 5 0 4 1 2 4 1 4 6 2 0 7 3 2 2 3 1 5 9 2 4 2 7 0 8 5 1 Sample Output 23 Solution: import java.io.*; import...

Programming using Java Hands On - Control Structures | Check for Leap Year

  Given a year, check if the year is leap year or not.  If yes, the output should be “Leap Year”.  Else output should be “Not a Leap Year”.  The input should be a positive four digit number.  Else,  the output should be “Invalid Year”. Sample Input  1 : Enter the Year 2016 Sample Output  1 : Leap Year Sample Input  2 : Enter the Year 2001 Sample Output  2 : Not a Leap Year Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ | 12 tests run/12 tests passed | +------------------------------+

Programming using Java Hands On - Control Structures | Bill Generation

Bill Generation Tom went to a movie with his friends in a multiplex theatre and during break time he bought pizzas, puffs and cool drinks.  Consider   the following prices :  Rs.100/pizza Rs.20/puffs Rs.10/cooldrink Generate a bill for What Tom has bought. Sample Input 1: Enter the no of pizzas bought:10 Enter the no of puffs bought:12 Enter the no of cool drinks bought:5 Sample Output 1: Bill Details No of pizzas:10 No of puffs:12 No of cooldrinks:5 Total price=1290 ENJOY THE SHOW!!! Result Description Summary of tests *Note: All the test cases might not have same weightage +------------------------------+ | 6 tests run/ 6 tests passed | +------------------------------+

Software Engineering Concepts Software Maintenance Test Your Understanding

  Software Engineering Concepts       Software Maintenance            Test Your Understanding

Subscribe to Get's Answer by Email