<p>A <strong>super ugly number</strong> is a positive integer whose prime factors are in the array <code>primes</code>.</p>

<p>Given an integer <code>n</code> and an array of integers <code>primes</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>super ugly number</strong></em>.</p>

<p>The <code>n<sup>th</sup></code> <strong>super ugly number</strong> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> signed integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 12, primes = [2,7,13,19]
<strong>Output:</strong> 32
<strong>Explanation:</strong> [1,2,4,7,8,13,14,16,19,26,28,32] is the sequence of the first 12 super ugly numbers given primes = [2,7,13,19].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1, primes = [2,3,5]
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are in the array primes = [2,3,5].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= primes.length &lt;= 100</code></li>
	<li><code>2 &lt;= primes[i] &lt;= 1000</code></li>
	<li><code>primes[i]</code> is <strong>guaranteed</strong> to be a prime number.</li>
	<li>All the values of <code>primes</code> are <strong>unique</strong> and sorted in <strong>ascending order</strong>.</li>
</ul>

class Solution {
public:
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        
        vector<long> dp(n + 1, 0); 

        // Base-condition
        dp[1] = 1; // First ugly number is 1 itself

        vector<int> pointers(primes.size(), 1); // Index of the prime array is representing the prime number ka location, & the value of the pointers will be the place where it is pointing within the dp array 

        // Since there are multiple prime numbers, just take them as an array. The value within this array will be the index of the prime number of the primes vector. 

        for(int i = 2; i<=n; i++)
        {
            // int mini = INT_MAX, mini_index = 0;

            int mini = INT_MAX;
            vector<int> mini_index; 

            // for(int i = 0; i<primes.size(); i++) {
            //     mini = min(mini, dp[pointers[i]] * primes[i]);
            //     if(mini == dp[pointers[i]] * primes[i]) mini_index = i;
            // }

            // dp[i] = mini; 
            // pointers[mini_index]++;

            for(int i = 0; i<primes.size(); i++) {

                long calc = dp[pointers[i]] * primes[i]; 
                if(mini == calc) {
                    mini_index.push_back(i);
                }
                else if(mini > calc) {
                    mini = calc;
                    mini_index.clear();
                    mini_index.push_back(i);
                }
            }

            dp[i] = mini; 
            for(auto it : mini_index) {
                pointers[it]++; 
            } 
        }
        
        return dp[n]; 
    }
};
43430