C++ 300ms solution
First we have to prevent duplicate visiting.\nFor example when primes = {2,3}\n\n    4-12-36\n    |\n    2- 6-18-54\n    |\n    1- 3- 9-27\n\nStarting from 1 (t
First we have to prevent duplicate visiting.\nFor example when primes = {2,3}\n\n    4-12-36\n    |\n    2- 6-18-54\n    |\n    1- 3- 9-27\n\nStarting from 1 (the first number), if we have *3, then we should not *2 further. Similarly, if primes = {2,3,5}, then we should not *2 if we have *3, and should not *2 or *3 if we have *5. \n\nNow we can realize this idea by using std::priority_queue. We use pair&lt;int,int&gt; to record current number and the prime index we have multiplied.\n\n\tint nthSuperUglyNumber(const int n, const vector<int>& primes) {\n\t\ttypedef pair<int,int> IP;\n\t\tstruct Comp {\n\t\t\tbool operator()(const IP &l, const IP &r) {\n\t\t\t\treturn l.first > r.first;\n\t\t\t}\n\t\t};\n\t\tpriority_queue<IP, vector<IP>, Comp> pq;\n\t\tpq.emplace(1, 0);\n\t\tfor (int i = 1;; ++i) {\n\t\t\tconst IP t = pq.top();\n\t\t\tif (i == n) {\n\t\t\t\treturn t.first;\n\t\t\t}\n\t\t\tpq.pop();\n\t\t\tfor (int j = t.second; j < primes.size(); ++j) {\n\t\t\t\tconst long long lltmp = static_cast<long long>(t.first) * primes[j];\n\t\t\t\tconst int itmp = static_cast<int>(lltmp);\n\t\t\t\tif (itmp != lltmp) {\n\t\t\t\t\t// overflow, WTF\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tassert(itmp > 0);\n\t\t\t\tpq.emplace(itmp, j);\n\t\t\t}\n\t\t}\n\t\tassert(0);\n\t\treturn 0;\n\t}
['C++']