- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find maximum price we can get by holding items into a bag in Python

Suppose we have two lists of numbers. One is called weights and another is called values. These are of same length, We also have two values called capacity and count. Here weights[i] and values[i] represent the weight and value of the ith item. We can hold at most capacity weight and at most count items in total, and we can take only one copy of each item, so we have to find the maximum amount of value we can get.

So, if the input is like weights = [2, 2, 4, 6] values = [15, 15, 20, 35] capacity = 8 count = 3, then the output will be 50, as we can select first 3 items, since the total weight is 8.

To solve this, we will follow these steps −

items := a list of pairs by taking weights and values

Define a function dp() . This will take i, cp, ct

if i is same as size of items or ct is same as 0, then

return 0.0

(w, v) := items[i]

ans := dp(i + 1, cp, ct)

if cp >= w, then

ans := maximum of ans, dp(i + 1, cp - w, ct - 1) + v

return ans

From the main method return dp(0, capacity, count)

## Example

Let us see the following implementation to get a better understanding −

class Solution: def solve(self, weights, values, capacity, count): items = list(zip(weights, values)) def dp(i, cp, ct): if i == len(items) or ct == 0: return 0.0 w, v = items[i] ans = dp(i + 1, cp, ct) if cp >= w: ans = max(ans, dp(i + 1, cp - w, ct - 1) + v) return ans return int(dp(0, capacity, count)) ob = Solution() weights = [2, 2, 4, 6] values = [15, 15, 20, 35] capacity = 8 count = 3 print(ob.solve(weights, values, capacity, count))

## Input

[2, 2, 4, 6], [15, 15, 20, 35], 8, 3

## Output

50

- Related Questions & Answers
- Program to find maximum profit we can make by holding and selling profit in Python
- Program to find maximum amount we can get by taking different items within the capacity in Python
- Program to find maximum credit we can get by finishing some assignments in python
- Program to find maximum profit we can get by buying and selling stocks with a fee in Python?
- Program to find maximum number of coins we can get using Python
- Program to find maximum score we can get in jump game in Python
- Program to find maximum value we can get in knapsack problem by taking multiple copies in Python
- Program to find the maximum profit we can get by buying on stock market once in Python
- Program to find the maximum profit we can get by buying on stock market multiple times in Python
- Program to find maximum coins we can get from disappearing coins matrix in Python
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum number of coins we can collect in Python
- Program to find minimum limit of balls in a bag in Python
- Program to find maximum score by splitting binary strings into two parts in Python
- Program to find maximum how many water bottles we can drink in Python