import scala.annotation.tailrec import scala.io.StdIn object Problem205 { def proc(N: Int, S: Seq[String]): String = { @tailrec def rec(result: Seq[Char], S: Seq[String]): String = { if (S.isEmpty) return result.reverse.mkString("") val sorted = S.sorted val (next, others) = (sorted.head, sorted.tail) val nextChar: Char = next.head val remains: Seq[String] = next.tail match { case "" => others case x if x.startsWith("{") => others case x => (x + "{") +: others } rec(nextChar +: result, remains) } val padS = S.map(s => s + ("{" * (N - s.length))) rec(Nil, padS) } def main(args: Array[String]) = { val N = StdIn.readInt() val S = Seq.fill(N)(StdIn.readLine()) val result: String = proc(N, S) println(result) } }