Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her some jewelry.He bought n pieces of jewelry. The i-th piece has price equal to i + 1, that is, the prices of the jewelry are 2, 3, 4, ... n + 1.Watson gave Sherlock a challenge to color these jewelry pieces such that two pieces don't have the same color if the price of one piece is a prime divisor of the price of the other piece. Also, Watson asked him to minimize the number of different colors used.Help Sherlock complete this trivial task.

The only line contains single integer n (1 ≤ n ≤ 100000) — the number of jewelry pieces.

The first line of output should contain a single integer k, the minimum number of colors that can be used to color the pieces of jewelry with the given constraints. The next line should consist of n space-separated integers (between 1 and k) that specify the color of each piece in the order of increasing price. If there are multiple ways to color the pieces using k colors, you can output any of them.

NoteIn the first input, the colors for first, second and third pieces of jewelry having respective prices 2, 3 and 4 are 1, 1 and 2 respectively.In this case, as 2 is a prime divisor of 4, colors of jewelry having prices 2 and 4 must be distinct.

//package solutions

import java.io.PrintWriter
import java.util.Scanner

import scala.annotation.tailrec
import scala.collection.parallel.mutable.ParSet

/**
  * @author traff
  */

object SolTaskB extends App {

  def sieveOfEratosthenes(limit: Int) = {
    val (primes: ParSet[Int], sqrtLimit) = (ParSet.empty ++ (2 to limit), math.sqrt(limit).toInt)

    @tailrec
    def prim(candidate: Int): Unit = {
      if (candidate <= sqrtLimit) {
        if (primes contains candidate) primes --= candidate * candidate to limit by candidate
        prim(candidate + 1)
      }
    }

    prim(2)
    primes
  }

  override def main(args: Array[String]): Unit = {
    val out = new PrintWriter(System.out)

    run(new Scanner(System.in), out)

    out.close()
  }

  def run(s: Scanner, out: PrintWriter): Unit = {
    val n = s.nextInt()

    val primes = sieveOfEratosthenes(n + 1)

    var colors = new Array[Int](n)

    if (n > 2) {
      out.println(2)

      for (i <- 2 to n + 1) {
        if (primes contains i) {
          out.print(s"1 ")
        } else {
          out.print(s"2 ")
        }
      }
    } else {
      out.println(1)
      for (i <- 2 to n + 1) {
        out.print(s"1 ")
      }
    }
  }

}

object B776 {

  import IO._
  import collection.{mutable => cu}
  import scala.collection.mutable.ArrayBuffer
  case class Sieve(to: Int) {
    val primes = scala.collection.mutable.ArrayBuffer.empty[Int]
    val isComposite = Array.fill(to + 1)(false)
    def sieve(): Unit = { // Works in linear time
      for (i <- 2 until to) {
        if (!isComposite(i)) primes.append(i)
        var j = 0
        var break = false
        while (j < primes.size && i * primes(j) < to && !break) {
          isComposite(i * primes(j)) = true
          if (i % primes(j) == 0)
            break = true
          j += 1
        }
      }
    }
  }
  val s = Sieve(100000 + 10)
  s.sieve()
  def main(args: Array[String]): Unit = {
    val n = readInt
    if (n <= 2)
      out.println(1)
    else
      out.println(2)
    out.println(
      s.isComposite
        .slice(2, n + 2)
        .map { if (_) 2 else 1 }
        .mkString(" ")
    )
    out.close()
  }

  object IO {
    private val input = new java.io.BufferedReader(
      new java.io.InputStreamReader(
        if (System.getProperty("ONLINE_JUDGE") != null) System.in
        else new java.io.FileInputStream("src/input")
      )
    )
    val out = new java.io.PrintWriter(System.out)

    @inline def read: String = input.readLine()

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInt: Int = tokenizeLine.nextToken.toInt

    def readInts(n: Int): Array[Int] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toInt)
    }

    def readLongs(n: Int): Array[Long] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toLong)
    }

    def readBigs(n: Int): Array[BigInt] = {
      val tl = tokenizeLine
      Array.fill(n)(BigInt(tl.nextToken))
    }
  }

}

import Utils._, annotation._, collection._, util._, control._, math.Ordering.Implicits._, Searching._

object _776B extends CodeForcesApp {
  override def apply(io: IO) = {import io.{read, write, writeAll, writeLine}
    val n = read[Int]
    val ans = Array.ofDim[Int](n)

    for {
      i <- 0 until n if ans(i) == 0
      _ = ans(i) = 1
      j <- 2*(i+2) - 2 until n by (i + 2)
    } ans(j) = 2

    write(ans.max).writeLine().writeAll(ans)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
object Utils {
  import scala.collection.mutable, mutable.{Map => Dict}
  implicit class GenericExtensions[A](val a: A) extends AnyVal {
    def in(set: collection.Set[A]): Boolean = set(a)
    def some: Option[A] = Some(a)
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }
  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](val t: C[A]) extends AnyVal {
    def onlyElement: A = assuming(t.size == 1)(t.head)
    def groupByIdentifier[K](f: A => K): Map[K, A] = t.groupBy(f).mapValues(_.onlyElement)
    def duplicates(implicit b: C of A): C[A] = {
      val elems = mutable.Set.empty[A]
      (t collect {case i if !elems.add(i) => i}).to[C]
    }
    def sumWith[B](f: A => B)(implicit n: Numeric[B]): B = t.foldLeft(n.zero) {case (p, i) => n.plus(p, f(i))}
    def maxWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.max)
    def minWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.min)
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]
    def mapTo[B](f: A => B)(implicit b: C of (A, B)): Map[A, B] = zipWith(f).toUniqueMap
    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)
    def collectFirst[B](f: PartialFunction[A, Option[B]]): Option[B] = t.view collect f collectFirst {case Some(x) => x}
    def toMutableMultiSet: Dict[A, Int] = {
      val c = map[A] to 0
      t.foreach(i => c(i) += 1)
      c
    }
    def toMultiSet: Map[A, Int] = toMutableMultiSet.toMap withDefaultValue 0
    def X[B](s: Traversable[B]): Iterator[(A, B)] = for {a <- t.toIterator; b <- s} yield (a, b)
  }
  implicit class ArrayExtensions[A <: AnyRef](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = java.util.Arrays.sort(a, from, until, cmp) //sort [from, until)
  }
  implicit class ArrayExtensions2[A](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = a.view(from, until).sorted.copyToArray(a, from)
  }
  implicit class SeqExtensions[A, C[X] <: Seq[X]](val s: C[A]) extends AnyVal {
    def isRotationOf(t: Seq[A]): Boolean = s ++ s containsSlice t
  }
  implicit class PairExtensions[A, B](val p: (A, B)) extends AnyVal {
    def key = p._1
    def value = p._2
  }
  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_.key).mapValues(_.value)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C])
    def swap(implicit b: C of (B, A)): C[(B, A)] = t.map(_.swap).to[C]
  }
  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](val m: C[K, V]) extends AnyVal {
    def lastKeyOption: Option[K] = m.lastOption.map(_.key)
    def firstKeyOption: Option[K] = m.headOption.map(_.key)
    def invert: Map[V, Set[K]] = m.toSet[(K, V)].swap.toMultiMap
    def mapKeys[K2](f: K => K2): Map[K2, V] = m map {case (k, v) => f(k) -> v}
    def toMutable: Dict[K, V] = Dict.empty[K, V] ++ m
    def mapValuesSafely[V2](f: V => V2): Map[K, V2] = m.mapValues(f).view.force //Hack to get around https://issues.scala-lang.org/browse/SI-4776
  }
  implicit class MutableMapExtensions[K, V](val m: mutable.Map[K, V]) extends AnyVal {
    def removeLast(): Option[(K, V)] = m.lastOption.map(removeNode)
    def removeFirst(): Option[(K, V)] = m.headOption.map(removeNode)
    private def removeNode(kv: (K, V)): (K, V) = {
      m -= kv.key
      kv
    }
  }
  implicit class SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }
  implicit class IterableExtensions[A, C[A] <: Seq[A]](val s: C[A]) extends AnyVal {
    def indicesWhere(f: A => Boolean): Seq[Int] = s.zipWithIndex.collect {case (a, i) if f(a) => i}
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
    def foldUntil[B](z: B)(f: (B, A) => B)(c: B => Boolean): Option[(B, Int)] = s.view.scanLeft(z)(f).findWithIndex(c)
  }
  implicit class BooleanExtensions(val x: Boolean) extends AnyVal {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toEnglish = if(x) "YES" else "NO"
  }
  implicit class IntExtensions(val x: Int) extends AnyVal {
    import java.lang.{Integer => JInt}
    def withHighestOneBit: Int = JInt.highestOneBit(x)
    def withLowestOneBit: Int = JInt.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JInt.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JInt.numberOfTrailingZeros(x)
    def bitCount: Int = JInt.bitCount(x)
    def setLowestBits(n: Int): Int = x | (left(n) - 1)
    def clearLowestBits(n: Int): Int = x & left(n, ~0)
    def setBit(i: Int, set: Boolean): Int = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Int = x | left(i)
    def clearBit(i: Int): Int = x & ~left(i)
    def toggleBit(i: Int): Int = x ^ left(i)
    def testBit(i: Int): Boolean = ((x >> i) & 1) == 1
    private def left(bits: Int, num: Int = 1) = assuming(bits >= 0 && bits < JInt.SIZE)(num << bits)
    def X(y: Int): Iterator[(Int, Int)] = until(x) X until(y)
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    import java.lang.{Long => JLong}
    def withHighestOneBitSet: Long = JLong.highestOneBit(x)
    def withLowestOneBitSet: Long = JLong.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JLong.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JLong.numberOfTrailingZeros(x)
    def bitCount: Int = JLong.bitCount(x)
    def setLowestBits(n: Int): Long = x | (left(n) - 1)
    def clearLowestBits(n: Int): Long = x & left(n, ~0L)
    def setBit(i: Int, set: Boolean): Long = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Long = x | left(i)
    def clearBit(i: Int): Long = x & ~left(i)
    def toggleBit(i: Int): Long = x ^ left(i)
    def testBit(i: Int): Boolean = ((x >> i) & 1) == 1
    private def left(bits: Int, num: Long = 1L) = assuming(bits >= 0 && bits < JLong.SIZE)(num << bits)
  }
  implicit class NumericExtensions[A](val x: A)(implicit n: Numeric[A]) {
    import Numeric.Implicits._
    def **(i: Int): A = if (i == 0) n.one else {
      val h = x ** (i/2)
      if (i%2 == 0) h * h else h * h * x
    }
    def distance(y: A) = (x - y).abs()
    def nonNegative: Option[A] = when(n.gteq(x, n.zero))(x)
    def whenNegative(f: => A): A = nonNegative getOrElse f   //e.g. students indexOf "Greg" whenNegative Int.MaxValue
  }
  implicit class IntegralExtensions[A](val x: A)(implicit n: Integral[A]) {
    import scala.collection.immutable.NumericRange, Integral.Implicits._
    def mod(y: A): A = x - ((x / y) * y) //(x/y)*y + (x mod y) == x
    def -->(y: A): NumericRange.Inclusive[A] = NumericRange.inclusive(x, y, if (n.lteq(x, y)) n.one else -n.one)
    def ceilDiv(y: A): A = if (mod(y) == n.zero) x/y else n.one + (x/y)
  }
  implicit def toPoint[A](p: (A, A))(implicit n: Numeric[A]) = new java.awt.geom.Point2D.Double(n.toDouble(p._1), n.toDouble(p._2))
  def desc[A: Ordering]: Ordering[A] = asc[A].reverse  //e.g. students.sortBy(_.height)(desc)
  def asc[A](implicit order: Ordering[A]): Ordering[A] = order
  def map[K] = new {
    def to[V](default: => V): Dict[K, V] = using(_ => default)
    def using[V](f: K => V): Dict[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }
  def newMultiMap[K, V] = map[K] to mutable.Set.empty[V]
  def memoize[A, B](f: A => B): A ==> B = map[A] using f
  implicit class FuzzyDouble(val x: Double) extends AnyVal {
    def  >~(y: Double) = x > y + eps
    def >=~(y: Double) = x >= y + eps
    def  ~<(y: Double) = y >=~ x
    def ~=<(y: Double) = y >~ x
    def  ~=(y: Double) = (x distance y) <= eps
  }
  def toPolygon(points: Traversable[(Int, Int)]): java.awt.Polygon = {
    val (xs, ys) = points.unzip
    new java.awt.Polygon(xs.toArray, ys.toArray, points.size)
  }
  def toShape(points: Traversable[(Double, Double)]): java.awt.geom.GeneralPath = {
    val shape = new java.awt.geom.GeneralPath()
    points.headOption foreach {case (x, y) => shape.moveTo(x, y)}
    points.tail foreach {case (x, y) => shape.lineTo(x, y)}
    shape.closePath()
    shape
  }
  def until(until: Int): Range = 0 until until
  def not[A](f: A => Boolean): A => Boolean = {x: A => !f(x)}
  def assuming[A](cond: => Boolean)(f: => A): A = {require(cond); f}
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) f.some else None
  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  type ==>[A, B] = collection.Map[A, B]
  val mod: Int = (1e9 + 7).toInt
  val eps: Double = 1e-9
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    when(tokenizers.nonEmpty)(tokenizers.head)
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: C of A): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }
  object Read {
    implicit def collection[C[_], A: Read](implicit b: C of A): Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                       : Read[String]       = new Read(_.next())
    implicit val char                                         : Read[Char]         = string.map(_.toTraversable.onlyElement)
    implicit val int                                          : Read[Int]          = string.map(_.toInt)
    implicit val long                                         : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                       : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                       : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                   : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                     : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]            : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]   : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                      : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}

//package solutions

import java.io.PrintWriter
import java.util.Scanner

import scala.annotation.tailrec
import scala.collection.parallel.mutable.ParSet

/**
  * @author traff
  */

object SolTaskB extends App {

  def sieveOfEratosthenes(limit: Int) = {
    val (primes: ParSet[Int], sqrtLimit) = (ParSet.empty ++ (2 to limit), math.sqrt(limit).toInt)

    @tailrec
    def prim(candidate: Int): Unit = {
      if (candidate <= sqrtLimit) {
        if (primes contains candidate) primes --= candidate * candidate to limit by candidate
        prim(candidate + 1)
      }
    }

    prim(2)
    primes
  }

  override def main(args: Array[String]): Unit = {
    val out = new PrintWriter(System.out)

    run(new Scanner(System.in), out)

    out.close()
  }

  def run(s: Scanner, out: PrintWriter): Unit = {
    val n = s.nextInt()

    val primes = sieveOfEratosthenes(n+1)

    var colors = new Array[Int](n)

    out.println(2)

    for (i <- 2 to n+1) {
      if (primes contains i) {
        out.print(s"1 ")
      } else {
        out.print(s"2 ")
      }
    }
  }
}

import Utils._, annotation._, collection._, util._, control._, math.Ordering.Implicits._, Searching._

object _776B extends CodeForcesApp {
  override def apply(io: IO) = {import io.{read, write, writeAll, writeLine}
    val n = read[Int]
    val ans = Array.ofDim[Int](n)
    val illegal = Array.fill(n)(mutable.Set.empty[Int])

    for {
      i <- 0 until n
      color <- (1 to n).find(not(illegal(i)))
      _ = ans(i) = color
      j <- i until n by (i + 2)
    } illegal(j) += color

    write(ans.toSet.size)
      .writeLine()
      .writeAll(ans)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
object Utils {
  import scala.collection.mutable, mutable.{Map => Dict}
  implicit class GenericExtensions[A](val a: A) extends AnyVal {
    def in(set: collection.Set[A]): Boolean = set(a)
    def some: Option[A] = Some(a)
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }
  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](val t: C[A]) extends AnyVal {
    def onlyElement: A = assuming(t.size == 1)(t.head)
    def groupByIdentifier[K](f: A => K): Map[K, A] = t.groupBy(f).mapValues(_.onlyElement)
    def duplicates(implicit b: C of A): C[A] = {
      val elems = mutable.Set.empty[A]
      (t collect {case i if !elems.add(i) => i}).to[C]
    }
    def sumWith[B](f: A => B)(implicit n: Numeric[B]): B = t.foldLeft(n.zero) {case (p, i) => n.plus(p, f(i))}
    def maxWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.max)
    def minWith[B](f: A => B)(implicit ord: Ordering[B]): Option[B] = t.view.map(f).reduceOption(ord.min)
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]
    def mapTo[B](f: A => B)(implicit b: C of (A, B)): Map[A, B] = zipWith(f).toUniqueMap
    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)
    def collectFirst[B](f: PartialFunction[A, Option[B]]): Option[B] = t.view collect f collectFirst {case Some(x) => x}
    def toMutableMultiSet: Dict[A, Int] = {
      val c = map[A] to 0
      t.foreach(i => c(i) += 1)
      c
    }
    def toMultiSet: Map[A, Int] = toMutableMultiSet.toMap withDefaultValue 0
    def X[B](s: Traversable[B]): Iterator[(A, B)] = for {a <- t.toIterator; b <- s} yield (a, b)
  }
  implicit class ArrayExtensions[A <: AnyRef](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = java.util.Arrays.sort(a, from, until, cmp) //sort [from, until)
  }
  implicit class ArrayExtensions2[A](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = a.view(from, until).sorted.copyToArray(a, from)
  }
  implicit class SeqExtensions[A, C[X] <: Seq[X]](val s: C[A]) extends AnyVal {
    def isRotationOf(t: Seq[A]): Boolean = s ++ s containsSlice t
  }
  implicit class PairExtensions[A, B](val p: (A, B)) extends AnyVal {
    def key = p._1
    def value = p._2
  }
  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_.key).mapValues(_.value)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C])
    def swap(implicit b: C of (B, A)): C[(B, A)] = t.map(_.swap).to[C]
  }
  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](val m: C[K, V]) extends AnyVal {
    def lastKeyOption: Option[K] = m.lastOption.map(_.key)
    def firstKeyOption: Option[K] = m.headOption.map(_.key)
    def invert: Map[V, Set[K]] = m.toSet[(K, V)].swap.toMultiMap
    def mapKeys[K2](f: K => K2): Map[K2, V] = m map {case (k, v) => f(k) -> v}
    def toMutable: Dict[K, V] = Dict.empty[K, V] ++ m
    def mapValuesSafely[V2](f: V => V2): Map[K, V2] = m.mapValues(f).view.force //Hack to get around https://issues.scala-lang.org/browse/SI-4776
  }
  implicit class MutableMapExtensions[K, V](val m: mutable.Map[K, V]) extends AnyVal {
    def removeLast(): Option[(K, V)] = m.lastOption.map(removeNode)
    def removeFirst(): Option[(K, V)] = m.headOption.map(removeNode)
    private def removeNode(kv: (K, V)): (K, V) = {
      m -= kv.key
      kv
    }
  }
  implicit class SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }
  implicit class IterableExtensions[A, C[A] <: Seq[A]](val s: C[A]) extends AnyVal {
    def indicesWhere(f: A => Boolean): Seq[Int] = s.zipWithIndex.collect {case (a, i) if f(a) => i}
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
    def foldUntil[B](z: B)(f: (B, A) => B)(c: B => Boolean): Option[(B, Int)] = s.view.scanLeft(z)(f).findWithIndex(c)
  }
  implicit class BooleanExtensions(val x: Boolean) extends AnyVal {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toEnglish = if(x) "YES" else "NO"
  }
  implicit class IntExtensions(val x: Int) extends AnyVal {
    import java.lang.{Integer => JInt}
    def withHighestOneBit: Int = JInt.highestOneBit(x)
    def withLowestOneBit: Int = JInt.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JInt.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JInt.numberOfTrailingZeros(x)
    def bitCount: Int = JInt.bitCount(x)
    def setLowestBits(n: Int): Int = x | (left(n) - 1)
    def clearLowestBits(n: Int): Int = x & left(n, ~0)
    def setBit(i: Int, set: Boolean): Int = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Int = x | left(i)
    def clearBit(i: Int): Int = x & ~left(i)
    def toggleBit(i: Int): Int = x ^ left(i)
    def testBit(i: Int): Boolean = ((x >> i) & 1) == 1
    private def left(bits: Int, num: Int = 1) = assuming(bits >= 0 && bits < JInt.SIZE)(num << bits)
    def X(y: Int): Iterator[(Int, Int)] = until(x) X until(y)
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    import java.lang.{Long => JLong}
    def withHighestOneBitSet: Long = JLong.highestOneBit(x)
    def withLowestOneBitSet: Long = JLong.lowestOneBit(x)
    def numberOfLeadingZeroes: Int = JLong.numberOfLeadingZeros(x)
    def numberOfTrailingZeroes: Int = JLong.numberOfTrailingZeros(x)
    def bitCount: Int = JLong.bitCount(x)
    def setLowestBits(n: Int): Long = x | (left(n) - 1)
    def clearLowestBits(n: Int): Long = x & left(n, ~0L)
    def setBit(i: Int, set: Boolean): Long = if (set) setBit(i) else clearBit(i)
    def setBit(i: Int): Long = x | left(i)
    def clearBit(i: Int): Long = x & ~left(i)
    def toggleBit(i: Int): Long = x ^ left(i)
    def testBit(i: Int): Boolean = ((x >> i) & 1) == 1
    private def left(bits: Int, num: Long = 1L) = assuming(bits >= 0 && bits < JLong.SIZE)(num << bits)
  }
  implicit class NumericExtensions[A](val x: A)(implicit n: Numeric[A]) {
    import Numeric.Implicits._
    def **(i: Int): A = if (i == 0) n.one else {
      val h = x ** (i/2)
      if (i%2 == 0) h * h else h * h * x
    }
    def distance(y: A) = (x - y).abs()
    def nonNegative: Option[A] = when(n.gteq(x, n.zero))(x)
    def whenNegative(f: => A): A = nonNegative getOrElse f   //e.g. students indexOf "Greg" whenNegative Int.MaxValue
  }
  implicit class IntegralExtensions[A](val x: A)(implicit n: Integral[A]) {
    import scala.collection.immutable.NumericRange, Integral.Implicits._
    def mod(y: A): A = x - ((x / y) * y) //(x/y)*y + (x mod y) == x
    def -->(y: A): NumericRange.Inclusive[A] = NumericRange.inclusive(x, y, if (n.lteq(x, y)) n.one else -n.one)
    def ceilDiv(y: A): A = if (mod(y) == n.zero) x/y else n.one + (x/y)
  }
  implicit def toPoint[A](p: (A, A))(implicit n: Numeric[A]) = new java.awt.geom.Point2D.Double(n.toDouble(p._1), n.toDouble(p._2))
  def desc[A: Ordering]: Ordering[A] = asc[A].reverse  //e.g. students.sortBy(_.height)(desc)
  def asc[A](implicit order: Ordering[A]): Ordering[A] = order
  def map[K] = new {
    def to[V](default: => V): Dict[K, V] = using(_ => default)
    def using[V](f: K => V): Dict[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }
  def newMultiMap[K, V] = map[K] to mutable.Set.empty[V]
  def memoize[A, B](f: A => B): A ==> B = map[A] using f
  implicit class FuzzyDouble(val x: Double) extends AnyVal {
    def  >~(y: Double) = x > y + eps
    def >=~(y: Double) = x >= y + eps
    def  ~<(y: Double) = y >=~ x
    def ~=<(y: Double) = y >~ x
    def  ~=(y: Double) = (x distance y) <= eps
  }
  def toPolygon(points: Traversable[(Int, Int)]): java.awt.Polygon = {
    val (xs, ys) = points.unzip
    new java.awt.Polygon(xs.toArray, ys.toArray, points.size)
  }
  def toShape(points: Traversable[(Double, Double)]): java.awt.geom.GeneralPath = {
    val shape = new java.awt.geom.GeneralPath()
    points.headOption foreach {case (x, y) => shape.moveTo(x, y)}
    points.tail foreach {case (x, y) => shape.lineTo(x, y)}
    shape.closePath()
    shape
  }
  def until(until: Int): Range = 0 until until
  def not[A](f: A => Boolean): A => Boolean = {x: A => !f(x)}
  def assuming[A](cond: => Boolean)(f: => A): A = {require(cond); f}
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) f.some else None
  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  type ==>[A, B] = collection.Map[A, B]
  val mod: Int = (1e9 + 7).toInt
  val eps: Double = 1e-9
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    when(tokenizers.nonEmpty)(tokenizers.head)
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: C of A): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }
  object Read {
    implicit def collection[C[_], A: Read](implicit b: C of A): Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                       : Read[String]       = new Read(_.next())
    implicit val char                                         : Read[Char]         = string.map(_.toTraversable.onlyElement)
    implicit val int                                          : Read[Int]          = string.map(_.toInt)
    implicit val long                                         : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                       : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                       : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                   : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                     : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]            : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]   : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                      : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}


You are given n rectangles, labeled 1 through n. The corners of rectangles have integer coordinates and their edges are parallel to the Ox and Oy axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle).Your task is to determine if there's a non-empty subset of the rectangles that forms a square. That is, determine if there exists a subset of the rectangles and some square for which every point that belongs to the interior or the border of that square belongs to the interior or the border of at least one of the rectangles in the subset, and every point that belongs to the interior or the border of at least one rectangle in the subset belongs to the interior or the border of that square.

First line contains a single integer n (1 ≤ n ≤ 105) — the number of rectangles. Each of the next n lines contains a description of a rectangle, with the i-th such line describing the rectangle labeled i. Each rectangle description consists of four integers: x1, y1, x2, y2 — coordinates of the bottom left and the top right corners (0 ≤ x1 < x2 ≤ 3000, 0 ≤ y1 < y2 ≤ 3000). No two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle).

If such a subset exists, print "YES" (without quotes) on the first line of the output file, followed by k, the number of rectangles in the subset. On the second line print k numbers — the labels of rectangles in the subset in any order. If more than one such subset exists, print any one. If no such subset exists, print "NO" (without quotes).

NoteThe first test case looks as follows:  Note that rectangles 6, 8, and 9 form a square as well, and would be an acceptable answer.The second test case looks as follows:

import scala.collection._

object D extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val dim = 3001

  val n = readString.toInt

  val a = Array.fill(dim, dim)(0)
  val area = Array.ofDim[Int](dim, dim)
  val tl = Array.fill(dim)(new mutable.BitSet(dim))
  val tr = Array.fill(dim)(new mutable.BitSet(dim))
  val blx = Array.fill(dim)(new mutable.BitSet(dim))
  val br = Array.fill(dim)(new mutable.BitSet(dim))
  val te = Array.fill(dim)(new mutable.BitSet(dim))
  val be = Array.fill(dim)(new mutable.BitSet(dim))
  val lex = Array.fill(dim)(new mutable.BitSet(dim))
  val rex = Array.fill(dim)(new mutable.BitSet(dim))

  for (i <- 0 until dim) {
    area(0)(i) = 0
    area(i)(0) = 0
  }

  var i = 1
  while (i <= n) {
    val Array(x1, y1, x2, y2) = readInts
    tl(y1)(x1) = true
    tr(y1)(x2 - 1) = true
    blx(x1)(y2 - 1) = true
    br(y2 - 1)(x2 - 1) = true
    var x = x1
    while (x < x2) {
      te(y1)(x) = true
      be(y2 - 1)(x) = true
      x += 1
    }
    var y = y1
    while (y < y2) {
      lex(x1)(y) = true
      rex(x2 - 1)(y) = true
      y += 1
    }
    y = y1
    while (y < y2) {
      var x = x1
      val a2 = a(y)
      while (x < x2) {
        a2(x) = i
        x += 1
      }
      y += 1
    }
    i += 1
  }

  y = 1
  while (y < dim) {
    var x = 1
    val a2 = a(y)
    var horSum = 0
    while (x < dim) {
      if (a2(x) > 0) horSum += 1
      area(y)(x) = area(y - 1)(x) + horSum
      x += 1
    }
    y += 1
  }

  def holes(x1: Int, y1: Int, x2: Int, y2: Int): Boolean = {
    x2 >= x1 && y2 >= y1 &&
    area(y2)(x2) - area(y1 - 1)(x2) - area(y2)(x1 - 1) + area(y1 - 1)(x1 - 1) != (x2 - x1 + 1) * (y2 - y1 + 1)
//    var y = y1
//    while (y <= y2) {
//      var x = x1
//      val a2 = a(y)
//      while (x <= x2) {
//        if (a2(x) == 0) return true
//        x += 1
//      }
//      y += 1
//    }
//    false
  }

  def rectIds(x1: Int, y1: Int, x2: Int, y2: Int) = {
    val ids = mutable.Set[Int]()
    var y = y1
    while (y <= y2) {
      var x = x1
      val a2 = a(y)
      while (x <= x2) {
        ids += a2(x)
        x += 1
      }
      y += 1
    }
    ids
  }

  var y = 0
  while (y < dim) {
    var x = 0
    val te2 = te(y)
    val tr2 = tr(y)
    val tl2 = tl(y)
    while (x < dim) {
      if (tl2(x)) {
        var nextX = 0
        var size = 0
        val blx2 = blx(x)
        val lex2 = lex(x)
        while (x + size < dim && y + size < dim && te2(x + size) && lex2(y + size)) {
          if (tr2(x + size) && blx2(y + size) && br(y + size)(x + size)) {
            var d = 1
            val rex2 = rex(x + size)
            val be2 = be(y + size)
            while (d < size && rex2(y + d) && be2(x + d)) d += 1
            if (d >= size) {
              if (!holes(x + 1, y + 1, x + size - 1, y + size - 1)) {
                val ids = rectIds(x, y, x + size, y + size)
                println("YES " + ids.size)
                println(ids.mkString(" "))
                exit
              } else size = dim - x - 2
            }
          }
          size += 1
          if (tl2(x + size) && nextX == 0) nextX = x + size
        }
        x = if (nextX > 0) nextX else x + size
      } else x += 1
    }
    y += 1
  }

  println("NO")
}

import scala.collection._

object D extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val dim = 3001

  val n = readString.toInt

  val a = Array.fill(dim, dim)(0)
  val area = Array.ofDim[Int](dim, dim)
  val tl = Array.fill(dim)(new mutable.BitSet(dim))
  val tr = Array.fill(dim)(new mutable.BitSet(dim))
  val blx = Array.fill(dim)(new mutable.BitSet(dim))
  val br = Array.fill(dim)(new mutable.BitSet(dim))
  val te = Array.fill(dim)(new mutable.BitSet(dim))
  val be = Array.fill(dim)(new mutable.BitSet(dim))
  val lex = Array.fill(dim)(new mutable.BitSet(dim))
  val rex = Array.fill(dim)(new mutable.BitSet(dim))

  for (i <- 0 until dim) {
    area(0)(i) = 0
    area(i)(0) = 0
  }

  var i = 1
  while (i <= n) {
    val Array(x1, y1, x2, y2) = readInts
    tl(y1)(x1) = true
    tr(y1)(x2 - 1) = true
    blx(x1)(y2 - 1) = true
    br(y2 - 1)(x2 - 1) = true
    var x = x1
    while (x < x2) {
      te(y1)(x) = true
      be(y2 - 1)(x) = true
      x += 1
    }
    var y = y1
    while (y < y2) {
      lex(x1)(y) = true
      rex(x2 - 1)(y) = true
      y += 1
    }
    y = y1
    while (y < y2) {
      var x = x1
      while (x < x2) {
        a(y)(x) = i
        x += 1
      }
      y += 1
    }
    i += 1
  }

  y = 1
  while (y < dim) {
    var x = 1
    var horSum = 0
    while (x < dim) {
      if (a(y)(x) > 0) horSum += 1
      area(y)(x) = area(y - 1)(x) + horSum
      x += 1
    }
    y += 1
  }

  def full(x1: Int, y1: Int, x2: Int, y2: Int): Boolean = {
    x2 < x1 || y2 < y1 ||
    area(y2)(x2) - area(y1 - 1)(x2) - area(y2)(x1 - 1) + area(y1 - 1)(x1 - 1) == (x2 - x1 + 1) * (y2 - y1 + 1)
  }

  def rectIds(x1: Int, y1: Int, x2: Int, y2: Int) = {
    val ids = mutable.Set[Int]()
    var y = y1
    while (y <= y2) {
      var x = x1
      while (x <= x2) {
        ids += a(y)(x)
        x += 1
      }
      y += 1
    }
    ids
  }

  var y = 0
  while (y < dim) {
    var x = 0
    while (x < dim) {
      if (tl(y)(x)) {
        var nextX = 0
        var size = 0
        while (x + size < dim && y + size < dim && te(y)(x + size) && lex(x)(y + size)) {
          if (tr(y)(x + size) && blx(x)(y + size) && br(y + size)(x + size)) {
            var d = 1
            while (d < size && rex(x + size)(y + d) && be(y + size)(x + d)) d += 1
            if (d >= size) {
              if (full(x + 1, y + 1, x + size - 1, y + size - 1)) {
                val ids = rectIds(x, y, x + size, y + size)
                println("YES " + ids.size)
                println(ids.mkString(" "))
                exit
              } else size = dim - x - 2
            }
          }
          size += 1
          if (tl(y)(x + size) && nextX == 0) nextX = x + size
        }
        x = if (nextX > 0) nextX else x + size
      } else x += 1
    }
    y += 1
  }

  println("NO")
}

import scala.collection._

object D extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val dim = 3001

  val n = readString.toInt

  val a = Array.fill(dim, dim)(0)
  val area = Array.ofDim[Int](dim, dim)
  val tl = Array.fill(dim)(new mutable.BitSet(dim))
  val tr = Array.fill(dim)(new mutable.BitSet(dim))
  val blx = Array.fill(dim)(new mutable.BitSet(dim))
  val br = Array.fill(dim)(new mutable.BitSet(dim))
  val te = Array.fill(dim)(new mutable.BitSet(dim))
  val be = Array.fill(dim)(new mutable.BitSet(dim))
  val lex = Array.fill(dim)(new mutable.BitSet(dim))
  val rex = Array.fill(dim)(new mutable.BitSet(dim))

  for (i <- 0 until dim) {
    area(0)(i) = 0
    area(i)(0) = 0
  }

  var i = 1
  while (i <= n) {
    val Array(x1, y1, x2, y2) = readInts
    tl(y1)(x1) = true
    tr(y1)(x2 - 1) = true
    blx(y2 - 1)(x1) = true
    br(y2 - 1)(x2 - 1) = true
    var x = x1
    while (x < x2) {
      te(y1)(x) = true
      be(y2 - 1)(x) = true
      x += 1
    }
    var y = y1
    while (y < y2) {
      lex(y)(x1) = true
      rex(y)(x2 - 1) = true
      y += 1
    }
    y = y1
    while (y < y2) {
      var x = x1
      while (x < x2) {
        a(y)(x) = i
        x += 1
      }
      y += 1
    }
    i += 1
  }

  y = 1
  while (y < dim) {
    var x = 1
    var horSum = 0
    while (x < dim) {
      if (a(y)(x) > 0) horSum += 1
      area(y)(x) = area(y - 1)(x) + horSum
      x += 1
    }
    y += 1
  }

  def full(x1: Int, y1: Int, x2: Int, y2: Int): Boolean = {
    x2 < x1 || y2 < y1 ||
    area(y2)(x2) - area(y1 - 1)(x2) - area(y2)(x1 - 1) + area(y1 - 1)(x1 - 1) == (x2 - x1 + 1) * (y2 - y1 + 1)
  }

  def rectIds(x1: Int, y1: Int, x2: Int, y2: Int) = {
    val ids = mutable.Set[Int]()
    var y = y1
    while (y <= y2) {
      var x = x1
      while (x <= x2) {
        ids += a(y)(x)
        x += 1
      }
      y += 1
    }
    ids
  }

  var y = 0
  while (y < dim) {
    var x = 0
    while (x < dim) {
      if (tl(y)(x)) {
        var nextX = 0
        var size = 0
        while (x + size < dim && y + size < dim && te(y)(x + size) && lex(y + size)(x)) {
          if (tr(y)(x + size) && blx(y + size)(x) && br(y + size)(x + size)) {
            var d = 1
            while (d < size && rex(y + d)(x + size) && be(y + size)(x + d)) d += 1
            if (d >= size) {
              if (full(x + 1, y + 1, x + size - 1, y + size - 1)) {
                val ids = rectIds(x, y, x + size, y + size)
                println("YES " + ids.size)
                println(ids.mkString(" "))
                exit
              } else size = dim - x - 2
            }
          }
          size += 1
          if (tl(y)(x + size) && nextX == 0) nextX = x + size
        }
        x = if (nextX > 0) nextX else x + size
      } else x += 1
    }
    y += 1
  }

  println("NO")
}

import scala.collection._

object D extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val dim = 3001

  val n = readString.toInt

  val a = Array.fill(dim, dim)(0)
  val area = Array.ofDim[Int](dim, dim)
  val tl = Array.fill(dim)(new mutable.BitSet(dim))
  val tr = Array.fill(dim)(new mutable.BitSet(dim))
  val blx = Array.fill(dim)(new mutable.BitSet(dim))
  val br = Array.fill(dim)(new mutable.BitSet(dim))
  val te = Array.fill(dim)(new mutable.BitSet(dim))
  val be = Array.fill(dim)(new mutable.BitSet(dim))
  val lex = Array.fill(dim)(new mutable.BitSet(dim))
  val rex = Array.fill(dim)(new mutable.BitSet(dim))

  for (i <- 0 until dim) {
    area(0)(i) = 0
    area(i)(0) = 0
  }

  var i = 1
  while (i <= n) {
    val Array(x1, y1, x2, y2) = readInts
    tl(y1)(x1) = true
    tr(y1)(x2 - 1) = true
    blx(x1)(y2 - 1) = true
    br(y2 - 1)(x2 - 1) = true
    var x = x1
    while (x < x2) {
      te(y1)(x) = true
      be(y2 - 1)(x) = true
      x += 1
    }
    var y = y1
    while (y < y2) {
      lex(x1)(y) = true
      rex(x2 - 1)(y) = true
      y += 1
    }
    y = y1
    while (y < y2) {
      var x = x1
      val a2 = a(y)
      while (x < x2) {
        a2(x) = i
        x += 1
      }
      y += 1
    }
    i += 1
  }

  y = 1
  while (y < dim) {
    var x = 1
    val a2 = a(y)
    var horSum = 0
    while (x < dim) {
      if (a2(x) > 0) horSum += 1
      area(y)(x) = area(y - 1)(x) + horSum
      x += 1
    }
    y += 1
  }

  def full(x1: Int, y1: Int, x2: Int, y2: Int): Boolean = {
    x2 < x1 || y2 < y1 ||
    area(y2)(x2) - area(y1 - 1)(x2) - area(y2)(x1 - 1) + area(y1 - 1)(x1 - 1) == (x2 - x1 + 1) * (y2 - y1 + 1)
  }

  def rectIds(x1: Int, y1: Int, x2: Int, y2: Int) = {
    val ids = mutable.Set[Int]()
    var y = y1
    while (y <= y2) {
      var x = x1
      val a2 = a(y)
      while (x <= x2) {
        ids += a2(x)
        x += 1
      }
      y += 1
    }
    ids
  }

  var y = 0
  while (y < dim) {
    var x = 0
    val te2 = te(y)
    val tr2 = tr(y)
    val tl2 = tl(y)
    while (x < dim) {
      if (tl2(x)) {
        var nextX = 0
        var size = 0
        val blx2 = blx(x)
        val lex2 = lex(x)
        while (x + size < dim && y + size < dim && te2(x + size) && lex2(y + size)) {
          if (tr2(x + size) && blx2(y + size) && br(y + size)(x + size)) {
            var d = 1
            val rex2 = rex(x + size)
            val be2 = be(y + size)
            while (d < size && rex2(y + d) && be2(x + d)) d += 1
            if (d >= size) {
              if (full(x + 1, y + 1, x + size - 1, y + size - 1)) {
                val ids = rectIds(x, y, x + size, y + size)
                println("YES " + ids.size)
                println(ids.mkString(" "))
                exit
              } else size = dim - x - 2
            }
          }
          size += 1
          if (tl2(x + size) && nextX == 0) nextX = x + size
        }
        x = if (nextX > 0) nextX else x + size
      } else x += 1
    }
    y += 1
  }

  println("NO")
}

object D extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val dim = 3001

  val n = readString.toInt

  val a = Array.fill(dim, dim)(0)
  val tl = Array.fill(dim, dim)(false)
  val tr = Array.fill(dim, dim)(false)
  val bl = Array.fill(dim, dim)(false)
  val br = Array.fill(dim, dim)(false)
  val he = Array.fill(dim, dim)(false)
  val ve = Array.fill(dim, dim)(false)

  var i = 1
  while (i <= n) {
    val Array(x1, y1, x2, y2) = readInts
    tl(y1)(x1) = true
    tr(y1)(x2 - 1) = true
    bl(y2 - 1)(x1) = true
    br(y2 - 1)(x2 - 1) = true
    var x = x1
    while (x < x2) {
      he(y1)(x) = true
      he(y2 - 1)(x) = true
      x += 1
    }
    var y = y1
    while (y < y2) {
      ve(y)(x1) = true
      ve(y)(x2 - 1) = true
      y += 1
    }
    y = y1
    while (y <= y2) {
      var x = x1
      while (x <= x2) {
        a(y)(x) = i
        x += 1
      }
      y += 1
    }
    i += 1
  }

  def holes(x1: Int, y1: Int, x2: Int, y2: Int): Boolean = {
    var y = y1
    while (y <= y2) {
      var x = x1
      while (x <= x2) {
        if (a(y)(x) == 0) return true
        x += 1
      }
      y += 1
    }
    false
  }

  def rectIds(x1: Int, y1: Int, x2: Int, y2: Int) = {
    val ids = scala.collection.mutable.Set[Int]()
    var y = y1
    while (y <= y2) {
      var x = x1
      while (x <= x2) {
        ids += a(y)(x)
        x += 1
      }
      y += 1
    }
    ids
  }

  var y = 0
  while (y < dim) {
    var x = 0
    while (x < dim) {
      if (tl(y)(x)) {
        var dx = 0
        while (x + dx < dim && y + dx < dim && he(y)(x + dx)) {
          if (tr(y)(x + dx) && bl(y + dx)(x) && br(y + dx)(x + dx)) {
            var dy = 1
            var goodEdges = true
            while (dy < dx && goodEdges) {
              goodEdges = ve(y + dy)(x) && ve(y + dy)(x + dx) && he(y + dx)(x + dy)
              dy += 1
            }
            if (goodEdges && !holes(x + 1, y + 1, x + dx - 1, y + dx - 1)) {
              val ids = rectIds(x, y, x + dx, y + dx)
              println("YES " + ids.size)
              println(ids.mkString(" "))
              exit
            }
          }
          dx += 1
        }
      }
      x += 1
    }
    y += 1
  }

  println("NO")
}

import scala.collection._

object D extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val dim = 3001

  val n = readString.toInt

  val a = Array.fill(dim, dim)(0)
  val area = Array.ofDim[Int](dim, dim)
  val tl = Array.fill(dim)(new mutable.BitSet(dim))
  val tr = Array.fill(dim)(new mutable.BitSet(dim))
  val blx = Array.fill(dim)(new mutable.BitSet(dim))
  val br = Array.fill(dim)(new mutable.BitSet(dim))
  val te = Array.fill(dim)(new mutable.BitSet(dim))
  val be = Array.fill(dim)(new mutable.BitSet(dim))
  val lex = Array.fill(dim)(new mutable.BitSet(dim))
  val rex = Array.fill(dim)(new mutable.BitSet(dim))

  for (i <- 0 until dim) {
    area(0)(i) = 0
    area(i)(0) = 0
  }

  var i = 1
  while (i <= n) {
    val Array(x1, y1, x2, y2) = readInts
    tl(y1)(x1) = true
    tr(y1)(x2 - 1) = true
    blx(x1)(y2 - 1) = true
    br(y2 - 1)(x2 - 1) = true
    var x = x1
    while (x < x2) {
      te(y1)(x) = true
      be(y2 - 1)(x) = true
      x += 1
    }
    var y = y1
    while (y < y2) {
      lex(x1)(y) = true
      rex(x2 - 1)(y) = true
      y += 1
    }
    y = y1
    while (y < y2) {
      var x = x1
      val a2 = a(y)
      while (x < x2) {
        a2(x) = i
        x += 1
      }
      y += 1
    }
    i += 1
  }

  y = 1
  while (y < dim) {
    var x = 1
    val a2 = a(y)
    var horSum = 0
    while (x < dim) {
      if (a2(x) > 0) horSum += 1
      area(y)(x) = area(y - 1)(x) + horSum
      x += 1
    }
    y += 1
  }

  def holes(x1: Int, y1: Int, x2: Int, y2: Int): Boolean = {
    area(y2)(x2) - area(math.min(y1 - 1, 0))(x2) - area(y2)(math.min(x1 - 1, 0)) +
    	area(math.min(y1 - 1, 0))(math.min(x1 - 1, 0)) != (x2 - x1 + 1) * (y2 - y1 + 1)
//    var y = y1
//    while (y <= y2) {
//      var x = x1
//      val a2 = a(y)
//      while (x <= x2) {
//        if (a2(x) == 0) return true
//        x += 1
//      }
//      y += 1
//    }
//    false
  }

  def rectIds(x1: Int, y1: Int, x2: Int, y2: Int) = {
    val ids = mutable.Set[Int]()
    var y = y1
    while (y <= y2) {
      var x = x1
      val a2 = a(y)
      while (x <= x2) {
        ids += a2(x)
        x += 1
      }
      y += 1
    }
    ids
  }

  var y = 0
  while (y < dim) {
    var x = 0
    val te2 = te(y)
    val tr2 = tr(y)
    val tl2 = tl(y)
    while (x < dim) {
      if (tl2(x)) {
        var nextX = 0
        var size = 0
        val blx2 = blx(x)
        val lex2 = lex(x)
        while (x + size < dim && y + size < dim && te2(x + size) && lex2(y + size)) {
          if (tr2(x + size) && blx2(y + size) && br(y + size)(x + size)) {
            var d = 1
            val rex2 = rex(x + size)
            val be2 = be(y + size)
            while (d < size && rex2(y + d) && be2(x + d)) d += 1
            if (d >= size) {
              if (!holes(x + 1, y + 1, x + size - 1, y + size - 1)) {
                val ids = rectIds(x, y, x + size, y + size)
                println("YES " + ids.size)
                println(ids.mkString(" "))
                exit
              } else size = dim - x - 2
            }
          }
          size += 1
          if (tl2(x + size) && nextX == 0) nextX = x + size
        }
        x = if (nextX > 0) nextX else x + size
      } else x += 1
    }
    y += 1
  }

  println("NO")
}

import scala.collection._

object D extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val dim = 3001

  val n = readString.toInt

  val a = Array.fill(dim, dim)(0)
  val area = Array.ofDim[Int](dim, dim)
  val tl = Array.fill(dim)(new mutable.BitSet(dim))
  val tr = Array.fill(dim)(new mutable.BitSet(dim))
  val blx = Array.fill(dim)(new mutable.BitSet(dim))
  val br = Array.fill(dim)(new mutable.BitSet(dim))
  val te = Array.fill(dim)(new mutable.BitSet(dim))
  val be = Array.fill(dim)(new mutable.BitSet(dim))
  val lex = Array.fill(dim)(new mutable.BitSet(dim))
  val rex = Array.fill(dim)(new mutable.BitSet(dim))

  for (i <- 0 until dim) {
    area(0)(i) = 0
    area(i)(0) = 0
  }

  var i = 1
  while (i <= n) {
    val Array(x1, y1, x2, y2) = readInts
    tl(y1)(x1) = true
    tr(y1)(x2 - 1) = true
    blx(x1)(y2 - 1) = true
    br(y2 - 1)(x2 - 1) = true
    var x = x1
    while (x < x2) {
      te(y1)(x) = true
      be(y2 - 1)(x) = true
      x += 1
    }
    var y = y1
    while (y < y2) {
      lex(x1)(y) = true
      rex(x2 - 1)(y) = true
      y += 1
    }
    y = y1
    while (y < y2) {
      var x = x1
      val a2 = a(y)
      while (x < x2) {
        a2(x) = i
        x += 1
      }
      y += 1
    }
    i += 1
  }

  y = 1
  while (y < dim) {
    var x = 1
    val a2 = a(y)
    var horSum = 0
    while (x < dim) {
      if (a2(x) > 0) horSum += 1
      area(y)(x) = area(y - 1)(x) + horSum
      x += 1
    }
    y += 1
  }

  def holes(x1: Int, y1: Int, x2: Int, y2: Int): Boolean = {
    x2 < x1 || y2 < y1 ||
    area(y2)(x2) - area(y1 - 1)(x2) - area(y2)(x1 - 1) + area(y1 - 1)(x1 - 1) != (x2 - x1 + 1) * (y2 - y1 + 1)
//    var y = y1
//    while (y <= y2) {
//      var x = x1
//      val a2 = a(y)
//      while (x <= x2) {
//        if (a2(x) == 0) return true
//        x += 1
//      }
//      y += 1
//    }
//    false
  }

  def rectIds(x1: Int, y1: Int, x2: Int, y2: Int) = {
    val ids = mutable.Set[Int]()
    var y = y1
    while (y <= y2) {
      var x = x1
      val a2 = a(y)
      while (x <= x2) {
        ids += a2(x)
        x += 1
      }
      y += 1
    }
    ids
  }

  var y = 0
  while (y < dim) {
    var x = 0
    val te2 = te(y)
    val tr2 = tr(y)
    val tl2 = tl(y)
    while (x < dim) {
      if (tl2(x)) {
        var nextX = 0
        var size = 0
        val blx2 = blx(x)
        val lex2 = lex(x)
        while (x + size < dim && y + size < dim && te2(x + size) && lex2(y + size)) {
          if (tr2(x + size) && blx2(y + size) && br(y + size)(x + size)) {
            var d = 1
            val rex2 = rex(x + size)
            val be2 = be(y + size)
            while (d < size && rex2(y + d) && be2(x + d)) d += 1
            if (d >= size) {
              if (!holes(x + 1, y + 1, x + size - 1, y + size - 1)) {
                val ids = rectIds(x, y, x + size, y + size)
                println("YES " + ids.size)
                println(ids.mkString(" "))
                exit
              } else size = dim - x - 2
            }
          }
          size += 1
          if (tl2(x + size) && nextX == 0) nextX = x + size
        }
        x = if (nextX > 0) nextX else x + size
      } else x += 1
    }
    y += 1
  }

  println("NO")
}

object D extends App {

  def readString = Console.readLine
  def readInts = readString.split(" ").map(_.toInt)

  val dim = 3001

  val n = readString.toInt

  val a = Array.fill(dim, dim)(0)
  val tl = Array.fill(dim, dim)(false)
  val tr = Array.fill(dim, dim)(false)
  val bl = Array.fill(dim, dim)(false)
  val br = Array.fill(dim, dim)(false)
  val he = Array.fill(dim, dim)(false)
  val ve = Array.fill(dim, dim)(false)

  for (i <- 1 to n) {
    val Array(x1, y1, x2, y2) = readInts
    tl(y1)(x1) = true
    tr(y1)(x2 - 1) = true
    bl(y2 - 1)(x1) = true
    br(y2 - 1)(x2 - 1) = true
    for (x <- x1 until x2) {
      he(y1)(x) = true
      he(y2 - 1)(x) = true
    }
    for (y <- y1 until y2) {
      ve(y)(x1) = true
      ve(y)(x2 - 1) = true
    }
    for (y <- y1 until y2; x <- x1 until x2) a(y)(x) = i
  }

  def holes(x1: Int, y1: Int, x2: Int, y2: Int): Boolean = {
    var y = y1
    while (y <= y2) {
      var x = x1
      while (x <= x2) {
        if (a(y)(x) == 0) return true
        x += 1
      }
      y += 1
    }
    false
  }

  def rectIds(x1: Int, y1: Int, x2: Int, y2: Int) = {
    val ids = scala.collection.mutable.Set[Int]()
    var y = y1
    while (y <= y2) {
      var x = x1
      while (x <= x2) {
        ids += a(y)(x)
        x += 1
      }
      y += 1
    }
    ids
  }

  for (y <- 0 until dim) {
    var x = 0
    while (x < dim) {
      if (tl(y)(x)) {
        var dx = 0
        while (x + dx < dim && y + dx < dim && he(y)(x + dx)) {
          if (tr(y)(x + dx) && bl(y + dx)(x) && br(y + dx)(x + dx)) {
            var dy = 1
            var goodEdges = true
            while (dy < dx && goodEdges) {
              goodEdges = ve(y + dy)(x) && ve(y + dy)(x + dx) && he(y + dx)(x + dy)
              dy += 1
            }
            if (goodEdges && !holes(x + 1, y + 1, x + dx - 1, y + dx - 1)) {
              val ids = rectIds(x, y, x + dx, y + dx)
              println(ids.size)
              println(ids.mkString(" "))
              exit
            }
          }
          dx += 1
        }
      }
      x += 1
    }
  }

  println("NO")
}


Mihai has an $$$8 \times 8$$$ chessboard whose rows are numbered from $$$1$$$ to $$$8$$$ from top to bottom and whose columns are numbered from $$$1$$$ to $$$8$$$ from left to right.Mihai has placed exactly one bishop on the chessboard. The bishop is not placed on the edges of the board. (In other words, the row and column of the bishop are between $$$2$$$ and $$$7$$$, inclusive.)The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked.     An example of a bishop on a chessboard. The squares it attacks are marked in red. Mihai has marked all squares the bishop attacks, but forgot where the bishop was! Help Mihai find the position of the bishop.

The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 36$$$) — the number of test cases. The description of test cases follows. There is an empty line before each test case. Each test case consists of $$$8$$$ lines, each containing $$$8$$$ characters. Each of these characters is either '#' or '.', denoting a square under attack and a square not under attack, respectively.

For each test case, output two integers $$$r$$$ and $$$c$$$ ($$$2 \leq r, c \leq 7$$$) — the row and column of the bishop.  The input is generated in such a way that there is always exactly one possible location of the bishop that is not on the edge of the board.

NoteThe first test case is pictured in the statement. Since the bishop lies in the intersection row $$$4$$$ and column $$$3$$$, the correct output is 4 3.

import scala.io.StdIn
import scala.math.BigDecimal.double2bigDecimal

object Main {
  def main(args: Array[String]): Unit = {
    val t = StdIn.readInt()

    for (_ <- 0 until t) {
      StdIn.readLine()
      val chessboard = for (_ <- 0 until 8) yield {
        StdIn.readLine().split("").map(x =>
          if (x == "#") {
            true
          } else {
            false
          }
        )
      }

      for(i <- 0 until 8) {
        for(j <- 0 until 8) {
          if(lookup(chessboard, i, j) && lookup(chessboard, i - 1, j - 1) && lookup(chessboard, i - 1, j + 1) && lookup(chessboard, i + 1, j + 1) && lookup(chessboard, i + 1, j - 1)) {
            println(s"${i + 1} ${j + 1}")
          }
        }
      }

    }
  }

  def lookup(seq: Seq[Array[Boolean]], i: Int, j: Int): Boolean = {
    if(i < 8 && i >= 0 && j < 8 && j >= 0) {
      seq(i)(j)
    } else {
      false
    }
  }
}


You have $$$n$$$ barrels lined up in a row, numbered from left to right from one. Initially, the $$$i$$$-th barrel contains $$$a_i$$$ liters of water.You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels $$$x$$$ and $$$y$$$ (the $$$x$$$-th barrel shouldn't be empty) and pour any possible amount of water from barrel $$$x$$$ to barrel $$$y$$$ (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them. Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $$$k$$$ times.Some examples:   if you have four barrels, each containing $$$5$$$ liters of water, and $$$k = 1$$$, you may pour $$$5$$$ liters from the second barrel into the fourth, so the amounts of water in the barrels are $$$[5, 0, 5, 10]$$$, and the difference between the maximum and the minimum is $$$10$$$;  if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still $$$0$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k < n \le 2 \cdot 10^5$$$) — the number of barrels and the number of pourings you can make. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{9}$$$), where $$$a_i$$$ is the initial amount of water the $$$i$$$-th barrel has. It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most $$$k$$$ times.

import scala.io.StdIn.readLine

object task extends App {
  val t: Int = readLine.toInt
  for (i <- 1 to t) {
    val ln1 = readLine.split(" ").map(_.toInt)
    val b: Int = ln1(0)
    val r: Int = ln1(1)
    val barrels = readLine.split(" ").map(_.toLong).sorted
    val sum: Long = barrels.takeRight(r + 1).sum
    println(sum)
  }
}

object B {
  import InOut._
  def main(args: Array[String]): Unit = {

    for (_ <- 1 to nextInt) {
      val n, k = nextInt
      val as = nextLongs(n).sorted.reverse
      val res = as.take(k + 1).sum

      out.println(res)

      out.flush()
    }
  }

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

object B extends App {
  import scala.io.StdIn._

  val t = readInt()

  (0 until t).foreach { _ =>
    val Array(n, k) = readLine().split(" ").map(_.toInt)
    val an          = readLine().split(" ").map(_.toInt).sorted(Ordering.Int.reverse)

    val ans = an.take(k + 1).foldLeft(0L)(_ + _)

    println(ans)
  }
}

import scala.io.StdIn.readLine

object task extends App {
  val t: Int = readLine.toInt
  for (i <- 1 to t) {
    val ln1 = readLine.split(" ").map(_.toInt)
    val b: Int = ln1(0)
    val r: Int = ln1(1)
    val barrels = readLine.split(" ").map(_.toInt).sorted
    val sum: Int = barrels.takeRight(r + 1).sum
    println(sum)
  }
}

import scala.io.StdIn.readLine

object task extends App {
  val t: Int = readLine.toInt
  for (i <- 1 to t) {
    val ln1 = readLine.split(" ").map(_.toInt)
    val b: Int = ln1(0)
    val r: Int = ln1(1)
    val barrels = readLine.split(" ").map(_.toInt).sorted
    val sum: Int = barrels.take(r + 1).sum
    println(sum)
  }
}