dynpro.java:import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class dynpro {

    static class Item {
        String name;
        int profit;
        int weight;

        public Item(String name, int profit, int weight) {
            this.name = name;
            this.profit = profit;
            this.weight = weight;
        }
    }

    public static void main(String[] args) {
        if (args.length > 0 && "knapsack01.txt".equals(args[0])) {
            try {
                List<Item> items = readItems(args[0]);
                int capacity = readCapacity(args[0]);
                solveKnapsack(items, capacity);
            } catch (IOException e) {
                System.out.println("Error reading file: " + e.getMessage());
            }
        } else {
            System.out.println("Usage: dynpro knapsack01.txt");
        }
    }

    private static List<Item> readItems(String fileName) throws IOException {
        List<Item> items = new ArrayList<>();
        try (Scanner scanner = new Scanner(new File(fileName))) {
            scanner.nextLine(); // Skip the first line
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] parts = line.split("\t");
                items.add(new Item(parts[0], Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
            }
        }
        return items;
    }

    private static int readCapacity(String fileName) throws IOException {
        try (Scanner scanner = new Scanner(new File(fileName))) {
            return Integer.parseInt(scanner.nextLine().split("\\s+")[1]);
        }
    }

    private static void solveKnapsack(List<Item> items, int capacity) throws IOException {
        int n = items.size();
        int[] dp = new int[capacity + 1];
        boolean[][] sol = new boolean[n + 1][capacity + 1];
    
        for (int i = 1; i <= n; i++) {
            int weight = items.get(i - 1).weight;
            int profit = items.get(i - 1).profit;
            for (int w = capacity; w >= weight; w--) {
                if (dp[w - weight] + profit > dp[w]) {
                    dp[w] = dp[w - weight] + profit;
                    sol[i][w] = true;
                }
            }
        }
    
        // Find which items are included in the optimal solution
        List<Item> resultItems = new ArrayList<>();
        for (int i = n, w = capacity; i > 0 && w > 0; i--) {
            if (sol[i][w]) {
                Item item = items.get(i - 1);
                resultItems.add(item);
                w -= item.weight;
            }
        }
    
        Collections.reverse(resultItems);
        writeOutput(resultItems, dp[capacity], capacity, "output2.txt");
        writeEntries(sol, items, capacity, "entries2.txt");
    }

    private static void writeOutput(List<Item> items, int maxProfit, int totalWeight, String fileName) throws IOException {
        try (FileWriter writer = new FileWriter(fileName)) {
            writer.write(items.size() + "\t" + maxProfit + "\t" + totalWeight + "\n");
            for (Item item : items) {
                writer.write(item.name + "\t" + item.profit + "\t" + item.weight + "\n");
            }
        }
    }

    private static void writeEntries(boolean[][] sol, List<Item> items, int capacity, String fileName) throws IOException {
        try (FileWriter writer = new FileWriter(fileName)) {
            int[] dp = new int[capacity + 1]; // Array to store maximum profit at each capacity
            List<Integer> previousCapacities = new ArrayList<>(); // List to track previous capacities
    
            for (int i = 1; i <= items.size(); i++) {
                int weight = items.get(i - 1).weight;
                int profit = items.get(i - 1).profit;
                List<Integer> currentCapacities = new ArrayList<>(); // List to track current capacities
    
                for (int w = capacity; w >= weight; w--) {
                    if (dp[w - weight] + profit > dp[w]) {
                        dp[w] = dp[w - weight] + profit;
                        if (!previousCapacities.contains(w)) {
                            currentCapacities.add(w); // Only add capacity if it's a new maximum
                        }
                    }
                }
    
                // Update previous capacities to current for the next iteration
                previousCapacities = new ArrayList<>(currentCapacities);
    
                // Write the current row to the file
                writer.write("row" + i + "\t");
                for (int cap : currentCapacities) {
                    writer.write(cap + " ");
                }
                writer.write("\n");
            }
        }
    }
} The entries2.txt is:

row1	0 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 
row2	0 14 14 14 22 22 22 22 22 22 22 22 22 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 
row3	0 15 15 15 22 22 22 22 22 22 29 29 29 37 37 37 37 37 37 37 37 37 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 
row4	0 15 15 15 22 29 29 29 29 29 29 29 29 37 44 44 44 51 51 51 51 51 51 58 58 58 66 66 66 66 66 66 66 66 66 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 
row5	0 15 15 15 22 29 29 29 29 29 29 29 29 37 44 44 44 51 51 51 51 51 51 58 58 58 66 66 66 66 66 66 66 66 67 80 80 80 80 80 80 80 80 82 82 82 82 82 82 82 82 82 96 96 96 96 96 96 96 96 
row6	0 15 23 23 23 29 29 29 29 29 29 38 38 38 45 52 52 52 52 52 52 52 52 60 67 67 67 74 74 74 74 74 74 81 81 81 89 89 89 89 89 89 89 89 90 103 103 103 103 103 103 103 103 105 105 105 105 105 105 105 
row7	0 15 23 25 25 29 29 29 29 29 29 38 40 48 48 52 54 54 54 54 54 54 63 63 67 70 77 77 77 77 77 77 77 81 85 92 92 92 99 99 99 99 99 99 106 106 106 114 114 114 114 114 114 114 114 115 128 128 128 128 
row8	0 15 23 25 25 29 29 29 29 29 29 38 40 48 48 52 54 54 54 54 54 54 63 63 67 70 77 77 77 77 77 77 77 81 85 92 92 93 99 99 99 99 99 99 106 106 108 114 114 115 115 115 115 115 115 122 128 128 130 130 
row9	0 10 10 15 23 25 25 29 29 29 29 33 35 38 40 48 48 52 54 54 54 54 58 58 63 64 67 70 77 77 77 77 77 77 80 87 87 92 92 93 99 99 99 99 102 102 106 109 109 114 114 115 115 116 116 118 124 124 128 128 130 130 
row10	0 10 10 15 23 25 25 29 29 29 29 33 35 38 40 48 48 52 54 54 54 54 58 58 63 64 67 70 77 77 77 77 77 77 80 87 87 92 92 93 99 99 99 99 102 102 106 109 109 114 114 115 115 116 116 118 124 124 128 128 130 130 

and output2.txt is:
3	130	66
Item1	22	12
Item3	15	9
Item8	16	11

which are incorrect. 

The output2.txt should excatly be same as output1.txt:
6	130	66
Item1	22	12
Item3	15	9
Item4	29	13
Item6	23	10
Item7	25	11
Item8	16	11

The format of entries2.txt should be as follows.

entries2.txt  
row1	<col1> <col2> … <coli>
row2	<col1> <col2> … <colj> 
.
.
.
.
row9	<col1> <col2>
row10	<col1>
You will have one column index for row10 and two column indices for row9.


Consider an example to understand the formats clearly:
knapsack:
6 54
item1 22 8
item2 20 20
item3 21 20
item4 18 13
item5 12 18
item6 12 12

output1.txt:
4 73 53
item1 22 8
item3 21 20
item4 18 13
item6 12 12

output2.txt:
4 73 53
item1 22 8
item3 21 20
item4 18 13
item6 12 12

entries2.txt:
row1    0    0    0    0   22   22   22   22   22   22   22   22   22   22   22   22   22   22
row2    0    0   22   22   22   22   22   22   22   42   42   42   42   42   42
row3   22   22   22   43   43   43   43   63
row4   40   43   61   63
row5   61   63
row6   73