An exact knapsack problem online calculator solves the 0/1 model by finding the highest-value subset of indivisible items that fits within one whole-number capacity.
The Knapsack Problem Calculator is built for this single-container case. It runs the calculation in your browser and returns the selected item labels, total value, total weight, and unused capacity so you can inspect the answer rather than rely on a final number alone.

What the result represents
The calculator solves the classic 0/1 knapsack problem: maximize the sum of selected values while keeping the sum of selected weights at or below the available capacity. Each listed item may be selected once or excluded; it cannot be split, repeated, or partially packed.
Formally, the model can be expressed as maximize Σ(vᵢxᵢ), subject to Σ(wᵢxᵢ) ≤ C, with xᵢ ∈ {0,1}. Here, vᵢ is an item’s value, wᵢ is its weight, C is the capacity, and xᵢ records whether the item is selected.
“Exact” has a specific scope. The result is globally optimal for the entered bounded model: one capacity, whole-number weights, nonnegative values, and each item available at most once. It does not mean that the selection accounts for every constraint that could arise in a real-world problem. If two selections have the same maximum value, the calculator retains the earlier solution rather than switching arbitrarily, making repeated input deterministic.
Enter and solve a valid 0/1 model
Prepare one capacity and one row for every candidate item. The input format is deliberately compact, so check the labels, units, and numeric fields before solving.
- Set the capacity. Enter a whole-number capacity from 1 through 10,000. Use the same weight unit that you will use for the item weights.
- Add one row per item. Enter each item as label, weight, value. The label must be unique and cannot contain a comma. Enter a positive whole-number weight that is no greater than the capacity, followed by a zero or positive decimal value. The calculator accepts no more than 100 items.
- Run the exact 0/1 solve. Submit the model for processing. The calculator compares selections that exclude each item with compatible selections that include it, then reconstructs the maximum-value subset.
- Inspect every selected item and the totals. Review the selected labels, total value, total weight, and unused capacity. Confirm that the returned rows and totals match the capacity and item data you intended to enter.
- Copy the selection when needed. If useful, copy the optimal selection as plain text for a classroom solution, allocation draft, or other bounded planning exercise.
The calculator rejects blank fields, duplicate labels, nonpositive weights, items heavier than the entered capacity, non-finite values, and malformed comma-separated rows. An overweight item is reported as invalid rather than silently ignored, which makes the incompatible input visible before you interpret the result.
How the exact answer is produced and checked
For each item and each capacity state from zero through the entered limit, the dynamic program compares two possibilities: the best value available without the current item and the best value available by including it with a compatible earlier state. It retains the larger value. After the table is complete, it walks backward through the stored decisions to reconstruct the chosen labels.
This structure is what makes the 0/1 restriction enforceable: every item contributes at most once, and the reconstructed selection never exceeds the capacity. The objective also matches the formulation covered by Google OR-Tools’ knapsack guide, while Stanford CS161’s lecture notes provide a detailed treatment of 0/1 knapsack dynamic programming.
One worked example
Set the capacity to 10 and include two candidate rows: Alpha, 4, 40 and Beta, 3, 50. Their combined weight is 4 + 3 = 7, so both fit within the capacity. The unused capacity is 10 - 7 = 3, and the total selected value is 40 + 50 = 90. The calculator displays the same selected labels and totals, allowing the arithmetic to be checked directly.
The distinction between an exact method and a greedy shortcut matters because the item with the best value-to-weight ratio is not always part of the maximum-value subset. An exact dynamic program compares the relevant combinations rather than committing to a single ratio-based choice.
Prepare decimals and respect input limits
Capacity and weights must be whole numbers because the exact dynamic-programming table uses one state per capacity unit. If your measurements contain fixed decimals, convert all weights and the capacity to a consistent whole-unit scale before entering them. Choose a scale that preserves the distinctions you need, because a finer scale also creates a larger table.
Values are treated differently from weights. They may be zero or positive decimals, and they do not need to use the same unit as weight. Each value must still be finite. Before submitting, check that every item has a meaningful label and that the comma-separated row contains exactly three fields.
The calculator limits capacity to 10,000 and the item list to 100 entries to keep browser memory use and response time bounded. Calculation stays in the browser, so the model can be solved locally without treating the page as a remote optimization service.
Confirm the knapsack variant
The single-container 0/1 model is only one form of the knapsack problem. A requirement that permits fractional items or assigns items to several containers calls for a different model.
| Problem variant | Can an item be divided? | Container model | Main goal |
|---|---|---|---|
| Single-container 0/1 knapsack | No; each item is selected once or not at all | One capacity | Maximize total value |
| Fractional knapsack | Yes; a portion may be selected | One capacity | Maximize total value |
| Multiple-knapsack or bin-packing | Depends on the separate model | Several containers | Distribute items or optimize a different packing objective |
If your application allows a portion of an item, do not treat the 0/1 answer as equivalent. Likewise, if the goal is to minimize the number of containers, balance loads, or fit items into several bins, the maximum-value subset returned here does not solve that different objective.
Match the model to the decision
The calculator is most useful when the objective can be represented cleanly by one weight or effort limit and one value measure for each item. It can support classroom problems, feature prioritization with an explicit effort budget, bounded portfolio examples, and small allocation drafts.
| Appropriate use | What the model contributes | What still needs review |
|---|---|---|
| Classroom 0/1 problem | An exact subset with transparent totals | The stated objective, units, and tie assumptions |
| Feature prioritization | A value-maximizing set under an explicit effort budget | Dependencies, mandatory work, deadlines, and operational effects |
| Bounded portfolio example | An illustrative selection under one scalar limit | Risk, diversification, concentration, and real-world constraints |
| Small allocation draft | A candidate set that fits the stated capacity | Subject-matter requirements and the consequences of implementation |
For financial portfolios, safety loading, medical resource allocation, or other high-stakes decisions, the scalar model is insufficient. Validate the objective, dependencies, constraints, and real-world consequences with an appropriate domain model and an accountable reviewer. A mathematically optimal selection can still be physically impossible or operationally inappropriate.
Check the answer before using it
Before relying on the result, run through a short verification pass:
- Confirm that the capacity and all item weights use the same whole-number unit.
- Check that each selected label appears once and that no eligible item was accidentally omitted because of a data-entry error.
- Verify that total weight does not exceed capacity and that the displayed unused capacity is consistent with it.
- Confirm that total value represents the objective you intended to maximize rather than a ratio, cost, or priority score with a different meaning.
- Consider equal-value alternatives, remembering that the earlier solution is retained on a tie.
- Review any constraints the model does not represent, including dimensions, balance, fragility, interactions, mandatory groups, incompatible pairs, deadlines, risk, and uncertainty.
This verification turns the output into more than a number: it shows exactly which items were chosen under a defined set of assumptions and where additional modeling may be necessary.
Related reading: Accurate Metal Weight Calculator: Formulas and Limits.