





'''simple docstring'''


from __future__ import annotations

from collections import namedtuple
from dataclasses import dataclass
@dataclass
class 				a_       :
     __A								=      42
     __A								=      None
     __A								=      None







lowerCAmelCase :					List[str] 							=namedtuple('''CoinsDistribResult''', '''moves excess''')
def 							UpperCAmelCase_  (						__lowerCamelCase : Any			):




   if root is None:
      return 0

   # Validation
   def count_nodes(__lowerCamelCase : Any			) -> int:
      if node is None:
         return 0

      return count_nodes(node.left			) + count_nodes(node.right			) + 1

   def count_coins(__lowerCamelCase : Optional[Any]			) -> int:
      if node is None:
         return 0

      return count_coins(node.left			) + count_coins(node.right			) + node.data

   if count_nodes(__lowerCamelCase			) != count_coins(__lowerCamelCase			):
      raise ValueError("The nodes number should be same as the number of coins"			)

   # Main calculation
   def get_distrib(__lowerCamelCase : Optional[Any]			) -> CoinsDistribResult:

      if node is None:
         return CoinsDistribResult(0     ,1			)

      lowercase_      ,							lowercase_      :Optional[int]         =			get_distrib(node.left			)
      lowercase_      ,							lowercase_      :str         =			get_distrib(node.right			)

      lowercase_      :Optional[int]         =			1 - left_distrib_excess
      lowercase_      :int         =			1 - right_distrib_excess

      lowercase_      :Optional[Any]         =			(
          left_distrib_moves
          + right_distrib_moves
          + abs(__lowerCamelCase			)
          + abs(__lowerCamelCase			)
      )
      lowercase_      :List[Any]         =			node.data - coins_to_left - coins_to_right

      return CoinsDistribResult(__lowerCamelCase     ,__lowerCamelCase			)

   return get_distrib(__lowerCamelCase			)[0]


if __name__ == "__main__":
 import doctest

 doctest.testmod()









'''simple docstring'''


import numpy as np
def 							UpperCAmelCase_  (						__lowerCamelCase : str     ,__lowerCamelCase : Optional[Any]     ,__lowerCamelCase : int     ,__lowerCamelCase : List[str]     ,__lowerCamelCase : List[Any]			):
   lowercase_      :Dict         =			int(np.ceil((x_end - xa) / h			)			)
   lowercase_      :Optional[int]         =			np.zeros((n + 1,)			)
   lowercase_      :Tuple         =			ya
   lowercase_      :List[str]         =			xa

   for k in range(__lowerCamelCase			):
      lowercase_      :Any         =			f(__lowerCamelCase     ,y[k]			)
      lowercase_      :Optional[Any]         =			f(x + 0.5 * h     ,y[k] + 0.5 * h * ka			)
      lowercase_      :Optional[Any]         =			f(x + 0.5 * h     ,y[k] + 0.5 * h * ka			)
      lowercase_      :Any         =			f(x + h     ,y[k] + h * ka			)
      lowercase_      :Tuple         =			y[k] + (1 / 6) * h * (ka + 2 * ka + 2 * ka + ka)
      x += h

   return y


if __name__ == "__main__":
 import doctest

 doctest.testmod()


