"""simple docstring"""
def 						__a   (	_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )	->Optional[Any]:
  global f  # a global dp table for knapsack
  if f[i][j] < 0:
    if j < wt[i - 1]:
      a__:     Dict										=	mf_knapsack(i - 1		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )
    else:
      a__:     List[str]										=	max(
          mf_knapsack(i - 1		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )		,     mf_knapsack(i - 1		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     j - wt[i - 1]   ) + val[i - 1]		,     )
    a__:     Union[str, Any]										=	val
  return f[i][j]







def 						__a   (	_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )	->List[str]:
  a__:     str										=	[[0] * (w + 1) for _ in range(n + 1   )]

  for i in range(1		,     n + 1   ):
    for w_ in range(1		,     w + 1   ):
      if wt[i - 1] <= w_:
        a__:     int										=	max(val[i - 1] + dp[i - 1][w_ - wt[i - 1]]		,     dp[i - 1][w_]   )
      else:
        a__:     Union[str, Any]										=	dp[i - 1][w_]

  return dp[n][w_], dp







def 						__a   (	_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )	->Optional[int]:
  if not (isinstance(_SCREAMING_SNAKE_CASE		,     (list, tuple)   ) and isinstance(_SCREAMING_SNAKE_CASE		,     (list, tuple)   )):
    raise ValueError(
        'Both the weights and values vectors must be either lists or tuples'   )

  a__:     int										=	len(_SCREAMING_SNAKE_CASE   )
  if num_items != len(_SCREAMING_SNAKE_CASE   ):
    a__:     Optional[int]										=	(
        'The number of weights must be the same as the number of values.\n'
        F'But got {num_items} weights and {len(_SCREAMING_SNAKE_CASE   )} values'
    )
    raise ValueError(_SCREAMING_SNAKE_CASE   )
  for i in range(_SCREAMING_SNAKE_CASE   ):
    if not isinstance(wt[i]		,     _SCREAMING_SNAKE_CASE   ):
      a__:     List[Any]										=	(
          'All weights must be integers but got weight of '
          F'type {type(wt[i]   )} at index {i}'
      )
      raise TypeError(_SCREAMING_SNAKE_CASE   )

  a__							,    a__:     List[Any]										=	knapsack(_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )
  a__:     set										=	set()
  _construct_solution(_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )

  return optimal_val, example_optional_set







def 						__a   (	_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )	->Optional[Any]:
  # for the current item i at a maximum weight j to be part of an optimal subset,
  # the optimal value at (i, j) must be greater than the optimal value at (i-1, j).
  # where i - 1 means considering only the previous items at the given maximum weight
  if i > 0 and j > 0:
    if dp[i - 1][j] == dp[i][j]:
      _construct_solution(_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     i - 1		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE   )
    else:
      optimal_set.add(_SCREAMING_SNAKE_CASE   )
      _construct_solution(_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     i - 1		,     j - wt[i - 1]		,     _SCREAMING_SNAKE_CASE   )


if __name__ == "__main__":
 lowercase__					       =	[3, 2, 4, 4]
 lowercase__					       =	[4, 3, 2, 3]
 lowercase__					       =	4
 lowercase__					       =	6
 lowercase__					       =	[[0] * (w + 1)] + [[0] + [-1] * (w + 1) for _ in range(n + 1)]
 lowercase__       ,					lowercase__					       =	knapsack(w, wt, val, n)
 print(optimal_solution)
 print(mf_knapsack(n, wt, val, w))  # switched the n and w

 # testing the dynamic programming problem with example
 # the optimal subset for the above example are items 3 and 4
 lowercase__       ,					lowercase__					       =	knapsack_with_example_solution(w, wt, val)
 assert optimal_solution == 8
 assert optimal_subset == {3, 4}
 print('optimal_value = ', optimal_solution)
 print('An optimal subset corresponding to the optimal value', optimal_subset)



"""simple docstring"""
class    __snake_case       :



      def __init__(						self    ,			lowercase    ,			lowercase=None    ,			lowercase=None)	->			List[str]:





        '''simple docstring'''





        a__:     Dict										=	data
        a__:     List[Any]										=	previous
        a__:     Any										=	next_node



      def __str__(						self)	->			str:





        '''simple docstring'''





        return f'{self.data}'



      def   lowerCamelCase_   (						self)	->			int:





        '''simple docstring'''





        return self.data



      def   lowerCamelCase_   (						self)	->			Union[str, Any]:





        '''simple docstring'''





        return self.next



      def   lowerCamelCase_   (						self)	->			Optional[Any]:





        '''simple docstring'''





        return self.previous







class    __snake_case       :



      def __init__(						self    ,			lowercase)	->			Dict:





        '''simple docstring'''





        a__:     List[Any]										=	head



      def __iter__(						self)	->			List[Any]:





        '''simple docstring'''





        return self



      def   lowerCamelCase_   (						self)	->			Optional[Any]:





        '''simple docstring'''





        if not self.current:
          raise StopIteration
        else:
          a__:     Dict										=	self.current.get_data()
          a__:     Optional[Any]										=	self.current.get_next()
          return value







class    __snake_case       :



      def __init__(						self)	->			Dict:





        '''simple docstring'''





        a__:     List[Any]										=	None  # First node in list
        a__:     Optional[int]										=	None  # Last node in list



      def __str__(						self)	->			Optional[Any]:





        '''simple docstring'''





        a__:     Dict										=	self.head
        a__:     Optional[Any]										=	[]
        while current is not None:
          nodes.append(current.get_data())
          a__:     str										=	current.get_next()
        return " ".join(str(lowercase) for node in nodes)



      def __contains__(						self    ,			lowercase)	->			Optional[int]:





        '''simple docstring'''





        a__:     Optional[int]										=	self.head
        while current:
          if current.get_data() == value:
            return True
          a__:     Dict										=	current.get_next()
        return False



      def __iter__(						self)	->			int:





        '''simple docstring'''





        return LinkedListIterator(self.head)



      def   lowerCamelCase_   (						self)	->			List[Any]:





        '''simple docstring'''





        if self.head:
          return self.head.get_data()
        return None



      def   lowerCamelCase_   (						self)	->			Optional[Any]:





        '''simple docstring'''





        if self.tail:
          return self.tail.get_data()
        return None



      def   lowerCamelCase_   (						self    ,			lowercase)	->			None:





        '''simple docstring'''





        if self.head is None:
          a__:     Optional[Any]										=	node
          a__:     Optional[Any]										=	node
        else:
          self.insert_before_node(self.head    ,			lowercase)



      def   lowerCamelCase_   (						self    ,			lowercase)	->			None:





        '''simple docstring'''





        if self.head is None:
          self.set_head(lowercase)
        else:
          self.insert_after_node(self.tail    ,			lowercase)



      def   lowerCamelCase_   (						self    ,			lowercase)	->			None:





        '''simple docstring'''





        a__:     Tuple										=	Node(lowercase)
        if self.head is None:
          self.set_head(lowercase)
        else:
          self.set_tail(lowercase)



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase)	->			None:





        '''simple docstring'''





        a__:     Union[str, Any]										=	node
        a__:     Optional[Any]										=	node.previous

        if node.get_previous() is None:
          a__:     Tuple										=	node_to_insert
        else:
          a__:     int										=	node_to_insert

        a__:     Optional[int]										=	node_to_insert



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase)	->			None:





        '''simple docstring'''





        a__:     Optional[int]										=	node
        a__:     Tuple										=	node.next

        if node.get_next() is None:
          a__:     Optional[int]										=	node_to_insert
        else:
          a__:     Any										=	node_to_insert

        a__:     str										=	node_to_insert



      def   lowerCamelCase_   (						self    ,			lowercase    ,			lowercase)	->			None:





        '''simple docstring'''





        a__:     Any										=	1
        a__:     Tuple										=	Node(lowercase)
        a__:     Tuple										=	self.head
        while node:
          if current_position == position:
            self.insert_before_node(lowercase    ,			lowercase)
            return
          current_position += 1
          a__:     List[Any]										=	node.next
        self.insert_after_node(self.tail    ,			lowercase)



      def   lowerCamelCase_   (						self    ,			lowercase)	->			Node:





        '''simple docstring'''





        a__:     Tuple										=	self.head
        while node:
          if node.get_data() == item:
            return node
          a__:     List[str]										=	node.get_next()
        raise Exception('Node not found')



      def   lowerCamelCase_   (						self    ,			lowercase)	->			Any:





        '''simple docstring'''





        if (node := self.get_node(lowercase)) is not None:
          if node == self.head:
            a__:     Any										=	self.head.get_next()

          if node == self.tail:
            a__:     List[Any]										=	self.tail.get_previous()

          self.remove_node_pointers(lowercase)



      @staticmethod
      def   lowerCamelCase_   (						lowercase)	->			None:





        '''simple docstring'''





        if node.get_next():
          a__:     Any										=	node.previous

        if node.get_previous():
          a__:     List[str]										=	node.next

        a__:     int										=	None
        a__:     Union[str, Any]										=	None



      def   lowerCamelCase_   (						self)	->			str:





        '''simple docstring'''





        return self.head is None







def 						__a   (	)	->None:
  pass


if __name__ == "__main__":
 import doctest

 doctest.testmod()


