public Inventory solveDynamically() {
        int[] solution = new int[capacity + 1];
        ArrayList<Double> amounts = new ArrayList<Double>();
        boolean[][] isIncluded = new boolean[items.size()][capacity + 1];

        if (capacity < 0) {
            return new Inventory(this, amounts);
        }
  
        for (int i = 0; i < items.size(); i++) {
            amounts.add(0.0);
            Item item = items.get(i);
            int value = item.getValue();
            int weight = item.getWeight();
            for (int c = capacity; c >= weight; c--) {
                int newValue = value + solution[c - weight];
                if (newValue > solution[c]) {
                    isIncluded[i][c] = true;
                    solution[c] = newValue;
                }
            }
        }
 
        int c = capacity;
        int p = 0;
        for (int i = items.size() - 1; i >= 0; i--) {
            boolean b = isIncluded[i][c];
            if (b) {
                c -= items.get(i).getWeight();
                p += items.get(i).getValue();
                amounts.set(i, 1.0);
            }
        }
        return new Inventory(this, amounts);
    }