// @EXPECTED_RESULTS@: ACCEPTED, TIME_LIMIT_EXCEEDED /** * This tries to solve the problem by building a trie of all the usernames. It uses recursion, so it might fail on the * stack size but should be fine with a max depth of 1000 chars. * * Same idea, but rather than using fancy datastructures with recursion, a flat approach which basically does the same. */ fun main() { val words = System.`in`.bufferedReader().lineSequence().drop(1).map { it to 1 }.toList() val wordsQueue = mutableListOf(words) var usernames = 0 while (wordsQueue.isNotEmpty()) { val current = wordsQueue.removeLast() val map = current.groupBy { it.first.first() } usernames += map.entries.filter { it.value.size == 1 }.sumOf { it.value.first().second } map.values.filter { it.size > 1 }.mapTo(wordsQueue) { list -> list.map { it.first.drop(1) to it.second + 1 } } } println(usernames) }