
"""simple docstring"""







_SCREAMING_SNAKE_CASE	       =      range(2, 20 + 1)
_SCREAMING_SNAKE_CASE	       =      [10**k for k in range(ks[-1] + 1)]
_SCREAMING_SNAKE_CASE	       =      {}
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     Dict:







							"""simple docstring"""


							__snake_case     			=       sum(a_i[j] for j in range(SCREAMING_SNAKE_CASE							,    len(SCREAMING_SNAKE_CASE ) ) )
							__snake_case     			=       sum(a_i[j] * base[j] for j in range(min(len(SCREAMING_SNAKE_CASE )							,    SCREAMING_SNAKE_CASE ) ) )

							__snake_case			,   __snake_case     			=       0, 0
							__snake_case     			=       n - i

							__snake_case     			=       memo.get(SCREAMING_SNAKE_CASE )

							if sub_memo is not None:
														__snake_case     			=       sub_memo.get(SCREAMING_SNAKE_CASE )

														if jumps is not None and len(SCREAMING_SNAKE_CASE ) > 0:
																					# find and make the largest jump without going over
																					__snake_case     			=       -1
																					for _k in range(len(SCREAMING_SNAKE_CASE ) - 1							,    -1							,    -1 ):
																												if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
																																			__snake_case     			=       _k
																																			break

																					if max_jump >= 0:
																												__snake_case			,   __snake_case			,   __snake_case     			=       jumps[max_jump]
																												# since the difference between jumps is cached, add c
																												__snake_case     			=       diff + c
																												for j in range(min(SCREAMING_SNAKE_CASE							,    len(SCREAMING_SNAKE_CASE ) ) ):
																																			__snake_case			,   __snake_case     			=       divmod(SCREAMING_SNAKE_CASE							,    10 )
																												if new_c > 0:
																																			add(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )

														else:
																					__snake_case     			=       []
							else:
														__snake_case     			=       {c: []}
														__snake_case     			=       sub_memo

							if dn >= max_dn or c + diff >= base[k]:
														return diff, dn

							if k > ks[0]:
														while True:
																					# keep doing smaller jumps
																					__snake_case			,   __snake_case     			=       next_term(SCREAMING_SNAKE_CASE							,    k - 1							,    i + dn							,    SCREAMING_SNAKE_CASE )
																					diff += _diff
																					dn += terms_jumped

																					if dn >= max_dn or c + diff >= base[k]:
																												break
							else:
														# would be too small a jump, just compute sequential terms instead
														__snake_case			,   __snake_case     			=       compute(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    i + dn							,    SCREAMING_SNAKE_CASE )
														diff += _diff
														dn += terms_jumped

							__snake_case     			=       sub_memo[c]

							# keep jumps sorted by # of terms skipped
							__snake_case     			=       0
							while j < len(SCREAMING_SNAKE_CASE ):
														if jumps[j][1] > dn:
																					break
														j += 1

							# cache the jump for this value digitsum(b) and c
							sub_memo[c].insert(SCREAMING_SNAKE_CASE							,    (diff, dn, k) )
							return (diff, dn)
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     str:







							"""simple docstring"""


							if i >= n:
														return 0, i
							if k > len(SCREAMING_SNAKE_CASE ):
														a_i.extend([0 for _ in range(k - len(SCREAMING_SNAKE_CASE ) )] )

							# note: a_i -> b * 10^k + c
							# ds_b -> digitsum(b)
							# ds_c -> digitsum(c)
							__snake_case     			=       i
							__snake_case			,   __snake_case			,   __snake_case     			=       0, 0, 0
							for j in range(len(SCREAMING_SNAKE_CASE ) ):
														if j >= k:
																					ds_b += a_i[j]
														else:
																					ds_c += a_i[j]

							while i < n:
														i += 1
														__snake_case     			=       ds_c + ds_b
														diff += addend
														__snake_case     			=       0
														for j in range(SCREAMING_SNAKE_CASE ):
																					__snake_case     			=       a_i[j] + addend
																					__snake_case			,   __snake_case     			=       divmod(SCREAMING_SNAKE_CASE							,    10 )

																					ds_c += a_i[j]

														if addend > 0:
																					break

							if addend > 0:
														add(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )
							return diff, i - start_i
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     Optional[Any]:







							"""simple docstring"""


							for j in range(SCREAMING_SNAKE_CASE							,    len(SCREAMING_SNAKE_CASE ) ):
														__snake_case     			=       digits[j] + addend
														if s >= 10:
																					__snake_case			,   __snake_case     			=       divmod(SCREAMING_SNAKE_CASE							,    10 )
																					__snake_case     			=       addend // 10 + quotient
														else:
																					__snake_case     			=       s
																					__snake_case     			=       addend // 10

														if addend == 0:
																					break

							while addend > 0:
														__snake_case			,   __snake_case     			=       divmod(SCREAMING_SNAKE_CASE							,    10 )
														digits.append(SCREAMING_SNAKE_CASE )
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE = 10**15 )  ->     int:







							"""simple docstring"""


							__snake_case     			=       [1]
							__snake_case     			=       1
							__snake_case     			=       0
							while True:
														__snake_case			,   __snake_case     			=       next_term(SCREAMING_SNAKE_CASE							,    20							,    i + dn							,    SCREAMING_SNAKE_CASE )
														dn += terms_jumped
														if dn == n - i:
																					break

							__snake_case     			=       0
							for j in range(len(SCREAMING_SNAKE_CASE ) ):
														a_n += digits[j] * 10**j
							return a_n


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


"""simple docstring"""







from __future__ import annotations

from dataclasses import dataclass







@dataclass
class 							__magic_name__     :
					_SCREAMING_SNAKE_CASE       :					float
					_SCREAMING_SNAKE_CASE       :					TreeNode | None     							=   None
					_SCREAMING_SNAKE_CASE       :					TreeNode | None     							=   None




def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE )  ->     bool:







							"""simple docstring"""


							def is_valid_tree(SCREAMING_SNAKE_CASE ) -> bool:
														if node is None:
																					return True

														if not isinstance(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE ):
																					return False

														try:
																					float(node.data )
														except (TypeError, ValueError):
																					return False

														return is_valid_tree(node.left ) and is_valid_tree(node.right )

							if not is_valid_tree(SCREAMING_SNAKE_CASE ):
														raise ValueError(
														    "Each node should be type of TreeNode and data should be float." )

							def is_binary_search_tree_recursive_check(
							    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE ) -> bool:

														if node is None:
																					return True

														return (
														    left_bound < node.data < right_bound
														    and is_binary_search_tree_recursive_check(node.left							,    SCREAMING_SNAKE_CASE							,    node.data )
														    and is_binary_search_tree_recursive_check(
														        node.right							,    node.data							,    SCREAMING_SNAKE_CASE )
														)

							return is_binary_search_tree_recursive_check(SCREAMING_SNAKE_CASE							,    -float("inf" )							,    float("inf" ) )


if __name__ == "__main__":
		import doctest

		doctest.testmod()
