/* What does the formula .5rn(3n+1) mean? r = number of topics for every topic the score should be at least .5n(3n+1) -> divide by .5n -> 3n+1 n=2 (1 2 3 4) -> (1 2 3 4) -> min 7 = 3 + 4 = 7 n=4 (1 2 3 4 5 6 7 8 ) -> (.5 1 1.5 2 2.5 3 3.5 4) -> min 13 -> 4 + 3.5 + 3 + 2.5 = 13 Meaning every round we require to have the best matches So every pair should at least have a score of (n+1 .. 2n) for every topic The formula enforces that every round we want an optimal matching with max scores For the first contest divide in 2 groups with scores in (n+1, 2n) and those not create a vector to if their other scores will provide a winning score if all vectors have their inverse present in the other group then it is possible, else impossible By sorting we can make the search doable in O(n log n) */ fun main() { val (n, r) = readln().split(" ").map { it.toInt() } val ranking = System.`in`.bufferedReader().lineSequence().take(r).map { line -> line.split(" ").map { it.toInt() } }.toList() // build the vectors (as strings for simpler sorting) val students = (0 until 2 * n) .map { s -> ranking.map { if (it[s] > n) 1 else 0 }.joinToString(separator = "") } .groupBy { it[0] } // sort for easier search val winners = students['1']!!.sorted() // flip the inverse of the vectors and sort them val losers = students['0']!!.map { l -> l.map { if (it == '1') '0' else '1' }.joinToString(separator = "") }.sorted() // the matching of winners and the flipped losers should be the same contents if (winners.zip(losers).all { (a, b) -> a == b }) println("possible") else println("impossible") }