#C8224. Taco Flavor Value Maximization
Taco Flavor Value Maximization
Taco Flavor Value Maximization
You are given a set of spices with their respective flavor values and a threshold value M. For each test case, you need to select a subset of spices such that their total flavor value is as high as possible but does not exceed M. This is a typical subset sum problem.
Problem Statement:
Given an integer T representing the number of test cases. For each test case, you are given two integers N and M in the first line where N indicates the number of spices available and M is the maximum allowed flavor value. In the next line, there are N positive integers representing the flavor values of each spice.
Your task is to compute, for each test case, the maximum sum that does not exceed M that can be formed by choosing some of the spices.
Note: Each spice can be used at most once per test case.
The problem can be modeled using dynamic programming with a 0/1 knapsack approach. The optimal solution uses a DP array where dp[j] represents the maximum flavor value achievable with a limit of j without exceeding it.
The mathematical formulation is given by:
[ \text{dp}[j] = \max(\text{dp}[j],, \text{dp}[j - v] + v) \quad \text{for each spice with flavor value } v \text{ and } j \text{ ranging from } M \text{ down to } v. ]
inputFormat
The input begins with an integer T representing the number of test cases, followed by the descriptions of the test cases. Each test case consists of two lines:
- The first line contains two space-separated integers N and M, where N is the number of spices and M is the threshold for the flavor value.
- The second line contains N space-separated positive integers, each representing the flavor value of a spice.
For example:
2 4 10 2 3 5 9 3 7 3 6 4
outputFormat
For each test case, output a single line containing the maximum flavor value that does not exceed M.
For the example input above, the output should be:
10 7## sample
2
4 10
2 3 5 9
3 7
3 6 4
10
7
</p>