import math def dist(p, q): x1, y1 = p x2, y2 = q return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) from functools import lru_cache @lru_cache def calc(pizza,pos,bits): if pizza==0: return calc(K,0,bits)+dist(dot[0],dot[pos]) if bits==((1<<(N+1))-1): return dist(dot[0],dot[pos]) ret=None if pizza!=K: ret=calc(K,0,bits)+dist(dot[0],dot[pos]) for i in range(1,N+1): if not (bits>>i)&1: now=calc(pizza-1,i,bits|(1<