






'''simple docstring'''
from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload


class        _UpperCAmelCase						:




    def __init__(			self  :       Optional[Any]  ,     a  :       Collection[float] | None = None			):






       '''simple docstring'''






       if components is None:
          lowercase_	:    Any           =     []
       lowercase_	:    Union[str, Any]           =     list(a			)




    def __len__(			self  :       Tuple			):






       '''simple docstring'''






       return len(self.__components			)




    def __str__(			self  :       List[Any]			):






       '''simple docstring'''






       return "(" + ",".join(map(a  ,     self.__components			)			) + ")"




    def __add__(			self  :       Dict  ,     a  :       Vector			):






       '''simple docstring'''






       lowercase_	:    List[str]           =     len(self			)
       if size == len(a			):
          lowercase_	:    List[str]           =     [self.__components[i] + other.component(a			) for i in range(a			)]
          return Vector(a			)
       else:
          raise Exception("must have the same size"			)




    def __sub__(			self  :       Any  ,     a  :       Vector			):






       '''simple docstring'''






       lowercase_	:    Any           =     len(self			)
       if size == len(a			):
          lowercase_	:    Any           =     [self.__components[i] - other.component(a			) for i in range(a			)]
          return Vector(a			)
       else:  # error case
          raise Exception("must have the same size"			)




    @overload
    def __mul__(			self  :       Optional[int]  ,     a  :       float			):






       '''simple docstring'''






       ...




    @overload
    def __mul__(			self  :       str  ,     a  :       Vector			):






       '''simple docstring'''






       ...




    def __mul__(			self  :       Union[str, Any]  ,     a  :       float | Vector			):






       '''simple docstring'''






       if isinstance(a  ,     (float, int)			):
          lowercase_	:    Tuple           =     [c * other for c in self.__components]
          return Vector(a			)
       elif isinstance(a  ,     a			) and len(self			) == len(a			):
          lowercase_	:    List[str]           =     len(self			)
          lowercase_	:    str           =     [self.__components[i] * other.component(a			) for i in range(a			)]
          return sum(a			)
       else:  # error case
          raise Exception("invalid operand!"			)




    def 				lowerCAmelCase__				(			self  :       Union[str, Any]			):






       '''simple docstring'''






       return Vector(self.__components			)




    def 				lowerCAmelCase__				(			self  :       List[str]  ,     a  :       int			):






       '''simple docstring'''






       if isinstance(a  ,     a			) and -len(self.__components			) <= i < len(self.__components			):
          return self.__components[i]
       else:
          raise Exception("index out of range"			)




    def 				lowerCAmelCase__				(			self  :       Union[str, Any]  ,     a  :       int  ,     a  :       float			):






       '''simple docstring'''






       assert -len(self.__components			) <= pos < len(self.__components			)
       lowercase_	:    int           =     value




    def 				lowerCAmelCase__				(			self  :       int			):






       '''simple docstring'''






       if len(self.__components			) == 0:
          raise Exception("Vector is empty"			)
       lowercase_	:    Dict           =     [c**2 for c in self.__components]
       return math.sqrt(sum(a			)			)





    def 				lowerCAmelCase__				(			self  :       List[Any]  ,     a  :       Vector  ,     a  :       bool = False			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     self * other
       lowercase_	:    List[str]           =     self.euclidean_length() * other.euclidean_length()
       if deg:
          return math.degrees(math.acos(num / den			)			)
       else:
          return math.acos(num / den			)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase							):


   """simple docstring"""




   assert isinstance(_UpperCamelCase						,		_UpperCamelCase							)
   return Vector([0] * dimension							)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   assert isinstance(_UpperCamelCase						,		_UpperCamelCase							) and (isinstance(_UpperCamelCase						,		_UpperCamelCase							))
   lowercase_	:    Tuple           =     [0] * dimension
   lowercase_	:    List[str]           =     1
   return Vector(_UpperCamelCase							)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   assert (
       isinstance(_UpperCamelCase						,		_UpperCamelCase							)
       and isinstance(_UpperCamelCase						,		_UpperCamelCase							)
       and (isinstance(_UpperCamelCase						,		(int, float)							))
   )
   return x * scalar + y




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   random.seed(_UpperCamelCase							)
   lowercase_	:    List[str]           =     [random.randint(_UpperCamelCase						,		_UpperCamelCase							) for _ in range(_UpperCamelCase							)]
   return Vector(_UpperCamelCase							)


class        _UpperCAmelCase						:




    def __init__(			self  :       str  ,     a  :       list[list[float]]  ,     a  :       int  ,     a  :       int			):






       '''simple docstring'''






       lowercase_	:    Optional[int]           =     matrix
       lowercase_	:    Optional[int]           =     w
       lowercase_	:    List[Any]           =     h




    def __str__(			self  :       Dict			):






       '''simple docstring'''






       lowercase_	:    int           =     ""
       for i in range(self.__height			):
          ans += "|"
          for j in range(self.__width			):
             if j < self.__width - 1:
                ans += str(self.__matrix[i][j]			) + ","
             else:
                ans += str(self.__matrix[i][j]			) + "|\n"
       return ans




    def __add__(			self  :       List[str]  ,     a  :       Matrix			):






       '''simple docstring'''






       if self.__width == other.width() and self.__height == other.height():
          lowercase_	:    Optional[Any]           =     []
          for i in range(self.__height			):
             lowercase_	:    Tuple           =     [
                 self.__matrix[i][j] + other.component(a  ,     a			)
                 for j in range(self.__width			)
             ]
             matrix.append(a			)
          return Matrix(a  ,     self.__width  ,     self.__height			)
       else:
          raise Exception("matrix must have the same dimension!"			)




    def __sub__(			self  :       List[str]  ,     a  :       Matrix			):






       '''simple docstring'''






       if self.__width == other.width() and self.__height == other.height():
          lowercase_	:    Optional[Any]           =     []
          for i in range(self.__height			):
             lowercase_	:    Union[str, Any]           =     [
                 self.__matrix[i][j] - other.component(a  ,     a			)
                 for j in range(self.__width			)
             ]
             matrix.append(a			)
          return Matrix(a  ,     self.__width  ,     self.__height			)
       else:
          raise Exception("matrices must have the same dimension!"			)




    @overload
    def __mul__(			self  :       Tuple  ,     a  :       float			):






       '''simple docstring'''






       ...




    @overload
    def __mul__(			self  :       List[str]  ,     a  :       Vector			):






       '''simple docstring'''






       ...




    def __mul__(			self  :       Dict  ,     a  :       float | Vector			):






       '''simple docstring'''






       if isinstance(a  ,     a			):  # matrix-vector
          if len(a			) == self.__width:
             lowercase_	:    Optional[Any]           =     zero_vector(self.__height			)
             for i in range(self.__height			):
                lowercase_	:    List[Any]           =     [
                    self.__matrix[i][j] * other.component(a			)
                    for j in range(self.__width			)
                ]
                ans.change_component(a  ,     sum(a			)			)
             return ans
          else:
             raise Exception(
                 "vector must have the same size as the "
                 "number of columns of the matrix!"			)
       elif isinstance(a  ,     (int, float)			):  # matrix-scalar
          lowercase_	:    int           =     [
              [self.__matrix[i][j] * other for j in range(self.__width			)]
              for i in range(self.__height			)
          ]
          return Matrix(a  ,     self.__width  ,     self.__height			)
       return None




    def 				lowerCAmelCase__				(			self  :       Optional[int]			):






       '''simple docstring'''






       return self.__height




    def 				lowerCAmelCase__				(			self  :       int			):






       '''simple docstring'''






       return self.__width




    def 				lowerCAmelCase__				(			self  :       List[str]  ,     a  :       int  ,     a  :       int			):






       '''simple docstring'''






       if 0 <= x < self.__height and 0 <= y < self.__width:
          return self.__matrix[x][y]
       else:
          raise Exception("change_component: indices out of bounds"			)




    def 				lowerCAmelCase__				(			self  :       Tuple  ,     a  :       int  ,     a  :       int  ,     a  :       float			):






       '''simple docstring'''






       if 0 <= x < self.__height and 0 <= y < self.__width:
          lowercase_	:    Union[str, Any]           =     value
       else:
          raise Exception("change_component: indices out of bounds"			)




    def 				lowerCAmelCase__				(			self  :       Dict  ,     a  :       int  ,     a  :       int			):






       '''simple docstring'''






       if self.__height != self.__width:
          raise Exception("Matrix is not square"			)
       lowercase_	:    List[str]           =     self.__matrix[:x] + self.__matrix[x + 1 :]
       for i in range(len(a			)			):
          lowercase_	:    int           =     minor[i][:y] + minor[i][y + 1 :]
       return Matrix(a  ,     self.__width - 1  ,     self.__height - 1			).determinant()




    def 				lowerCAmelCase__				(			self  :       Union[str, Any]  ,     a  :       int  ,     a  :       int			):






       '''simple docstring'''






       if self.__height != self.__width:
          raise Exception("Matrix is not square"			)
       if 0 <= x < self.__height and 0 <= y < self.__width:
          return (-1) ** (x + y) * self.minor(a  ,     a			)
       else:
          raise Exception("Indices out of bounds"			)





    def 				lowerCAmelCase__				(			self  :       Any			):






       '''simple docstring'''






       if self.__height != self.__width:
          raise Exception("Matrix is not square"			)
       if self.__height < 1:
          raise Exception("Matrix has no element"			)
       elif self.__height == 1:
          return self.__matrix[0][0]
       elif self.__height == 2:
          return (
              self.__matrix[0][0] * self.__matrix[1][1]
              - self.__matrix[0][1] * self.__matrix[1][0]
          )
       else:
          lowercase_	:    Dict           =     [
              self.__matrix[0][y] * self.cofactor(0  ,     a			) for y in range(self.__width			)
          ]
          return sum(a			)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase							):


   """simple docstring"""




   lowercase_	:    list[list[float]]           =     [[0] * n for _ in range(_UpperCamelCase							)]
   return Matrix(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)





def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							):


   """simple docstring"""




   random.seed(_UpperCamelCase							)
   lowercase_	:    list[list[float]]           =     [
       [random.randint(_UpperCamelCase						,		_UpperCamelCase							) for _ in range(_UpperCamelCase							)] for _ in range(_UpperCamelCase							)
   ]
   return Matrix(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)












'''simple docstring'''
from __future__ import annotations

UpperCamelCase__ =			[-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0]
UpperCamelCase__ =			[-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase							):


   """simple docstring"""




   lowercase_	:    str           =     []
   lowercase_	:    List[str]           =     len(_UpperCamelCase							)

   for i in range(_UpperCamelCase							):
      lowercase_	:    float           =     -1
      for j in range(i + 1						,		_UpperCamelCase							):
         if arr[i] < arr[j]:
            lowercase_	:    Union[str, Any]           =     arr[j]
            break
      result.append(_UpperCamelCase							)
   return result




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase							):


   """simple docstring"""




   lowercase_	:    List[str]           =     []
   for i, outer in enumerate(_UpperCamelCase							):
      lowercase_	:    float           =     -1
      for inner in arr[i + 1 :]:
         if outer < inner:
            lowercase_	:    Optional[Any]           =     inner
            break
      result.append(_UpperCamelCase							)
   return result




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase							):


   """simple docstring"""




   lowercase_	:    List[str]           =     len(_UpperCamelCase							)
   lowercase_	:    list[float]           =     []
   lowercase_	:    list[float]           =     [-1] * arr_size

   for index in reversed(range(_UpperCamelCase							)							):
      if stack:
         while stack[-1] <= arr[index]:
            stack.pop()
            if not stack:
               break
      if stack:
         lowercase_	:    Optional[Any]           =     stack[-1]
      stack.append(arr[index]							)
   return result


if __name__ == "__main__":
 from doctest import testmod
 from timeit import timeit

 testmod()
 print(next_greatest_element_slow(arr))
 print(next_greatest_element_fast(arr))
 print(next_greatest_element(arr))

 UpperCamelCase__ =			(
     'from __main__ import arr, next_greatest_element_slow, '
     'next_greatest_element_fast, next_greatest_element'
 )
 print(
     'next_greatest_element_slow():',
     timeit('next_greatest_element_slow(arr)', setup=setup),
 )
 print(
     'next_greatest_element_fast():',
     timeit('next_greatest_element_fast(arr)', setup=setup),
 )
 print(
     '     next_greatest_element():',
     timeit('next_greatest_element(arr)', setup=setup),
 )




