

import sys



def 			_A	(      SCREAMING_SNAKE_CASE      ):
	UpperCAmelCase__:	Optional[int]									= len(SCREAMING_SNAKE_CASE      )
	UpperCAmelCase__:	Optional[int]									= [[0 for x in range(SCREAMING_SNAKE_CASE      )] for x in range(SCREAMING_SNAKE_CASE      )]
	UpperCAmelCase__:	List[Any]									= [[0 for x in range(SCREAMING_SNAKE_CASE      )] for x in range(SCREAMING_SNAKE_CASE      )]

	for chain_length in range(2	,SCREAMING_SNAKE_CASE      ):
		for a in range(1	,n - chain_length + 1      ):
			UpperCAmelCase__:	str									= a + chain_length - 1

			UpperCAmelCase__:	Tuple									= sys.maxsize
			for c in range(SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE      ):
				UpperCAmelCase__:	int									= (
				    matrix[a][c] + matrix[c + 1][b] + array[a - 1] * array[c] * array[b]
				)
				if cost < matrix[a][b]:
					UpperCAmelCase__:	Union[str, Any]									= cost
					UpperCAmelCase__:	Any									= c
	return matrix, sol



def 			_A	(      SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE      ):
	if i == j:
		print("A" + str(SCREAMING_SNAKE_CASE      )	,end=" "      )
	else:
		print("("	,end=" "      )
		print_optiomal_solution(SCREAMING_SNAKE_CASE	,SCREAMING_SNAKE_CASE	,optimal_solution[i][j]      )
		print_optiomal_solution(SCREAMING_SNAKE_CASE	,optimal_solution[i][j] + 1	,SCREAMING_SNAKE_CASE      )
		print(")"	,end=" "      )



def 			_A	(      ):
	UpperCAmelCase__:	str									= [3_0, 3_5, 1_5, 5, 1_0, 2_0, 2_5]
	UpperCAmelCase__:	int									= len(SCREAMING_SNAKE_CASE      )
	# Size of matrix created from above array will be
	# 30*35 35*15 15*5 5*10 10*20 20*25
	UpperCAmelCase__							,						UpperCAmelCase__:	Dict									= matrix_chain_order(SCREAMING_SNAKE_CASE      )

	print("No. of Operation required: " + str(matrix[1][n - 1]      )      )
	print_optiomal_solution(SCREAMING_SNAKE_CASE	,1	,n - 1      )


if __name__ == "__main__":
					main()


from __future__ import annotations

from typing import Any

class __UpperCamelCase     :






					'''simple docstring'''



					def __init__(				self				,      lowerCamelCase__						):
						UpperCAmelCase__:	Optional[int]									= num_of_nodes
						UpperCAmelCase__:	list[list[int]]									= []
						UpperCAmelCase__:	dict[int, int]									= {}





					def    _UpperCAmelCase    (				self				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__						):
						self.m_edges.append([u_node, v_node, weight]						)





					def    _UpperCAmelCase    (				self				,      lowerCamelCase__						):

						if self.m_component[u_node] == u_node:
							return u_node
						return self.find_component(self.m_component[u_node]						)





					def    _UpperCAmelCase    (				self				,      lowerCamelCase__						):

						if self.m_component[u_node] != u_node:
							for k in self.m_component:
								UpperCAmelCase__:	Optional[Any]									= self.find_component(lowerCamelCase__						)





					def    _UpperCAmelCase    (				self				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__						):

						if component_size[u_node] <= component_size[v_node]:
							UpperCAmelCase__:	Tuple									= v_node
							component_size[v_node] += component_size[u_node]
							self.set_component(lowerCamelCase__						)

						elif component_size[u_node] >= component_size[v_node]:
							UpperCAmelCase__:	Dict									= self.find_component(lowerCamelCase__						)
							component_size[u_node] += component_size[v_node]
							self.set_component(lowerCamelCase__						)





					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	Dict									= []
						UpperCAmelCase__:	int									= 0

						UpperCAmelCase__:	list[Any]									= [-1] * self.m_num_of_nodes

						# A list of components (initialized to all of the nodes)
						for node in range(self.m_num_of_nodes						):
							self.m_component.update({node: node}						)
							component_size.append(1						)

						UpperCAmelCase__:	Union[str, Any]									= self.m_num_of_nodes

						while num_of_components > 1:
							for edge in self.m_edges:
								UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__:	str									= edge

								UpperCAmelCase__:	str									= self.m_component[u]
								UpperCAmelCase__:	Optional[Any]									= self.m_component[v]

								if u_component != v_component:

									for component in (u_component, v_component):
										if (
										    minimum_weight_edge[component] == -1
										    or minimum_weight_edge[component][2] > w
										):
											UpperCAmelCase__:	List[Any]									= [u, v, w]

							for edge in minimum_weight_edge:
								if isinstance(lowerCamelCase__				,      lowerCamelCase__						):
									UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__:	str									= edge

									UpperCAmelCase__:	str									= self.m_component[u]
									UpperCAmelCase__:	int									= self.m_component[v]

									if u_component != v_component:
										mst_weight += w
										self.union(lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__						)
										print(F"Added edge [{u} - {v}]\nAdded weight: {w}\n"						)
										num_of_components -= 1

							UpperCAmelCase__:	Any									= [-1] * self.m_num_of_nodes
						print(F"The total weight of the minimal spanning tree is: {mst_weight}"						)



def 			_A	(      ):
	pass


if __name__ == "__main__":
					import doctest

					doctest.testmod()