Dsa
Greedy
Medium
Grocery Bag Filling
You own a grocery store and have a certain capacity to fill bags with items from your inventory. Each item has a weight and a value associated with it. You want to maximize the total value of items in the bags, under the constraint of weight capacity. Write a program that reads the weight limit of the bags and the list of items with their weights and values, and calculates the maximum value that can be packed into the bags.
Input format:
- The first line contains two integers W (1 ≤ W ≤ 5000) and N (1 ≤ N ≤ 1000), representing the weight capacity of the bags and the number of items respectively.
- The next N lines each contain two integers Wt (1 ≤ Wt ≤ 2000) and V (1 ≤ V ≤ 100), representing the weight and value of each item.
Output format:
- A single integer representing the maximum total value possible.
Example:
Input:
10 3
5 10
3 15
4 7
Output:
22
Key concepts
greedyshoppingmaximization
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now