function [ more, u, t ] = subset_sum_next ( s, n, v, more, u, t )
 
%*****************************************************************************80
%
%% SUBSET_SUM_NEXT seeks, one at a time, subsets of V that sum to S.
%
%  Licensing:
%
%    I don't care what you do with this code.
%
%  Modified:
%
%    14 July 2017
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer S, the desired sum.
%
%    Input, integer N, the number of values.
%
%    Input, integer V(N), the values.
%    These must be nonnegative, and sorted in ascending order.  
%    Duplicate values are allowed.
%
%    Input, logical MORE, should be set to FALSE before the first call.
%    Thereafter, it should be the output value of the previous call.
%
%    Input, integer U(N), should be set to 0 before the first call.
%    Thereafter, it should be the output value of the previous call.
%
%    Input, integer T, should be set to 0 before the first call.
%    Thereafter, it should be the output value of the previous call.
%
%    Output, logical MORE, is TRUE if a new solution has been returned in U.
%    Process this solution, and call again if more solutions should be sought.
%
%    Output, integer U(N), if MORE is true, U indexes the solution values.
%
%    Output, integer T, if MORE is true, T is the highest index of the selected values.
%
  if ( ~ more )
  
    t = 0;
    u = zeros ( n, 1 );
    
  else
  
    more = false;
    u(t) = 0;

    t = backup_one ( n, u, t );
      
    if ( t < 1 )
      return
    end

    u(t) = 0;
    t = t + 1;
    u(t) = 1;
      
  end

  while ( true )

    su = u' * v;

    if ( su < s && t < n )

      t = t + 1;
      u(t) = 1;        

    elseif ( su == s )

      more = true;
      return

    else

      u(t) = 0;
      
      t = backup_one ( n, u, t );
      
      if ( t < 1 )
        break
      end

      u(t) = 0;
      t = t + 1;
      u(t) = 1;

    end
        
  end

  return
end
