
'''simple docstring'''

import colorsys
import io

import cva
import matplotlib as mpl
import matplotlib.colors as mplc
import matplotlib.figure as mplfigure
import numpy as np
import torch
from matplotlib.backends.backend_agg import FigureCanvasAgg

from utils import img_tensorize


_UpperCAmelCase   :      List[str] 						=  1000

class 			UpperCAmelCase				:





		"""simple docstring"""





		def __init__(    self  ,					_snake_case  ,					_snake_case=1.2  ,					_snake_case="g"  ,					_snake_case=0.5  ,					_snake_case="-"  ,					_snake_case="test_out.jpg"  ,					_snake_case=True  ,					_snake_case=False  ,					_snake_case=None  ,					_snake_case=None  ,					_snake_case=0.7  ,					)				->    List[str]:
				if isinstance(_snake_case  ,					torch.Tensor     ):
						_UpperCamelCase       : Tuple    =      img.numpy().astype('''np.uint8'''     )
				if isinstance(_snake_case  ,					_snake_case     ):
						_UpperCamelCase       : Union[str, Any]    =      img_tensorize(_snake_case     )
				assert isinstance(_snake_case  ,					np.ndarray     )

				_UpperCamelCase,							_UpperCamelCase       : List[Any]    =      img.shape[1], img.shape[0]
				_UpperCamelCase       : Optional[int]    =      mplfigure.Figure(frameon=_snake_case     )
				_UpperCamelCase       : Optional[Any]    =      fig.get_dpi()
				_UpperCamelCase       : Tuple    =      (width * scale + 1E-2) / dpi
				_UpperCamelCase       : Tuple    =      (height * scale + 1E-2) / dpi
				fig.set_size_inches(_snake_case  ,					_snake_case     )
				_UpperCamelCase       : Any    =      fig.add_axes([0.0, 0.0, 1.0, 1.0]     )
				ax.axis('''off'''     )
				ax.set_xlim(0.0  ,					_snake_case     )
				ax.set_ylim(_snake_case     )

				_UpperCamelCase       : str    =      saveas
				_UpperCamelCase       : Union[str, Any]    =      rgb
				_UpperCamelCase       : Dict    =      pynb
				_UpperCamelCase       : List[Any]    =      img
				_UpperCamelCase       : str    =      edgecolor
				_UpperCamelCase       : Dict    =      0.5
				_UpperCamelCase       : Dict    =      linestyle
				_UpperCamelCase       : Any    =      int(np.sqrt(min(_snake_case  ,					_snake_case     )     ) * scale // 3     )
				_UpperCamelCase       : Tuple    =      width
				_UpperCamelCase       : List[Any]    =      height
				_UpperCamelCase       : List[Any]    =      scale
				_UpperCamelCase       : Tuple    =      fig
				_UpperCamelCase       : List[Any]    =      ax
				_UpperCamelCase       : Optional[int]    =      pad
				_UpperCamelCase       : List[str]    =      idaobj
				_UpperCamelCase       : Optional[int]    =      idaattr
				_UpperCamelCase       : List[str]    =      FigureCanvasAgg(_snake_case     )
		def _lowercase  (    self  ,					_snake_case  ,					_snake_case=None     )				->    List[str]:
				if color is None:
						_UpperCamelCase       : List[Any]    =      self.edgecolor
				((_UpperCamelCase),							(_UpperCamelCase),							(_UpperCamelCase),							(_UpperCamelCase))       : Optional[Any]    =      box
				_UpperCamelCase       : Optional[Any]    =      xa - xa
				_UpperCamelCase       : Any    =      ya - ya
				self.ax.add_patch(
				    mpl.patches.Rectangle(
				        (xa, ya)  ,					_snake_case  ,					_snake_case  ,					fill=_snake_case  ,					edgecolor=_snake_case  ,					linewidth=self.font_size // 3  ,					alpha=self.alpha  ,					linestyle=self.linestyle  ,					)     )
		def _lowercase  (    self  ,					_snake_case  ,					_snake_case=None  ,					_snake_case=None  ,					_snake_case=None  ,					_snake_case=None     )				->    List[Any]:
				if len(boxes.shape     ) > 2:
						_UpperCamelCase       : Optional[int]    =      boxes[0]
				if len(obj_ids.shape     ) > 1:
						_UpperCamelCase       : Tuple    =      obj_ids[0]
				if len(obj_scores.shape     ) > 1:
						_UpperCamelCase       : Any    =      obj_scores[0]
				if len(attr_ids.shape     ) > 1:
						_UpperCamelCase       : str    =      attr_ids[0]
				if len(attr_scores.shape     ) > 1:
						_UpperCamelCase       : Dict    =      attr_scores[0]
				if isinstance(_snake_case  ,					torch.Tensor     ):
						_UpperCamelCase       : str    =      boxes.numpy()
				if isinstance(_snake_case  ,					_snake_case     ):
						_UpperCamelCase       : Dict    =      np.array(_snake_case     )
				assert isinstance(_snake_case  ,					np.ndarray     )
				_UpperCamelCase       : Optional[int]    =      np.prod(boxes[:, 2:] - boxes[:, :2]  ,					axis=1     )
				_UpperCamelCase       : str    =      np.argsort(-areas     ).tolist()
				_UpperCamelCase       : Tuple    =      boxes[sorted_idxs] if boxes is not None else None
				_UpperCamelCase       : Optional[int]    =      obj_ids[sorted_idxs] if obj_ids is not None else None
				_UpperCamelCase       : List[Any]    =      obj_scores[sorted_idxs] if obj_scores is not None else None
				_UpperCamelCase       : int    =      attr_ids[sorted_idxs] if attr_ids is not None else None
				_UpperCamelCase       : Any    =      attr_scores[sorted_idxs] if attr_scores is not None else None

				_UpperCamelCase       : Dict    =      [self._random_color(maximum=1     ) for _ in range(len(_snake_case     )     )]
				_UpperCamelCase       : Optional[Any]    =      [assigned_colors[idx] for idx in sorted_idxs]
				if obj_ids is not None:
						_UpperCamelCase       : str    =      self._create_text_labels_attr(_snake_case  ,					_snake_case  ,					_snake_case  ,					_snake_case     )
						for i in range(len(_snake_case     )     ):
								_UpperCamelCase       : Optional[int]    =      assigned_colors[i]
								self.add_box(boxes[i]  ,					_snake_case     )
								self.draw_labels(labels[i]  ,					boxes[i]  ,					_snake_case     )
		def _lowercase  (    self  ,					_snake_case  ,					_snake_case  ,					_snake_case     )				->    Union[str, Any]:
				_UpperCamelCase,							_UpperCamelCase,							_UpperCamelCase,							_UpperCamelCase       : Any    =      box
				_UpperCamelCase       : List[Any]    =      (xa, ya)
				_UpperCamelCase       : Optional[Any]    =      (ya - ya) * (xa - xa)
				_UpperCamelCase       : List[str]    =      _SMALL_OBJ * self.scale
				if instance_area < small or ya - ya < 40 * self.scale:
						if ya >= self.height - 5:
								_UpperCamelCase       : Any    =      (xa, ya)
						else:
								_UpperCamelCase       : Union[str, Any]    =      (xa, ya)

				_UpperCamelCase       : str    =      (ya - ya) / np.sqrt(self.height * self.width     )
				_UpperCamelCase       : Any    =      self._change_color_brightness(_snake_case  ,					brightness_factor=0.7     )
				_UpperCamelCase       : List[Any]    =      np.clip((height_ratio - 0.02) / 0.08 + 1  ,					1.2  ,					2     )
				font_size *= 0.75 * self.font_size

				self.draw_text(
				    text=_snake_case  ,					position=_snake_case  ,					color=_snake_case  ,					)
		def _lowercase  (    self  ,					_snake_case  ,					_snake_case  ,					_snake_case="g"  ,					_snake_case="left"  ,					)				->    str:
				_UpperCamelCase       : List[Any]    =      0
				_UpperCamelCase       : Union[str, Any]    =      self.font_size
				_UpperCamelCase       : Optional[Any]    =      np.maximum(list(mplc.to_rgb(_snake_case     )     )  ,					0.2     )
				_UpperCamelCase       : Dict    =      max(0.8  ,					np.max(_snake_case     )     )
				_UpperCamelCase       : int    =      {
				    '''facecolor''': '''black''',
				    '''alpha''': self.alpha,
				    '''pad''': self.pad,
				    '''edgecolor''': '''none''',
				}
				_UpperCamelCase,							_UpperCamelCase       : List[Any]    =      position
				self.ax.text(
				    _snake_case  ,					_snake_case  ,					_snake_case  ,					size=font_size * self.scale  ,					family='''sans-serif'''  ,					bbox=_snake_case  ,					verticalalignment='''top'''  ,					horizontalalignment=_snake_case  ,					color=_snake_case  ,					zorder=10  ,					rotation=_snake_case  ,					)
		def _lowercase  (    self  ,					_snake_case=None     )				->    Dict:
				if saveas is None:
						_UpperCamelCase       : List[str]    =      self.saveas
				if saveas.lower().endswith('''.jpg'''     ) or saveas.lower().endswith('''.png'''     ):
						cva.imwrite(
						    _snake_case  ,					self._get_buffer()[:, :, ::-1]  ,					)
				else:
						self.fig.savefig(_snake_case     )
		def _lowercase  (    self  ,					_snake_case  ,					_snake_case  ,					_snake_case  ,					_snake_case     )				->    Optional[int]:
				_UpperCamelCase       : Union[str, Any]    =      [self.idaobj[i] for i in classes]
				_UpperCamelCase       : Optional[int]    =      [self.idaattr[i] for i in attr_classes]
				_UpperCamelCase       : str    =      [
				    F'''{label} {score:.2f} {attr} {attr_score:.2f}'''
				    for label, score, attr, attr_score in zip(_snake_case  ,					_snake_case  ,					_snake_case  ,					_snake_case     )
				]
				return labels
		def _lowercase  (    self  ,					_snake_case  ,					_snake_case     )				->    int:
				_UpperCamelCase       : List[str]    =      [self.idaobj[i] for i in classes]
				if scores is not None:
						if labels is None:
								_UpperCamelCase       : Optional[int]    =      ['''{:.0f}%'''.format(s * 100     ) for s in scores]
						else:
								_UpperCamelCase       : str    =      ['''{} {:.0f}%'''.format(_snake_case  ,					s * 100     ) for li, s in zip(_snake_case  ,					_snake_case     )]
				return labels
		def _lowercase  (    self  ,					_snake_case=255     )				->    Optional[int]:
				_UpperCamelCase       : Union[str, Any]    =      np.random.randint(0  ,					len(_COLORS     )     )
				_UpperCamelCase       : Optional[Any]    =      _COLORS[idx] * maximum
				if not self.rgb:
						_UpperCamelCase       : Dict    =      ret[::-1]
				return ret
		def _lowercase  (    self     )				->    Optional[Any]:
				if not self.pynb:
						_UpperCamelCase,							_UpperCamelCase       : str    =      self.canvas.print_to_buffer()
						if (width, height) != (self.width, self.height):
								_UpperCamelCase       : List[str]    =      cva.resize(self.img  ,					(width, height)     )
						else:
								_UpperCamelCase       : Union[str, Any]    =      self.img
				else:
						_UpperCamelCase       : List[Any]    =      io.BytesIO()  # works for cairo backend
						self.canvas.print_rgba(_snake_case     )
						_UpperCamelCase,							_UpperCamelCase       : Optional[Any]    =      self.width, self.height
						_UpperCamelCase       : Optional[Any]    =      buf.getvalue()
						_UpperCamelCase       : List[str]    =      self.img

				_UpperCamelCase       : str    =      np.frombuffer(_snake_case  ,					dtype='''uint8'''     )
				_UpperCamelCase       : Dict    =      buffer.reshape(_snake_case  ,					_snake_case  ,					4     )
				_UpperCamelCase,							_UpperCamelCase       : List[Any]    =      np.split(_snake_case  ,					[3]  ,					axis=2     )

				try:
						import numexpr as ne  # fuse them with numexpr

						_UpperCamelCase       : List[str]    =      ne.evaluate('''img * (1 - alpha / 255.0) + rgb * (alpha / 255.0)'''     )
				except ImportError:
						_UpperCamelCase       : Optional[Any]    =      alpha.astype('''float32'''     ) / 255.0
						_UpperCamelCase       : Optional[int]    =      img * (1 - alpha) + rgb * alpha

				return visualized_image.astype('''uint8'''     )




		def _lowercase  (    self  ,					_snake_case  ,					_snake_case     )				->    Optional[Any]:
				assert brightness_factor >= -1.0 and brightness_factor <= 1.0
				_UpperCamelCase       : List[Any]    =      mplc.to_rgb(_snake_case     )
				_UpperCamelCase       : Optional[Any]    =      colorsys.rgb_to_hls(*mplc.to_rgb(_snake_case     )     )
				_UpperCamelCase       : Optional[Any]    =      polygon_color[1] + (brightness_factor * polygon_color[1])
				_UpperCamelCase       : str    =      0.0 if modified_lightness < 0.0 else modified_lightness
				_UpperCamelCase       : Tuple    =      1.0 if modified_lightness > 1.0 else modified_lightness
				_UpperCamelCase       : List[str]    =      colorsys.hls_to_rgb(polygon_color[0]  ,					_snake_case  ,					polygon_color[2]     )
				return modified_color







_UpperCAmelCase   :      Union[str, Any] 						=  (
    np.array(
        [
            0.000,
            0.447,
            0.741,
            0.850,
            0.325,
            0.098,
            0.929,
            0.694,
            0.125,
            0.494,
            0.184,
            0.556,
            0.466,
            0.674,
            0.188,
            0.301,
            0.745,
            0.933,
            0.635,
            0.078,
            0.184,
            0.300,
            0.300,
            0.300,
            0.600,
            0.600,
            0.600,
            1.000,
            0.000,
            0.000,
            1.000,
            0.500,
            0.000,
            0.749,
            0.749,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.333,
            0.333,
            0.000,
            0.333,
            0.667,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            0.333,
            0.000,
            0.667,
            0.667,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            1.000,
            0.000,
            0.000,
            0.333,
            0.500,
            0.000,
            0.667,
            0.500,
            0.000,
            1.000,
            0.500,
            0.333,
            0.000,
            0.500,
            0.333,
            0.333,
            0.500,
            0.333,
            0.667,
            0.500,
            0.333,
            1.000,
            0.500,
            0.667,
            0.000,
            0.500,
            0.667,
            0.333,
            0.500,
            0.667,
            0.667,
            0.500,
            0.667,
            1.000,
            0.500,
            1.000,
            0.000,
            0.500,
            1.000,
            0.333,
            0.500,
            1.000,
            0.667,
            0.500,
            1.000,
            1.000,
            0.500,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.333,
            0.333,
            1.000,
            0.333,
            0.667,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.667,
            0.333,
            1.000,
            0.667,
            0.667,
            1.000,
            0.667,
            1.000,
            1.000,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            1.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.143,
            0.143,
            0.143,
            0.857,
            0.857,
            0.857,
            1.000,
            1.000,
            1.000,
        ]
    )
    .astype(np.floataa)
    .reshape(-1, 3)
)







'''simple docstring'''

import os







def 						snake_case__	(      ) ->      Tuple:
		_UpperCamelCase       : Dict    =      os.path.dirname(os.path.realpath(UpperCamelCase	)	)
		_UpperCamelCase       : str    =      os.path.join(UpperCamelCase			,'''triangle.txt'''	)

		with open(UpperCamelCase	) as f:
				_UpperCamelCase       : Optional[Any]    =      f.readlines()

		_UpperCamelCase       : List[str]    =      [[int(UpperCamelCase	) for y in x.rstrip('''\r\n'''	).split(''' '''	)] for x in triangle]

		for i in range(1			,len(UpperCamelCase	)	):
				for j in range(len(a[i]	)	):
						_UpperCamelCase       : str    =      a[i - 1][j] if j != len(a[i - 1]	) else 0
						_UpperCamelCase       : Any    =      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())





