from heapq import heappop,heappush def main1(n,m,p,a): if any([x>m for x in a]):return 1 d={} mxv=0 for x in a: mxv=max(mxv,x) cnt=1 while x%p==0: cnt+=1 x//=p if x==1:continue if cnt in d:d[cnt]=max(d[cnt],x) else:d[cnt]=x if len(d)==0:return -1 todo=[[0,1]] costary=[0]*1000 costary[0]=1 while todo: c,v=heappop(todo) if costary[c]>v:continue if v>m:return c if v*mxv>m:return c+1 for key,value in d.items(): if costary[c+key]