与第18题的做法完全相同,结果是$7273$。
T=[]
with open("067_triangle.txt") as f:
for line in f:
T.append([int(i) for i in line.split()])
maxPath=[[59]]
for row in T[1:]:
maxPath.append([maxPath[-1][0]+row[0]]+[val+max(maxPath[-1][col],maxPath[-1][col+1]) for col,val in enumerate(row[1:-1])]+[row[-1]+maxPath[-1][-1]])
print max(maxPath[-1])
|