

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

from collections import namedtuple
from dataclasses import dataclass







@dataclass
class 							A__   :
  A__					=    42
  A__					=    None
  A__					=    None

lowerCamelCase			:				Union[str, Any] 			=					namedtuple("CoinsDistribResult", "moves excess")


def 	_lowerCAmelCase (						_UpperCamelCase						:					TreeNode | None    )				->      List[str]:
     """simple docstring"""




     if root is None:
          return 0

     # Validation
     def count_nodes(_UpperCamelCase						:					TreeNode | None    ) -> int:
          if node is None:
               return 0

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

     def count_coins(_UpperCamelCase						:					TreeNode | None    ) -> int:
          if node is None:
               return 0

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

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

     # Main calculation
     def get_distrib(_UpperCamelCase						:					TreeNode | None    ) -> CoinsDistribResult:

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

          _SCREAMING_SNAKE_CASE							   =get_distrib(node.left    )
          _SCREAMING_SNAKE_CASE							   =get_distrib(node.right    )

          _SCREAMING_SNAKE_CASE							   =1 - left_distrib_excess
          _SCREAMING_SNAKE_CASE							   =1 - right_distrib_excess

          _SCREAMING_SNAKE_CASE							   =(
              left_distrib_moves
              + right_distrib_moves
              + abs(__SCREAMING_SNAKE_CASE    )
              + abs(__SCREAMING_SNAKE_CASE    )
          )
          _SCREAMING_SNAKE_CASE							   =node.data - coins_to_left - coins_to_right

          return CoinsDistribResult(__SCREAMING_SNAKE_CASE							,			__SCREAMING_SNAKE_CASE    )

     return get_distrib(__SCREAMING_SNAKE_CASE    )[0]


if __name__ == "__main__":
     import doctest

     doctest.testmod()



'''simple docstring'''
import os


def 	_lowerCAmelCase (						)				->      List[str]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =os.path.dirname(os.path.realpath(_UpperCamelCase    )    )
     _SCREAMING_SNAKE_CASE							   =os.path.join(_UpperCamelCase							,			'triangle.txt'    )

     with open(_UpperCamelCase    ) as f:
          _SCREAMING_SNAKE_CASE							   =f.readlines()

     _SCREAMING_SNAKE_CASE							   =[]
     for line in triangle:
          _SCREAMING_SNAKE_CASE							   =[]
          for number in line.strip().split(' '    ):
               numbers_from_line.append(int(_UpperCamelCase    )    )
          a.append(_UpperCamelCase    )

     for i in range(1							,			len(_UpperCamelCase    )    ):
          for j in range(len(a[i]    )    ):
               _SCREAMING_SNAKE_CASE							   =a[i - 1][j] if j != len(a[i - 1]    ) else 0
               _SCREAMING_SNAKE_CASE							   =a[i - 1][j - 1] if j > 0 else 0
               a[i][j] += max(_UpperCamelCase							,			_UpperCamelCase    )
     return max(a[-1]    )


if __name__ == "__main__":
     print(solution())
