def cut_rod2(p, n, r={}):
    """Cut rod.

    Same functionality as the original but implemented
    as a top-down with memoization.
    """
    q = r.get(n, None)
    if q:
        return q
    else:
        if n == 0:
            return 0
        else:
            q = 0
            for i in range(n):
                q = max(q, p[i] + cut_rod2(p, n - (i + 1), r))
            r[n] = q
            return q