function object = knapsack_dynamic ( p, w, c )

%*****************************************************************************80
%
%% knapsack_dynamic() uses dynamic programming to solve a knapsack problem.
%
%  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:
%
%    integer object(): the list of objects to be taken.
%
  m = knapsack_dynamic_table ( p, w, c );

  n = length ( p );
  object = [];
  c2 = c;

  for i = n : -1 : 1
    if ( m(i,c2+1) < m(i+1,c2+1) )
      c2 = c2 - w(i);
      object = [ object, i ];
    end
  end

  return
end
