from heapq import heappop,heappush def main1(n,m,p,a): if any([x>m for x in a]):return 1 bd=m d={} for x in a: bd=min(bd,(m+1+x-1)//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>bd:return c+1 for key,value in d.items(): if costary[c+key]