

def 			A_      (    _UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase    ):
						global f  # a global dp table for knapsack
						if f[i][j] < 0:
												if j < wt[i - 1]:
																		SCREAMING_SNAKE_CASE_: str  		=       mf_knapsack(i - 1				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase    )
												else:
																		SCREAMING_SNAKE_CASE_: Tuple  		=       max(
																		    mf_knapsack(i - 1				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase    )				,	mf_knapsack(i - 1				,	_UpperCAmelCase				,	_UpperCAmelCase				,	j - wt[i - 1]    ) + val[i - 1]				,	)
												SCREAMING_SNAKE_CASE_: Any  		=       val
						return f[i][j]







def 			A_      (    _UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase    ):
						SCREAMING_SNAKE_CASE_: Any  		=       [[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_:
																								SCREAMING_SNAKE_CASE_: Optional[int]  		=       max(val[i - 1] + dp[i - 1][w_ - wt[i - 1]]				,	dp[i - 1][w_]    )
																		else:
																								SCREAMING_SNAKE_CASE_: List[str]  		=       dp[i - 1][w_]

						return dp[n][w_], dp







def 			A_      (    _UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase    ):
						if not (isinstance(_UpperCAmelCase				,	(list, tuple)    ) and isinstance(_UpperCAmelCase				,	(list, tuple)    )):
												raise ValueError(
												    "Both the weights and values vectors must be either lists or tuples"    )

						SCREAMING_SNAKE_CASE_: int  		=       len(_UpperCAmelCase    )
						if num_items != len(_UpperCAmelCase    ):
												SCREAMING_SNAKE_CASE_: Tuple  		=       (
												    "The number of weights must be the same as the number of values.\n"
												    f"But got {num_items} weights and {len(_UpperCAmelCase    )} values"
												)
												raise ValueError(_UpperCAmelCase    )
						for i in range(_UpperCAmelCase    ):
												if not isinstance(wt[i]				,	_UpperCAmelCase    ):
																		SCREAMING_SNAKE_CASE_: Tuple  		=       (
																		    "All weights must be integers but got weight of "
																		    f"type {type(wt[i]    )} at index {i}"
																		)
																		raise TypeError(_UpperCAmelCase    )

						SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Any  		=       knapsack(_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase    )
						SCREAMING_SNAKE_CASE_: set  		=       set()
						_construct_solution(_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase    )

						return optimal_val, example_optional_set







def 			A_      (    _UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase				,	_UpperCAmelCase    ):
						# 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(_UpperCAmelCase				,	_UpperCAmelCase				,	i - 1				,	_UpperCAmelCase				,	_UpperCAmelCase    )
												else:
																		optimal_set.add(_UpperCAmelCase    )
																		_construct_solution(_UpperCAmelCase				,	_UpperCAmelCase				,	i - 1				,	j - wt[i - 1]				,	_UpperCAmelCase    )


if __name__ == "__main__":
						lowerCAmelCase							:  Optional[int]   	=						[3, 2, 4, 4]
						lowerCAmelCase							:  str   	=						[4, 3, 2, 3]
						lowerCAmelCase							:  Any   	=						4
						lowerCAmelCase							:  List[Any]   	=						6
						lowerCAmelCase							:  List[str]   	=						[[0] * (w + 1)] + [[0] + [-1] * (w + 1) for _ in range(n + 1)]
						lowerCAmelCase	,			lowerCAmelCase							:  Any   	=						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
						lowerCAmelCase	,			lowerCAmelCase							:  Any   	=						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)









def 					__lowerCamelCase	( ):







							'''simple docstring'''






							return [list(range(1000 - i   ,							-1000 - i   ,							-1      )      ) for i in range(1000      )]


_UpperCAmelCase		:   Union[str, Any]										=       generate_large_matrix()
_UpperCAmelCase		:   Tuple										=       (
    [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]],
    [[3, 2], [1, 0]],
    [[7, 7, 6]],
    [[7, 7, 6], [-1, -2, -3]],
    grid,
)





def 					__lowerCamelCase	( UpperCamelCase__      ):







							'''simple docstring'''






							assert all(row == sorted(UpperCamelCase__   ,							reverse=UpperCamelCase__      ) for row in grid      )
							assert all(list(UpperCamelCase__      ) == sorted(UpperCamelCase__   ,							reverse=UpperCamelCase__      ) for col in zip(*UpperCamelCase__      )      )





def 					__lowerCamelCase	( UpperCamelCase__      ):







							'''simple docstring'''






							snake_case_							=    0
							snake_case_							=    len(UpperCamelCase__      ) - 1

							# Edge cases such as no values or all numbers are negative.
							if not array or array[0] < 0:
														return 0

							while right + 1 > left:
														snake_case_							=    (left + right) // 2
														snake_case_							=    array[mid]

														# Num must be negative and the index must be greater than or equal to 0.
														if num < 0 and array[mid - 1] >= 0:
																					return mid

														if num >= 0:
																					snake_case_							=    mid + 1
														else:
																					snake_case_							=    mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
							return len(UpperCamelCase__      )





def 					__lowerCamelCase	( UpperCamelCase__      ):







							'''simple docstring'''






							snake_case_							=    0
							snake_case_							=    len(grid[0]      )

							for i in range(len(UpperCamelCase__      )      ):
														snake_case_							=    find_negative_index(grid[i][:bound]      )
														total += bound
							return (len(UpperCamelCase__      ) * len(grid[0]      )) - total





def 					__lowerCamelCase	( UpperCamelCase__      ):







							'''simple docstring'''






							return len([number for row in grid for number in row if number < 0]      )





def 					__lowerCamelCase	( UpperCamelCase__      ):







							'''simple docstring'''






							snake_case_							=    0
							for row in grid:
														for i, number in enumerate(UpperCamelCase__      ):
																					if number < 0:
																												total += len(UpperCamelCase__      ) - i
																												break
							return total





def 					__lowerCamelCase	( ):







							'''simple docstring'''






							from timeit import timeit

							print('Running benchmarks'      )
							snake_case_							=    (
							    'from __main__ import count_negatives_binary_search, '
							    'count_negatives_brute_force, count_negatives_brute_force_with_break, grid'
							)
							for func in (
							    "count_negatives_binary_search",  # took 0.7727 seconds
							    "count_negatives_brute_force_with_break",  # took 4.6505 seconds
							    "count_negatives_brute_force",  # took 12.8160 seconds
							):
														snake_case_							=    timeit(F'''{func}(grid=grid)'''   ,							setup=UpperCamelCase__   ,							number=500      )
														print(F'''{func}() took {time:0.4f} seconds'''      )


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					benchmark()




