


'''simple docstring'''


def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				Tuple	    =					0
		while num > 0:
				digit_sum += num % 10
				num //= 10
		return digit_sum






def        __lowerCAmelCase       (__lowerCAmelCase = 100      ):
		_UpperCAmelCase						:				Optional[Any]	    =					1
		_UpperCAmelCase						:				Any	    =					2

		for i in range(2					,       max_n + 1      ):
				_UpperCAmelCase						:				Tuple	    =					pre_numerator
				_UpperCAmelCase						:				int	    =					2 * i // 3 if i % 3 == 0 else 1
				_UpperCAmelCase						:				Tuple	    =					cur_numerator
				_UpperCAmelCase						:				Optional[int]	    =					e_cont * pre_numerator + temp

		return sum_digits(__lowerCAmelCase      )


if __name__ == "__main__":
						print(F'''{solution() = }''')







'''simple docstring'''


def        __lowerCAmelCase       ():
		return [list(range(1_000 - i					,       -1_000 - i					,       -1      )      ) for i in range(1_000      )]


lowerCamelCase__					=   generate_large_matrix()
lowerCamelCase__					=   (
    [[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       (__lowerCAmelCase      ):
		assert all(row == sorted(__lowerCAmelCase					,       reverse=__lowerCAmelCase      ) for row in grid      )
		assert all(list(__lowerCAmelCase      ) == sorted(__lowerCAmelCase					,       reverse=__lowerCAmelCase      ) for col in zip(*__lowerCAmelCase      )      )






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				Any	    =					0
		_UpperCAmelCase						:				str	    =					len(__lowerCAmelCase      ) - 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:
				_UpperCAmelCase						:				Union[str, Any]	    =					(left + right) // 2
				_UpperCAmelCase						:				List[str]	    =					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:
						_UpperCAmelCase						:				Tuple	    =					mid + 1
				else:
						_UpperCAmelCase						:				Optional[Any]	    =					mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
		return len(__lowerCAmelCase      )






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				str	    =					0
		_UpperCAmelCase						:				int	    =					len(grid[0]      )

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






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






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				Tuple	    =					0
		for row in grid:
				for i, number in enumerate(__lowerCAmelCase      ):
						if number < 0:
								total += len(__lowerCAmelCase      ) - i
								break
		return total






def        __lowerCAmelCase       ():
		from timeit import timeit

		print("Running benchmarks"      )
		_UpperCAmelCase						:				Tuple	    =					(
		    "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
		):
				_UpperCAmelCase						:				str	    =					timeit(F"""{func}(grid=grid)"""					,       setup=__lowerCAmelCase					,       number=500      )
				print(F"""{func}() took {time:0.4f} seconds"""      )


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						benchmark()



