






from __future__ import annotations

import random
from collections.abc import Iterable
from typing import Any, Generic, TypeVar

__UpperCAmelCase      =				TypeVar("T", bound=bool)
class 	a_(		Generic[T]	):



     """simple docstring"""



     def __init__(		self					:	Tuple  ,  lowerCAmelCase__					:	T)							->	None:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		value
      SCREAMING_SNAKE_CASE				    =		None
      SCREAMING_SNAKE_CASE				    =		None





     @property
     def     __UpperCamelCase						(		self					:	int)							->	T:

      """simple docstring"""
      return self._value





     @staticmethod
     def     __UpperCamelCase						(		lowerCAmelCase__					:	RandomizedHeapNode[T] | None  ,  lowerCAmelCase__					:	RandomizedHeapNode[T] | None)							->	RandomizedHeapNode[T] | None:

      """simple docstring"""
      if not roota:
       return roota

      if not roota:
       return roota

      if roota.value > roota.value:
       SCREAMING_SNAKE_CASE    ,   SCREAMING_SNAKE_CASE				    =		roota, roota

      if random.choice([True, False]):
       SCREAMING_SNAKE_CASE    ,   SCREAMING_SNAKE_CASE				    =		roota.right, roota.left

      SCREAMING_SNAKE_CASE				    =		RandomizedHeapNode.merge(roota.left  ,  lowerCAmelCase__)

      return roota

class 	a_(		Generic[T]	):



     """simple docstring"""



     def __init__(		self					:	Union[str, Any]  ,  lowerCAmelCase__					:	Iterable[T] | None = ())							->	None:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		None

      if data:
       for item in data:
        self.insert(lowerCAmelCase__)





     def     __UpperCamelCase						(		self					:	Optional[int]  ,  lowerCAmelCase__					:	T)							->	None:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		RandomizedHeapNode.merge(self._root  ,  RandomizedHeapNode(lowerCAmelCase__))





     def     __UpperCamelCase						(		self					:	Optional[int])							->	T | None:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		self.top()

      if self._root is None:
       return None

      SCREAMING_SNAKE_CASE				    =		RandomizedHeapNode.merge(self._root.left  ,  self._root.right)

      return result





     def     __UpperCamelCase						(		self					:	Tuple)							->	T:

      """simple docstring"""
      if not self._root:
       raise IndexError('Can\'t get top element for the empty heap.')
      return self._root.value





     def     __UpperCamelCase						(		self					:	Optional[int])							->	None:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		None





     def     __UpperCamelCase						(		self					:	List[str])							->	list[Any]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE				    =		[]
      while self:
       result.append(self.pop())

      return result





     def __bool__(		self					:	Optional[int])							->	bool:

      """simple docstring"""
      return self._root is not None


if __name__ == "__main__":
      import doctest

      doctest.testmod()





from __future__ import annotations







def     A__	(       __A ,			__A						):







    '''simple docstring'''
    _lowerCamelCase  :		List[str]			    =    [None] * (len(__A						) + len(__A						))

    _lowerCamelCase  :		Optional[int]			    =    0  # pointer to current index for left Half
    _lowerCamelCase  :		List[str]			    =    0  # pointer to current index for the right Half
    _lowerCamelCase  :		List[str]			    =    0  # pointer to current index for the sorted array Half

    while pointera < len(__A						) and pointera < len(__A						):
        if left_half[pointera] < right_half[pointera]:
            _lowerCamelCase  :		Optional[Any]			    =    left_half[pointera]
            pointera += 1
            index += 1
        else:
            _lowerCamelCase  :		List[Any]			    =    right_half[pointera]
            pointera += 1
            index += 1
    while pointera < len(__A						):
        _lowerCamelCase  :		int			    =    left_half[pointera]
        pointera += 1
        index += 1

    while pointera < len(__A						):
        _lowerCamelCase  :		Optional[Any]			    =    right_half[pointera]
        pointera += 1
        index += 1

    return sorted_array







def     A__	(       __A						):







    '''simple docstring'''
    if len(__A						) <= 1:
        return array
    # the actual formula to calculate the middle element = left + (right - left) // 2
    # this avoids integer overflow in case of large N
    _lowerCamelCase  :		int			    =    0 + (len(__A						) - 0) // 2

    # Split the array into halves till the array length becomes equal to One
    # merge the arrays of single length returned by mergeSort function and
    # pass them into the merge arrays function which merges the array
    _lowerCamelCase  :		List[str]			    =    array[:middle]
    _lowerCamelCase  :		Optional[int]			    =    array[middle:]

    return merge(merge_sort(__A						) ,			merge_sort(__A						)						)


if __name__ == "__main__":
    import doctest

    doctest.testmod()




