def max_sum(tri, n):
    if n == 0:
        return 0

    dp = tri[0][:]

    for row in range(1, n):
        current = tri[row][:]
        current[0] += dp[0]
        current[row] += dp[row - 1]

        for col in range(1, row):
            current[col] += max(dp[col - 1], dp[col])

        dp = current

    return max(dp)