class Solution(object):
    def minPathSum(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        if len(grid) == 0:
            return grid
        dp = [[99999 for _ in range(len(grid[0]))] for _ in range(len(grid))]
        dp[0][0] = grid[0][0]

        for i in range(1, len(grid[0])):
            dp[0][i] = dp[0][i-1] + grid[0][i]

        for i in range(1, len(grid)):
            dp[i][0] = dp[i-1][0] + grid[i][0]

        for j in range(1, len(grid[0])):
            for i in range(1, len(grid)):
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]

        return dp[-1][-1]

if __name__ == '__main__':
    g = [[1,2,5],[3,2,1]]

    r = Solution().minPathSum(g)
    print(r)