import java.util.PriorityQueue import kotlin.math.max fun main() { val (n, r) = readln().split(" ").map { it.toInt() } val minScore = .5 * r * n * (3 * n + 1) val ranking = System.`in`.bufferedReader().lineSequence().take(r).map { line -> line.split(" ").map { it.toInt() } }.toList() // Pre calculate the score for all pairs i -> (i, i+1)..(i, 2n) val scores = mutableMapOf, Int>() (1 until 2 * n).forEach { a -> (a + 1..2 * n).forEach { b -> val rank = ranking.sumOf { r -> max(r[a - 1], r[b - 1]) } scores[a to b] = rank } } // greedy search for an answer val queue = PriorityQueue, Set>>> { a, b -> -a.first.compareTo(b.first) } val seen = mutableSetOf>>() scores.forEach { (k, v) -> val set = setOf(k.first, k.second) val pairSet = setOf(k) queue.add(Triple(v, set, pairSet)) seen.add(pairSet) } val combiCache = mutableMapOf, List>>() while (queue.isNotEmpty()) { val (score, current, pairSet) = queue.poll() if (score >= minScore) return println("possible") val options = combiCache.getOrPut(current) { (1 until 2 * n).filter { it !in current }.flatMap { a -> (a + 1..2 * n).filter { it !in current }.map { b -> a to b } } } options.map { Triple(it, current + setOf(it.first, it.second), pairSet + setOf(it)) } .filter { seen.add(it.third) } .map { (c, s, p) -> Triple(scores[c]!! + score, s, p) } .toCollection(queue) } println("impossible") }