Colourful Knapsack Problem Statement

You are given N stones labeled from 1 to N. The i-th stone has the weight W[i]. There are M colors labeled by integers from 1 to M. The i-th stone has the color C[i] which is an integer between 1 to M, both inclusive.

Your task is to fill a Knapsack with these stones. The Knapsack can hold a total weight of X. You need to choose exactly M stones, one of each color. The total weight of the stones should not exceed X. Since the Knapsack has a capacity X, you should aim to fill it as much as possible — minimizing the unused capacity.

Input:

 The first line contains three integers 'N', 'M', and 'X', where 'N' represents the total number of stones, 'M' represents the total number of color stones, and 'X' represents the total weight capacity of the knapsack.  The second line contains 'N' integers, W[1], W[2], ... W[N], representing the weights of the stones.  The third line contains 'N' integers C[1], C[2], ... C[N], representing the colors of the stones. 

Output:

 Print the minimum unused capacity of the Knapsack as a single integer. If there is no way to fill the Knapsack, print -1. 

Example:

Input:
N = 5, M = 3, X = 10
W = [1, 3, 3, 5, 5]
C = [1, 2, 3, 1, 2]
Output:
1

Constraints:

  • 1 <= M <= N <= 100
  • 1 <= X <= 10000
  • 1 <= W[i] <= 100
  • 1 <= C[i] <= M
Note:

The time limit for this problem is 1 second.

AnswerBot
2d

The task is to fill a Knapsack with stones of different colors and weights, minimizing unused capacity.

  • Given N stones with weights W and colors C, choose M stones (one of each color) to fill Knapsack ...read more

Help your peers!
Add answer anonymously...
Adobe Product Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter