0-1 Knapsack Problem Statement

You are tasked with determining the maximum profit a thief can earn. The thief is looting a store and can carry a maximum weight 'W' in his knapsack. There are 'N' items, each with a known weight and value. Considering the capacity constraints of the knapsack, calculate the maximum profit possible.

Input:

The first line contains a single integer 'T' representing the number of test cases.
For each test case:
- The first line contains two integers 'N' and 'W', representing the number of items and the maximum weight capacity of the knapsack.
- The second line contains 'N' space-separated integers denoting the weights of the items.
- The third line contains 'N' space-separated integers denoting the values of the items.

Output:

Output a single integer per test case representing the maximum profit the thief can achieve.
Provide each test case's result on a new line.

Example:

Input:
1
4 10
6 1 5 3
3 6 1 4
Output:
13

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 102
  • 1 ≤ W ≤ 102
  • 1 ≤ weights[i] ≤ 50
  • 1 ≤ values[i] ≤ 102

Note: Items cannot be broken and the thief should take the whole item or none.

Be the first one to answer
Add answer anonymously...
Samsung Software Developer 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