Colorful Knapsack Problem
You are given a set of 'N' stones, each with a specific weight and color. The goal is to fill a knapsack with exactly 'M' stones, choosing one stone of each color, so that the total weight does not exceed a given capacity 'X'. The objective is to minimize the unused capacity of the knapsack.
Input:
The first line contains three integers 'N', 'M', and 'X', separated by spaces, where:
- N is the total number of stones,
- M is the number of distinct colors,
- X is the total weight capacity of the knapsack.
The second line contains N integers representing the weights of the stones: W[1], W[2], ..., W[N].
The third line contains N integers representing the colors of the stones: C[1], C[2], ..., C[N].
Output:
Output a single integer: the minimum unused capacity of the knapsack. If the knapsack cannot be filled under the given conditions, output -1.
Example:
Assume the following input:
5 3 10
4 5 3 8 2
1 2 3 3 1
The output should be:
1
Constraints:
- 1 <= M <= N <= 100
- 1 <= X <= 10000
- 1 <= W[i] <= 100
- 1 <= C[i] <= M
- Time Limit: 1 sec
Note:
Focus on minimizing the unused capacity after selecting one stone of each color.

AnswerBot
4mo
The goal is to fill a knapsack with exactly 'M' stones, choosing one stone of each color, minimizing the unused capacity.
Use dynamic programming to solve this problem efficiently.
Create a 2D array to ...read more
Help your peers!
Add answer anonymously...
Dell Software Developer interview questions & answers
A Software Developer was asked 10mo agoQ. What was your experience with Java?
A Software Developer was asked 11mo agoQ. Given two strings, check if one is a substring of the other.
A Software Developer was asked 11mo agoQ. How do you find the smallest subset in a string?
Popular interview questions of Software Developer
A Software Developer was asked 4mo agoQ1. What technologies are you familiar with?
A Software Developer was asked 10mo agoQ2. What was your experience with Java?
A Software Developer was asked 11mo agoQ3. Given two strings, check if one is a substring of the other.
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

