Project Euler #82: Path sum: three ways

  • + 0 comments
    Python
    M, DP = [], []
    for _ in range(int(input())):
        M.append(list(map(int, input().split())))
        DP.append(M[-1][-1])
    
    for i in range(len(M)-2, -1, -1):
        DP[0] += M[0][i]
        for j in range(1, len(M)):
            DP[j] = min(DP[j], DP[j-1]) + M[j][i]
    
        for j in range(len(M)-2, -1, -1):
            DP[j] = min(DP[j], DP[j+1] + M[j][i])
    
    print(min(DP))