function m = knapsack_dynamic_table ( p, w, c )

%*****************************************************************************80
%
%% knapsack_dynamic_table() computes a knapsack problem dynamic programming table.
%
%  Discussion:
%
%    All data must be integers.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 October 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%   integer p(n): the profit of each item.
%
%   integer w(n): the weight of each item.
%
%   integer c: the knapsack capacity.
%
%  Output:
%
%    m(1:n+1,1:c+1): m(i+1,j+1) is the maximum value that can be stored in
%    the knapsack, selecting from the first i items, with a weight no more than j.
%
  n = length ( p );

  m = zeros ( n + 1, c + 1 );

  for j = 0 : c
    m(1,j+1) = 0;
  end

  for i = 1 : n
    m(i+1,1) = 0;
  end

  for i = 1 : n
    for j = 0 : c
      if ( j < w(i) )
        m(i+1,j+1) = m(i,j+1);
      else
        m(i+1,j+1) = max ( m(i,j+1), m(i,j-w(i)+1) + p(i) );
      end
    end
  end

  return
end

