



"""simple docstring"""
from __future__ import annotations

from random import random


class       SCREAMING_SNAKE_CASE__       :

    def __init__( self	: Union[str, Any]       ,     lowerCAmelCase_	: Optional[int] = None):







      """simple docstring"""







      lowercase_	    =       value
      lowercase_	    =       random()
      lowercase_	    =       None
      lowercase_	    =       None

    def __repr__( self	: Any):







      """simple docstring"""







      from pprint import pformat

      if self.left is None and self.right is None:
        return F'''\'{self.value}: {self.prior:.5}\''''
      else:
        return pformat(
            {F'''{self.value}: {self.prior:.5}''': (self.left, self.right)}       ,     indent=1)




    def __str__( self	: Optional[int]):







      """simple docstring"""







      lowercase_	    =       str(self.value) + ''' '''
      lowercase_	    =       str(self.left or """""")
      lowercase_	    =       str(self.right or """""")
      return value + left + right







def 	_SCREAMING_SNAKE_CASE  (__lowerCAmelCase       ,     __lowerCAmelCase							)       ->  Union[str, Any]:







  '''simple docstring'''


  if root is None:  # None tree is split into 2 Nones
    return None, None
  elif root.value is None:
    return None, None
  else:
    if value < root.value:
      lowercase_	    =       split(root.left       ,     __lowerCAmelCase							)
      return left, root
    else:
      lowercase_	    =       split(root.right       ,     __lowerCAmelCase							)
      return root, right


def 	_SCREAMING_SNAKE_CASE  (__lowerCAmelCase       ,     __lowerCAmelCase							)       ->  Tuple:







  '''simple docstring'''


  if (not left) or (not right):  # If one node is None, return the other
    return left or right
  elif left.prior < right.prior:
    lowercase_	    =       merge(left.right       ,     __lowerCAmelCase							)
    return left
  else:
    lowercase_	    =       merge(__lowerCAmelCase       ,     right.left							)
    return right


def 	_SCREAMING_SNAKE_CASE  (__lowerCAmelCase       ,     __lowerCAmelCase							)       ->  List[str]:







  '''simple docstring'''


  lowercase_	    =       Node(__lowerCAmelCase							)
  lowercase_	    =       split(__lowerCAmelCase       ,     __lowerCAmelCase							)
  return merge(merge(__lowerCAmelCase       ,     __lowerCAmelCase							)       ,     __lowerCAmelCase							)


def 	_SCREAMING_SNAKE_CASE  (__lowerCAmelCase       ,     __lowerCAmelCase							)       ->  Optional[Any]:







  '''simple docstring'''


  lowercase_	    =       split(__lowerCAmelCase       ,     value - 1							)
  lowercase_	    =       split(__lowerCAmelCase       ,     __lowerCAmelCase							)
  return merge(__lowerCAmelCase       ,     __lowerCAmelCase							)


def 	_SCREAMING_SNAKE_CASE  (__lowerCAmelCase							)       ->  Tuple:







  '''simple docstring'''


  if not root:  # None
    return
  else:
    inorder(root.left							)
    print(root.value       ,     end=""","""							)
    inorder(root.right							)


def 	_SCREAMING_SNAKE_CASE  (__lowerCAmelCase       ,     __lowerCAmelCase							)       ->  Tuple:







  '''simple docstring'''


  for arg in args.split():
    if arg[0] == "+":
      lowercase_	    =       insert(__lowerCAmelCase       ,     int(arg[1:]							)							)

    elif arg[0] == "-":
      lowercase_	    =       erase(__lowerCAmelCase       ,     int(arg[1:]							)							)

    else:
      print("""Unknown command"""							)

  return root


def 	_SCREAMING_SNAKE_CASE  ()       ->  Tuple:







  '''simple docstring'''


  lowercase_	    =       None
  print(
      """enter numbers to create a tree, + value to add value into treap, """
      """- value to erase all nodes with value. \'q\' to quit. """							)

  lowercase_	    =       input()
  while args != "q":
    lowercase_	    =       interact_treap(__lowerCAmelCase       ,     __lowerCAmelCase							)
    print(__lowerCAmelCase							)
    lowercase_	    =       input()

  print("""good by!"""							)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
       main()


'''simple docstring'''

from math import pow
def 			__UpperCamelCase       (   UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase		,     ):
 if current_sum == needed_sum:
  # If the sum of the powers is equal to needed_sum, then we have a solution.
  solutions_count += 1
  return current_sum, solutions_count

 lowercase__				:    Optional[Any]  =					int(pow(UpperCAmelCase		,     UpperCAmelCase     )     )
 if current_sum + i_to_n <= needed_sum:
  # If the sum of the powers is less than needed_sum, then continue adding powers.
  current_sum += i_to_n
  lowercase__ ,							lowercase__				:    Dict  =					backtrack(
      UpperCAmelCase		,     UpperCAmelCase		,     current_number + 1		,     UpperCAmelCase		,     UpperCAmelCase     )
  current_sum -= i_to_n
 if i_to_n < needed_sum:
  # If the power of i is less than needed_sum, then try with the next power.
  lowercase__ ,							lowercase__				:    str  =					backtrack(
      UpperCAmelCase		,     UpperCAmelCase		,     current_number + 1		,     UpperCAmelCase		,     UpperCAmelCase     )
 return current_sum, solutions_count
def 			__UpperCamelCase       (   UpperCAmelCase		,     UpperCAmelCase     ):
 if not (1 <= needed_sum <= 1000 and 2 <= power <= 10):
  raise ValueError(
      '''Invalid input\n'''
      '''needed_sum must be between 1 and 1000, power between 2 and 10.'''     )

 return backtrack(UpperCAmelCase		,     UpperCAmelCase		,     1		,     0		,     0     )[1]  # Return the solutions_count


if __name__ == "__main__":
  import doctest

  doctest.testmod()

