d={} def T(level, ball): if d.get((level, ball)): return d[(level, ball)] if ball is 1: return level elif level is 1: return 1 elif level is 0: return 0 res_list=[] for l in range(1,level+1): res_list.append(max(T(l-1, ball-1), T(level-l, ball)) + 1 ) res = min(res_list) d[(level, ball)] = res return res print(T(39,2))