fun main() { val builder = StringBuilder() val n = readInputLine().toInt() var plusCnt = 0 var minusCnt = 0 val numCnt = IntArray(10) readInputLine().split(" ").forEach { when(it) { "+" -> plusCnt++ "-" -> minusCnt++ else -> numCnt[it.toInt()]++ } } val digitCnt = n - plusCnt - minusCnt val allTermList = mutableListOf() var maxTermTmp = 0L var maxTermLen = 0 for (i in 9 downTo 0) { while (numCnt[i] != 0) { if (maxTermLen < digitCnt - plusCnt - minusCnt) { maxTermTmp = maxTermTmp * 10L + i.toLong() maxTermLen++ if (maxTermLen == digitCnt - plusCnt - minusCnt) { allTermList.add(maxTermTmp) } } else { allTermList.add(i.toLong()) } numCnt[i]-- } } allTermList.sortDescending() var max = 0L for ((i, t) in allTermList.withIndex()) { if (i <= plusCnt) { max += t } else { max -= t } } var min = 0L for ((i, t) in allTermList.withIndex()) { if (i < minusCnt) { min -= t } else { min += t } } builder.appendln("$max $min") print(builder.toString()) } fun readInputLine(): String { return readLine()!! }