Skip to content

Instantly share code, notes, and snippets.

@Izaron
Created June 17, 2016 12:17
Show Gist options
  • Save Izaron/4c71c1e0876925d5f7e05aea113e72ef to your computer and use it in GitHub Desktop.
Save Izaron/4c71c1e0876925d5f7e05aea113e72ef to your computer and use it in GitHub Desktop.
fun main(args: Array<String>) {
val pairs = (1..readLine()!!.toInt()).map {
val (a, b) = readLine()!!.split(' ').map(String::toInt)
Pair<Int, Int>(Math.min(a, b), Math.max(b, a))
}
print(when {
pairs.all { it.first % 3 == 0 && it.second % 3 == 0 } -> 0 // Has no answers
else -> {
val sum = pairs.sumBy { it.first }
when {
sum % 3 != 0 -> sum // Sum not divisible by 3
else -> sum + pairs.map { it.second - it.first }.filter { it % 3 != 0 }.min()!!.toInt() // Minimal answer
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment