fun main(args: Array) { val numberCount = readLine()!!.toInt() val numberList = readLine()!!.split(" ").map { it.toInt() } getPrimeList() numberList.forEach { getPrimeDivide(it) } var ans = mutableListOf() ans.add(numberList[0]) var subList = numberList.drop(1).sorted().toMutableList() while (subList.size > 0) { val target = ans.last() var minNum = 10000 var minKoubai = 100000000 var minIdx = -1 for(i in subList.indices) { val koubai = getMinKoubai(target, subList[i]) if(koubai >= minKoubai) { continue } minNum = subList[i] minKoubai = koubai minIdx = i } subList.removeAt(minIdx) ans.add(minNum) } println(ans.joinToString(" ")) } val dic = mutableMapOf>() val primeDivideDic = mutableMapOf>() val primeList = mutableListOf() fun getMinKoubai(num1:Int, num2:Int):Int { if(num1 > num2) { return getMinKoubai(num2, num1) } if(!dic.containsKey(num1)) { dic[num1] = mutableMapOf() } dic[num1]!![num2]?.let { return it } val keys = primeDivideDic[num1]!!.keys.union(primeDivideDic[num2]!!.keys) var num = 1 keys.forEach{ var maxCount = 1 primeDivideDic[num1]!![it]?.let { maxCount = it } primeDivideDic[num2]!![it]?.let { maxCount = Math.max(maxCount, it) } num = num * Math.pow(it.toDouble(), maxCount.toDouble()).toInt() } dic[num1]!![num2] = num return num } fun getPrimeList() { val max = 10000 val flags = arrayOfNulls(max + 1) val list = mutableListOf() for(i in 2..max) { if (flags[i] != null && flags[i]!!) { continue } val maxIndex = max/i for(j in 1..maxIndex) { flags[i*j] = true } primeList.add(i) } } fun getPrimeDivide(num: Int) { if(primeDivideDic.containsKey(num)) { return } val primeDivide = mutableMapOf() var subNum = num for (prime in primeList) { if(subNum % prime > 0) { continue } var cnt = 0 while (subNum%prime == 0) { cnt++ subNum = subNum / prime } primeDivide[prime] = cnt } primeDivideDic[num] = primeDivide }