#NEWLINE# def nthSuperUglyNumber( self , n , primes ) : #NEWLINE# #INDENT# '\n        :type n: int\n        :type primes: List[int]\n        :rtype: int\n        ' #NEWLINE# ( uglies , idx , heap ) = ( [ 1 ] , ( [ 0 ] * len ( primes ) ) , [ ] ) #NEWLINE# for ( k , p ) in enumerate ( primes ) : #NEWLINE# #INDENT# heapq . heappush ( heap , ( p , k ) ) #NEWLINE# #UNINDENT# for i in xrange ( 1 , n ) : #NEWLINE# #INDENT# ( min_val , k ) = heap [ 0 ] #NEWLINE# uglies += [ i ] #NEWLINE# while ( heap [ 0 ] [ 0 ] == min_val ) : #NEWLINE# #INDENT# ( min_val , k ) = heapq . heappop ( heap ) #NEWLINE# idx [ k ] += 1 #NEWLINE# heapq . heappush ( heap , ( ( primes [ k ] * uglies [ idx [ k ] ] ) , k ) ) #NEWLINE# #UNINDENT# #UNINDENT# return uglies [ ( - 1 ) ]