public int nthSuperUglyNumber(int n, int[] primes) {
        if(n==1) return 1;
        int[]ugly=new int[n];
        ugly[0]=1;
        int[] index=new int[primes.length];
        Arrays.fill(index,0);
        for (int pointer=1; pointer<n;pointer++){
            int nextValue=Integer.MAX_VALUE,nextIndex=0;
            for(int i=0;i<index.length;++i){
                while (ugly[index[i]]*primes[i]<=ugly[pointer-1]) index[i]++;
                if(ugly[index[i]]*primes[i] < nextValue){
                    nextValue=ugly[index[i]]*primes[i];
                    nextIndex=i;
                }
            }
            ugly[pointer]=nextValue;
            index[nextIndex]++;
        }
        return ugly[n-1];
    }