EIUKNAPSACK3 - KNAPSACK - 0/1 Knapsack Problem
Xem dạng PDF
Gửi bài giải
Điểm:
1,00 (OI)
Giới hạn thời gian:
1.0s
Giới hạn bộ nhớ:
1G
Input:
stdin
Output:
stdout
Nguồn bài:
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch
You are given ~N~ items, each with a weight and a value. You have a knapsack with weight capacity ~W~. Your goal is to select a subset of items such that:
- The total weight does not exceed ~W~
- The total value is maximized
Output the indices of the selected items (in any order).
Input Format
The first line contains two integers ~N~ and ~W~:
- ~N~ ~(1 ≤ N ≤ 2000)~: number of items
- ~W~ ~(1 ≤ W ≤ 50000)~: maximum weight capacity
The next ~N~ lines contain two integers each:
- ~w_i~ ~v_i~: weight and value of the i-th item ~(1 ≤ w_i ≤ W, 1 ≤ v_i ≤ 10^4)~
Output Format
- The first line contains a single integer: the maximum total value that can be achieved.
- Each subsequent line contains the indices of one valid optimal solution, with indices separated by spaces.
- You may output up to 100 different optimal solutions.
- Items are numbered from ~1~ to ~N~. If no items can be selected, print
0on the first line and leave the second line empty.
Example
Input
4 5
2 10
3 10
2 10
3 10
Output
20
1 2
3 4
Notes
- Multiple optimal solutions may exist. You may output up to 100 of them, each on a separate line.
- Any valid optimal solution will be accepted.
- The order of indices within each line does not matter.
- Ensure that the total weight of each solution does not exceed the capacity ~W~.
Constraints
- Time limit: 1 second
- Memory limit: 1GB
- ~1 ≤ N ≤ 2000~
- ~1 ≤ W ≤ 50000~
- ~1 ≤ w_i ≤ W~
- ~1 ≤ v_i ≤ 10^4~
Bình luận