Educational DP Contest AtCoder D - Knapsack 1

View as PDF

Submit solution

Points: 7
Time limit: 0.25s
Python 1.0s
Memory limit: 1G

Problem type

There are N items, numbered 1, 2, \dots, N. For each i (1 \le i \le N), item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

  • All values in input are integers.
  • 1 \le N \le 100
  • 1 \le W \le 10^5
  • 1 \le w_i \le W
  • 1 \le v_i \le 10^9

Input Specification

The first line of input will contain 2 space separated integers, N and W.

The next N lines will contain 2 space separated integers, w_i and v_i, the weight and value of item i.

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 1 and 3 should be taken. Then, the sum of the weights is 3+5 = 8, and the sum of the values is 30+60 = 90.

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 2, 4, and 5 should be taken. Then, the sum of the weights is 5+6+3 = 14, and the sum of the values is 6+6+5 = 17.


Comments


  • 1
    lwale1  commented on June 13, 2022, 7:00 p.m.

    Can someone have a look at my code please? I'm getting 62 test cases right, then a WA


    • 1
      andy_zhu23  commented on June 13, 2022, 10:26 p.m.

      try this:

      1 1
      1 1

      correct:

      1

      • 0
        lwale1  commented on June 14, 2022, 2:15 p.m.

        thanks


  • 5
    OneYearOld  commented on July 9, 2020, 2:34 p.m.

    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.


    • -6
      maxcruickshanks  commented on July 9, 2020, 7:11 p.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


      • 10
        wleung_bvg  commented on July 9, 2020, 7:37 p.m.

        The cause of 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 size N \times W 64-bit integers can take up to 80 MB of storage. In addition, vector::push_back can result in the actual capacity of the vector being up to twice as large as the actual size (assuming vector::reserve is not called), thus resulting in 160 MB of storage in the worst case.

        You should aim for a solution that uses \mathcal{O}(N + W) memory.