There are items, numbered
. For each
, item
has a weight of
and a value of
.
Taro has decided to choose some of the items and carry them home in a knapsack. The capacity of the knapsack is
, which means that the sum of the weights of items taken must be at most
.
Find the maximum possible sum of the values of items that Taro takes home.
Constraints
- All values in input are integers.
Input Specification
The first line of input will contain 2 space separated integers, and
.
The next lines will contain 2 space separated integers,
and
, the weight and value of item
.
Output Specification
You are to output a single integer, the maximum possible sum of the values of items that Taro takes home.
Sample Input 1
3 8
3 30
4 50
5 60
Sample Output 1
90
Sample Input 2
5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
Sample Output 2
5000000000
Sample Input 3
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Sample Output 3
17
Sample Explanations
For the first sample, items and
should be taken. Then, the sum of the weights is
, and the sum of the values is
.
For the second sample, it is important to note that the answer may not fit in a 32-bit integer type.
For the third sample, items ,
, and
should be taken. Then, the sum of the weights is
, and the sum of the values is
.
Comments
Can someone have a look at my code please? I'm getting 62 test cases right, then a WA
try this:
correct:
thanks
Does anyone know why I am getting std:bad_alloc on a few cases? My code works in my IDE without any errors. Any help would be very appreciated.
This comment is hidden due to too much negative feedback. Show it anyway.
The cause of
-bit integers can take up to
MB of storage. In addition,
MB of storage in the worst case.
std::bad_alloc
is unrelated to where the vector is located (in general, only fixed sized arrays have issues when allocated locally). The issue is that your code is exceeding the memory limit for this problem. A two dimensional vector of sizevector::push_back
can result in the actual capacity of the vector being up to twice as large as the actual size (assumingvector::reserve
is not called), thus resulting inYou should aim for a solution that uses
memory.