The straightforward solution is to use a big heap (i.e. priority_queue in C++) to store all currently generated ugly numbers and keep track of the minimum one. Everytime, we remove the minimum one and add the newly generated ugly numbers in the queue (i.e the minimum one X primes[i], for i=[0,K-1] and remove the duplicates). This will require roughly O(NK) space and O(Nlog(NK), worst case) time.

It is very slow since there are many ordering information we didn't use in the above algorithm (e.g. min*prime[i]< min*prime[j], if i<j). So we should try to use such information to speed up our algorithm. Can we regroup the currently generated ugly number in a way that each group is in a ascending order and we only need to use a heap to find the min of the first elements of all the groups?

One option would be divide all the currently generated ugly numbers in K groups, each time, we remove the top (i.e. min) number from priority_queue, we put the newly generated ugly number top()*primes[i] to the i-th group so that the i-th group includes all the currently generated ugly numbers that are multiple of primes[i]. Of course, this will include some duplicates (e.g 6 will be in the group of 2 and also in the group of 3). Since in this way, all the groups are in the ascending order (i.e. Group[i][m] > Group[i][n], if m>n). So we only need to use a K-element heap to track the next minimum ugly number. So the time complexity goes down. However, the memory complexity is still O(NK). We will get MLE error.

Can we do better? One thing we notice is that for all elements in the i_th group has the format Ugly[m]*primes[i]. So we don't need to save all those elements but just Ugly[0..n-1]. The i_th group (i.e. multiple of primes[i]) has elements [Ugly[0] * primes[i], Ugly[1] * primes[i], ..., Ugly[m] * primes[i]]. We just need to track the index of Ugly (which indicates which Ugly element) should be used to generate the next minimum element in the i_th group, which will be push in the priority_queue. Now we reduce the memory complexity to O(N). But due to the duplicates issue, the time complexity could be O(NKlogK). Can we do further optimization?

The below code has run time of 1020ms

```
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) { // assuming inputs are valid
int K = primes.size(), res[n], lastMul[n] ={0}, i, curVal, curIdx; //lastMul is the indices of K groups
priority_queue<pair<int, int>> headHeap; // K-element heap to find the minimum of the minimum of K groups
for(res[0]=-1, i=0; i<K;++i) // initialize the heap
headHeap.push(make_pair(-primes[i], i)); // first the ugly number value (change to negative to get the minimum one), second is the index of ugly number queues, i.e. candidates[i]
for(i=0;i<n;) //generate the first n ugly number
{ // the elements in the priority_queue are negative to help us find the minimum
curVal = headHeap.top().first;
curIdx = headHeap.top().second; // group index of the current minum ugly number
headHeap.pop();
if(curVal!=res[i]) res[++i] = curVal; // if it is not a duplicate, the top one will the ++i_th ugly number
headHeap.push(make_pair(res[++lastMul[curIdx]] * primes[curIdx], curIdx)); // generate the next minimum element of the curIdx_th group and push it in the heap
}
return -res[n-1];
}
};
```

(Thanks for StefanPochmann's comments that inspired this optimization)

To avoid duplicates, we can use extra memory (i.e.pIdx) to save the index of the maximum prime factor of each ugly number that treat each ugly number only as multiple of its maximum prime factor. For example 6 is treated only as multiple of 3 , not multiple of 2. So it can only be generated as 2 x 3, not 3x2. In that way, no duplicate will occur. The below version has N heap insert operations and run time of 230ms.

```
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
int K = primes.size(), res[n], pIdx[n], lastMul[n] ={}, i, curVal, curIdx;
priority_queue<pair<int, int>> headHeap; //
for(res[0]=-1, i=0; i<K;++i) headHeap.push(make_pair(-primes[i], i)); // first the ugly number value (change to negative to get the minimum one), second is the index of ugly number queues, i.e. candidates[i]
for(i=1;i<n;++i)
{
curVal = headHeap.top().first;
curIdx = headHeap.top().second;
headHeap.pop();
res[i] = curVal; pIdx[i] = curIdx; // pIdx is the index of the maximum prime factor
while(pIdx[++lastMul[curIdx]]>curIdx); // skip ugly numbers whose maximum factor index is larger than curIdx.
headHeap.push(make_pair(res[lastMul[curIdx]] * primes[curIdx], curIdx));
}
return -res[n-1];
}
};
```