classSolution:defminimumPerimeter(self, neededApples:int)->int:
l, r =1,10**5while l < r:
mid =(l + r)>>1if2*(2*(mid **3)+3*(mid **2)+ mid)>= neededApples:
r = mid
else:
l = mid +1return8* l
classSolution:defminimumCost(self, source:str, target:str, original: List[str], changed: List[str], cost: List[int])->int:
dis =[[inf]*26for _ inrange(26)]for i inrange(26):
dis[i][i]=0for x, y, c inzip(original, changed, cost):
x =ord(x)-ord('a')
y =ord(y)-ord('a')
dis[x][y]=min(dis[x][y], c)for k inrange(26):for i inrange(26):for j inrange(26):
dis[i][j]=min(dis[i][j], dis[i][k]+ dis[k][j])
ans =sum(dis[ord(x)-ord('a')][ord(y)-ord('a')]for x, y inzip(source, target))return ans if ans < inf else-1