solveNP

0-1 Knapsack Problem

The 0-1 knapsack problem is a classic optimization problem. Imagine you are a burglar with a knapsack that can carry a limited amount of weight. You break into a store with a variety of items, each with its own value and weight. Your goal is to choose a combination of items to put in your knapsack that maximizes the total value, without exceeding the knapsack's weight limit.

The "0-1" part of the name comes from the fact that for each item, you have two choices: either take it (1) or leave it (0). You cannot take a fraction of an item.

Input

The input consists of the weight limit of the knapsack and the items.

Output

The output represents the subset of picked items. It must be a space-separated list of indices of the items (zero indexed) in the input file.

Validation and Scoring

The goal is to maximize the total value of the items in the knapsack. The solution will be considered valid if it meets the following criteria:

Example

Consider the following input:

32

2 6
8 9
10 13
6 11
6 10
4 9
3 6
3 4
15 16
7 8

This means the knapsack has a weight limit of 32. There are 10 items:

One way to fill up the knapsack would be picking the items 1 3 6 8. The score would be 42.

We can do even better with 0 2 3 4 5 6. That would improve the score up to 55. Note, this way the knapsack is not fully loaded.


Sample input

32

2 6
8 9
10 13
6 11
6 10
4 9
3 6
3 4
15 16
7 8


Inputs

small_normal.txt 721B
small_bimodal.txt 807B
medium_normal.txt 7.11KB
medium_bimodal.txt 8.10KB
large_normal.txt 71KB
large_bimodal.txt 80KB
extra_large_normal.txt 715KB
extra_large_bimodal.txt 809KB

[Log in] to test and submit a solution.