注意 : 一定要加上当前数字 不要忘加了
Python 代码
N = 550
trangle = []
dp = [[0 for _ in range(N)] for _ in range(N)]
if __name__ == '__main__':
n = int(input())
for i in range(n):
trangle.append(list(map(int,input().split())))
dp[0][0] = trangle[0][0]
for i in range(1,n):
for j in range(i+1):
if j == 0:
dp[i][j] = dp[i-1][j] + trangle[i][j]
elif j == i:
dp[i][j] = dp[i-1][j-1] + trangle[i][j]
else:
dp[i][j] = max(dp[i-1][j],dp[i-1][j-1])
dp[i][j] += trangle[i][j]
ans = -2e9
for i in range(n):
if dp[n-1][i] > ans:
ans = dp[n-1][i]
print(ans)