def max_sum(tri, n):
    # Work bottom-up: for each row, add the max of the two children below
    dp = [row[:] for row in tri]
    
    for i in range(n - 2, -1, -1):
        for j in range(i + 1):
            dp[i][j] += max(dp[i + 1][j], dp[i + 1][j + 1])
    
    return dp[0][0]