ClassicProblem
SRM 674 · 2015-11-03 · by cgy4ever
Problem Statement
There are n types of items. The types are numbered 0 through n-1, inclusive. For each valid i, you have cnt[i] items of type i, and each of these items has weight w[i] and value v[i]. You are given the
Find a subset of the available items such that:
- the total weight of the selected items is smaller than or equal to limit
- the total value of the selected items is as large as possible
Constraints
- cnt will contain between 1 and 80 elements, inclusive.
- cnt, w and v will contain the same number of elements.
- Each element in cnt will be between 1 and 1,000,000,000, inclusive.
- Each element in w will be between 1 and 80, inclusive.
- Each element in v will be between 1 and 1,000,000,000, inclusive.
- limit will be between 1 and 1,000,000,000, inclusive.
{100,100}
{2,3}
{3,5}
6
Returns: 10
You have two types of items. Items of type 0 have weight 2 and value 3. Items of type 1 have weight 3 and value 5. You have 100 items of each type. The weight limit is 6. The best solution is to take two items of type 1. The total value will be 5 + 5 = 10.
{100,100}
{2,3}
{3,5}
5
Returns: 8
We have the same items as in Example 0, but now the weight limit is only 5. In this setting the best solution is to take one item of each type. The total value will be 3 + 5 = 8.
{100,102}
{2,3}
{3,5}
1000000000
Returns: 810
Again we have the same 200 items. This time, the weight limit is 10^9 and the optimal solution is to take all 200 items.
{100,100}
{2,3}
{3,5}
1
Returns: 0
We can't take anything.
{1,2,3,4,5,6,7,8}
{4,2,6,7,5,8,3,1}
{3,6,4,1,2,8,5,7}
15
Returns: 73
{1000000000}
{1}
{1000000000}
1000000000
Returns: 1000000000000000000
Note that the answer can be very large.
Submissions are judged against all 168 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ClassicProblem with a public method long long maximalValue(vector<int> cnt, vector<int> w, vector<int> v, int limit) · 168 test cases · 2 s / 256 MB per case