fun main(args:Array) { val n = readLine()!!.toInt() val ms = readLine()!!.split(" ").map(String::toInt) var targ = (1..n).toSet() - ms.toSet() val o = mutableMapOf() ms.map { if( o[it] == null ) o[it] = 0 o[it] = o[it]!! + 1 } var o2 = o.toList().filter { it.second > 1 }.toMutableList() var result = 0 while(true) { val p = o2.map { val (k,v) = it targ.map { ta -> Triple(ta, k, Math.abs( ta - k ) ) } }.flatten().minBy { it.third }!! //println( p ) targ = targ.filter { it != p.first }.toMutableSet() o2 = o2.map { val (k,v) = it if( k == p.second ) Pair(k, v-1) else Pair(k, v) }.filter { it.second > 1 } .toMutableList() //println(o2) result += p.third if( o2.size == 0 ) break } println(result) }