import scala.annotation.tailrec import scala.io.StdIn object Problem190 { val DRY: Int = -1 val MOIST: Int = 0 val WET: Int = 1 def humidity(a: Int, b: Int): Int = (a + b).compare(0) def dry(s: IndexedSeq[Int]): Int = { @tailrec def rec(result: Int, s: IndexedSeq[Int]): Int = { if (s.length < 2) return result humidity(s.head, s.last) match { case DRY => rec(result + 1, s.tail.init) case MOIST => rec(result, s.init) case WET => rec(result, s.init) } } rec(0, s) } def wet(s: IndexedSeq[Int]): Int = { @tailrec def rec(result: Int, s: IndexedSeq[Int]): Int = { if (s.length < 2) return result humidity(s.head, s.last) match { case DRY => rec(result, s.tail) case MOIST => rec(result, s.tail) case WET => rec(result + 1, s.tail.init) } } rec(0, s) } def moist(s: IndexedSeq[Int]): Int = { @tailrec def rec(result: Int, s: IndexedSeq[Int]): Int = { if (s.length < 2) return result humidity(s.head, s.last) match { case DRY => rec(result, s.tail) case MOIST => rec(result + 1, s.tail.init) case WET => rec(result, s.init) } } rec(0, s) } def proc(N: Int, A: IndexedSeq[Int]): (Int, Int, Int) = { val s = A.sorted (dry(s), wet(s), moist(s)) } def main(args: Array[String]) { val N = StdIn.readInt() val A = StdIn.readLine().split(" ").map(_.toInt).toIndexedSeq val (dry, wet, moist) = proc(N, A) println(s"$dry $wet $moist") } }