You are given a grid with $$$n$$$ rows and $$$m$$$ columns. We denote the square on the $$$i$$$-th ($$$1\le i\le n$$$) row and $$$j$$$-th ($$$1\le j\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$. All numbers are equal to $$$1$$$ or to $$$-1$$$. You start from the square $$$(1, 1)$$$ and can move one square down or one square to the right at a time. In the end, you want to end up at the square $$$(n, m)$$$.Is it possible to move in such a way so that the sum of the values written in all the visited cells (including $$$a_{11}$$$ and $$$a_{nm}$$$) is $$$0$$$?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$)  — the size of the grid. Each of the following $$$n$$$ lines contains $$$m$$$ integers. The $$$j$$$-th integer on the $$$i$$$-th line is $$$a_{ij}$$$ ($$$a_{ij} = 1$$$ or $$$-1$$$)  — the element in the cell $$$(i, j)$$$. It is guaranteed that the sum of $$$n\cdot m$$$ over all test cases does not exceed $$$10^6$$$.

For each test case, print "YES" if there exists a path from the top left to the bottom right that adds up to $$$0$$$, and "NO" otherwise. You can output each letter in any case.

NoteOne possible path for the fourth test case is given in the picture in the statement.

object prob2 {
  def main(args: Array[String]) = {
    new Resolver3(Console.in, Console.out).run()
  }
}

class Resolver3(input: java.io.BufferedReader, output: java.io.PrintStream) {

  import java.util.StringTokenizer
  import scala.collection.mutable
  import scala.collection.mutable._
  import scala.math.{abs,pow,min,max}
  import scala.collection.mutable.ListBuffer

  def run() {
    var tokenizer = new StringTokenizer(input.readLine())
    val q = tokenizer.nextToken().toInt
    for (sss <- 0 until q) {
      tokenizer = new StringTokenizer(input.readLine())
      val n = tokenizer.nextToken.toInt
      val m = tokenizer.nextToken.toInt
      val A =  new Array[Array[Int]](n)
      val B =  new Array[Array[Int]](n)
      for (i <- 0 until n) {
        A(i) = new Array[Int](m)
        B(i) = new Array[Int](m)
      }
      for (i <- 0 until n) {
        tokenizer = new StringTokenizer(input.readLine())
        for (j <- 0 until m) {
          val a = tokenizer.nextToken().toInt
          if (j == 0) {
            if (i == 0) {
              A(i)(j) = a
              B(i)(j) = a
            }
            else {
              A(i)(j) = a + A(i-1)(j)
              B(i)(j) = a + B(i-1)(j)
            }
          }
          else if (i == 0) {
            A(i)(j) =  a + A(i)(j-1)
            B(i)(j) =  a + B(i)(j-1)
          }
          else {
            A(i)(j) = a + min((A(i))(j-1),A(i-1)(j))
            B(i)(j) = a + max((B(i))(j-1),B(i-1)(j))
          }
        }
      }
      if ((A.last.last * B.last.last > 0) || ((n+m)% 2 == 0)) {
        output.println("no")
      }
      else {
        output.println("yes")}
    }
  }
}

object prob2 {
  def main(args: Array[String]) = {
    new Resolver3(Console.in, Console.out).run()
  }
}

class Resolver3(input: java.io.BufferedReader, output: java.io.PrintStream) {

  import java.util.StringTokenizer
  import scala.collection.mutable
  import scala.collection.mutable._
  import scala.math.{abs,pow,min,max}
  import scala.collection.mutable.ListBuffer

  def run() {
    var tokenizer = new StringTokenizer(input.readLine())
    val q = tokenizer.nextToken().toInt
    for (sss <- 0 until q) {
      tokenizer = new StringTokenizer(input.readLine())
      val n = tokenizer.nextToken.toInt
      val m = tokenizer.nextToken.toInt
      val A =  new Array[Array[Int]](n)
      val B =  new Array[Array[Int]](n)
      for (i <- 0 until n) {
        A(i) = new Array[Int](m)
        B(i) = new Array[Int](m)
      }
      for (i <- 0 until n) {
        tokenizer = new StringTokenizer(input.readLine())
        for (j <- 0 until m) {
          val a = tokenizer.nextToken().toInt
          if (j == 0) {
            if (i == 0) {
              A(i)(j) = a
              B(i)(j) = a
            }
            else {
              A(i)(j) = a + A(i-1)(j)
              B(i)(j) = a + B(i-1)(j)
            }
          }
          else if (i == 0) {
            A(i)(j) =  a + A(i)(j-1)
            B(i)(j) =  a + B(i)(j-1)
          }
          else {
            A(i)(j) = a + min((A(i))(j-1),A(i-1)(j))
            B(i)(j) = a + max((B(i))(j-1),B(i-1)(j))
          }
        }
      }
      if ((A.last.last * B.last.last > 0) || ((n+m)% 2 == 0)) {
        output.println("no")
      }
      else {
        output.println("yes")}
      println(A.toList.map(_.toList),B.toList.map(_.toList))
    }
  }
}

object prob2 {
  def main(args: Array[String]) = {
    new Resolver3(Console.in, Console.out).run()
  }
}

class Resolver3(input: java.io.BufferedReader, output: java.io.PrintStream) {

  import java.util.StringTokenizer
  import scala.collection.mutable
  import scala.collection.mutable._
  import scala.math.{abs,pow,min,max}
  import scala.collection.mutable.ListBuffer

  def run() {
    var tokenizer = new StringTokenizer(input.readLine())
    val q = tokenizer.nextToken().toInt
    for (sss <- 0 until q) {
      tokenizer = new StringTokenizer(input.readLine())
      val n = tokenizer.nextToken.toInt
      val m = tokenizer.nextToken.toInt
      val A =  new Array[Array[Int]](n)
      val B =  new Array[Array[Int]](n)
      for (i <- 0 until n) {
        A(i) = new Array[Int](m)
        B(i) = new Array[Int](m)
      }
      for (i <- 0 until n) {
        tokenizer = new StringTokenizer(input.readLine())
        for (j <- 0 until m) {
          val a = tokenizer.nextToken().toInt
          if (j == 0) {
            if (i == 0) {
              A(i)(j) = a
              B(i)(j) = a
            }
            else {
              A(i)(j) = a + A(i-1)(j)
              B(i)(j) = a + B(i-1)(j)
            }
          }
          else if (i == 0) {
            A(i)(j) =  a + A(i)(j-1)
            B(i)(j) =  a + B(i)(j-1)
          }
          else {
            A(i)(j) = a + min((A(i))(j-1),A(i-1)(j))
            B(i)(j) = a + max((B(i))(j-1),B(i-1)(j))
          }
        }
      }
      if (A.last.last * B.last.last > 0) {
        output.println("no")
      }
      else {
        output.println("yes")}
    }
  }
}


Constanze is the smartest girl in her village but she has bad eyesight.One day, she was able to invent an incredible machine! When you pronounce letters, the machine will inscribe them onto a piece of paper. For example, if you pronounce 'c', 'o', 'd', and 'e' in that order, then the machine will inscribe "code" onto the paper. Thanks to this machine, she can finally write messages without using her glasses.However, her dumb friend Akko decided to play a prank on her. Akko tinkered with the machine so that if you pronounce 'w', it will inscribe "uu" instead of "w", and if you pronounce 'm', it will inscribe "nn" instead of "m"! Since Constanze had bad eyesight, she was not able to realize what Akko did.The rest of the letters behave the same as before: if you pronounce any letter besides 'w' and 'm', the machine will just inscribe it onto a piece of paper.The next day, I received a letter in my mailbox. I can't understand it so I think it's either just some gibberish from Akko, or Constanze made it using her machine. But since I know what Akko did, I can just list down all possible strings that Constanze's machine would have turned into the message I got and see if anything makes sense.But I need to know how much paper I will need, and that's why I'm asking you for help. Tell me the number of strings that Constanze's machine would've turned into the message I got.But since this number can be quite large, tell me instead its remainder when divided by $$$10^9+7$$$.If there are no strings that Constanze's machine would've turned into the message I got, then print $$$0$$$.

Input consists of a single line containing a string $$$s$$$ ($$$1 \leq |s| \leq 10^5$$$) — the received message. $$$s$$$ contains only lowercase Latin letters.

Print a single integer — the number of strings that Constanze's machine would've turned into the message $$$s$$$, modulo $$$10^9+7$$$.

NoteFor the first example, the candidate strings are the following: "ouuokarinn", "ouuokarim", "owokarim", and "owokarinn".For the second example, there is only one: "banana".For the third example, the candidate strings are the following: "nm", "mn" and "nnn".For the last example, there are no candidate strings that the machine can turn into "amanda", since the machine won't inscribe 'm'.

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null
  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  @inline def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debugDim(m: Array[Array[Int]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  @inline def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }
  private def isDebug[A](debug: => A, online: => A): A = {
    if (oj) online else debug
  }

  class InputReader(val stream: InputStream) {
    private[this] val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }
  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Workspace {
  import Main._
  case class Len(len: Int, c: Char)

// nCk % MOD を求める。下準備で階乗Fと≡MODでの階乗の逆元Iを作る
class Comb(N: Int, MOD: Int) {
  def powMod(x: Int, n: Int, m: Int): Int = {
    def step(x: Long, n: Int, stack: Long): Long = {
      n match {
        case 0 => stack
        case _ => step(x * x % m, n / 2, if (n % 2 == 1) stack * x % m else stack)
      }
    }
    step(x, n, 1).toInt
  }

  private[this] val F = Array.ofDim[Long](N + 1)
  F(0) = 1
  REP(N) { i =>
    F(i + 1) = F(i) * (i + 1) % MOD
  }
  private[this] val I = Array.ofDim[Long](F.length)
  I(N) = powMod(F(N).toInt, MOD - 2, MOD)

  // x! = x(x-1)!
  // x!I(x!) ≡ (x-1)!I((x-1)!)
  // I((x-1)!) ≡ I(x!) * x   MODがでかいので(x-1)!はMODと素
  REP_r(N) { i =>
    I(i) = I(i + 1) * (i + 1) % MOD
  }

  def comb(n: Int, k: Int): Long = {
    if (n < k) 0
    else F(n) * I(n - k) % MOD * I(k) % MOD
  }

  def rev(x: Int): Long = {
    I(x) * F(x - 1) % MOD
  }

  /**
    * nのグループからk回重複ありで選ぶ組み合わせ数
    * n - 1のしきりとkの○で考える
    */
  def H(n: Int, k: Int): Long = {
    comb(n + k - 1, k)
  }

  /**
    * private[this]をつかってるのでgetterが必要
    * @return (F, I)
    */
  def get: (Array[Long], Array[Long]) = (F, I)
}
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer
  import Workspace._

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve(): Unit = {
    val s = ns()
    val n = s.length
    if (s.contains('m') || s.contains('w')) {
      out.println(0)
      return
    }

    val L = ArrayBuffer[Len]()
    var cnt = 0
    REP(n) { i =>
      if (i > 0 && s(i) != s(i - 1)) {
        L += Len(cnt, s(i - 1))
      }
      if (i == 0 || s(i) != s(i - 1)) cnt = 1
      else cnt += 1
    }
    L += Len(cnt, s(n - 1))

    debug(L.mkString)

    var ans = 1L
    val comb = new Comb(n, MOD)
    def calc(x: Int): Long = {
      debug(s"calc($x)")
      var res = 0L
      var a = x
      var b = 0
      while (a >= 0) {
        res += comb.comb(a + b, a) % MOD
        a -= 2
        b += 1
      }
      res % MOD
    }

    for {
      l <- L
      if l.c == 'u' || l.c == 'n'
    } {
      ans = ans * calc(l.len) % MOD
    }
    out.println(ans)
  }
}

object _1245C extends CodeForcesApp {
  import Utils._, annotation._, collection.{mutable, Searching}, util._, control._, math.Ordering.Implicits._, Searching._, Math._

  val mod = (1e9 + 7).toInt

  val fib = {
    val n = 1e5.toInt
    val f = Array.ofDim[Int](n + 1)
    f.indices foreach {
      case i if i <= 1 => f(i) = i
      case i => f(i) = (f(i-1) + f(i-2)) % mod
    }
    f
  }

  override def apply(implicit io: IO): io.type = {
    val msg = io.read[String]
    val ans = splitRepeating(msg) map { token =>
      token(0) match {
        case 'u' | 'n' => fib(token.length + 1)
        case 'w' | 'm' => 0
        case _ => 1
      }
    }
    io.writeLine(ans.foldLeft(BigInt(1)) {case (i, j) => (i * j).mod(mod)})
  }

  def splitRepeating(s: String): Seq[String] = s.split("(?<=(.))(?!\\1)")

}
/****************************[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(implicit io: IO): io.type
  def repeat(f: => Unit)(implicit io: IO): io.type = {Utils.repeat(io.read[Int])(f); io}
  def main(args: Array[String]): Unit = apply(new IO(System.in, System.out)).close()
}
/****************************[Scala Collection Utils]****************************************/
object Utils {
  import scala.collection.mutable

  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]

  def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None

  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)

  implicit class GenericExtensions[A](a: A) {
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }

  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](t: C[A]) {
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]

    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)

    def toMultiSet: Map[A, Int] = t.groupBy(identity).mapValues(_.size)
  }

  implicit class PairExtensions[A, B](p: (A, B)) {
    def key = p._1
    def value = p._2
    def toSeq(implicit ev: B =:= A): Seq[A] = Seq(p.key, ev(p.value))
  }

  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C]).withDefaultValue(b().result())
  }

  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](m: C[K, V]) {
    def toMutable: mutable.Map[K, V] = mutable.Map.empty[K, V] ++ m
  }

  implicit class SetExtensions[A, C[A] <: Set[A]](s: C[A]) {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }

  implicit class IterableExtensions[A, C[A] <: Seq[A]](s: C[A]) {
    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)}
  }

  implicit class BooleanExtensions(x: Boolean) {
    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 {
    @inline def isEven = x%2 == 0
    @inline def isOdd = x%2 == 1
  }

  def desc[A](implicit order: Ordering[A]): Ordering[A] = order.reverse  //e.g. students.sortBy(_.height)(desc)

  def map[K] = new {
    def to[V](default: => V): mutable.Map[K, V] = using(_ => default)
    def using[V](f: K => V): mutable.Map[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
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

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()
    tokenizers.headOption
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: CanBuild[A, C[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 writeLines(obj: Traversable[Any]): this.type = writeAll(obj, separator = "\n")
  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: CanBuild[A, C[A]]) : Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                                   : Read[String]       = new Read(_.next())
    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
      }
    }
  }
}


Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.

The first line contains two integers n, m (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages. The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message. The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message. It is guaranteed that x1 + ... + xn = y1 + ... + ym. Also, it is guaranteed that x1 + ... + xn ≤ 106.

Print the maximum number of files the intercepted array could consist of.

NoteIn the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.In the third example the only possibility is that the archive contains a single file of size 4.

import scala.io.StdIn._
object B950 {
  def main(args: Array[String]): Unit = {
    val Array(n, m) = readLine().split(" ").map(_.toInt)
    val a = readLine().split(" ").map(_.toInt)
    val b = readLine().split(" ").map(_.toInt)
    var left = 0
    var right = 0
    var aSum = -1
    var bSum = -1
    var res = 0
    while (!(left >= n && right >= m)) {
      if (aSum != -1 && aSum == bSum) {
        res += 1
        aSum = -1
        bSum = -1
      } else if (aSum == -1) {
        aSum = if (left < n) a(left) else 0
        left += 1
        bSum = if (right < m) b(right) else 0
        right += 1
      } else if (aSum < bSum) {
        aSum += (if (left < n) a(left) else 0)
        left += 1
      } else {
        bSum += (if (right < m) b(right) else 0)
        right += 1
      }
    }
    if (aSum != -1 && aSum == bSum) res += 1
    println(res)
  }
}

object bTask2 {
  import scala.io.StdIn.{readLine, readInt}

  def message(n: Int, m: Int, xs: List[Int], ys: List[Int]):Int = {

    def helper(xx: List[Int], yy: List[Int], ax: Int, ay: Int, num: Int):Int = {
      (xx, yy) match {
        case (Nil,_)|(_,Nil) => num
        case (l@(x::xss), y::yss) if ax > ay => helper(l, yss, ax, ay + y, num)
        case (x::xss, r@(y::yss)) if ax < ay => helper(xss, r, ax + x, ay, num)
        case (x::xss, y::yss) if ax == ay => helper(xss, yss, x, y, num + 1)
      }
    }
    helper(xs, ys, 0,0,0)
  }

  def main(args: Array[String]):Unit = {
    val n::m::Nil = readLine.split(" ").map(_.toInt).toList
    val xs = readLine.split(" ").map(_.toInt).toList
    val ys = readLine.split(" ").map(_.toInt).toList
    println(message(n,m,xs,ys))
  }
}

object Solution {
  def main(args: Array[String]) {
    val sc = new java.util.Scanner(System.in)
    val n, m = sc.nextInt
    val xs = prefixSum(List.fill(n)(sc.nextInt))
    val ys = prefixSum(List.fill(m)(sc.nextInt))

    println(xs.foldLeft((ys, 0)) {
      case ((zs, res), x) =>
        zs.span(_ < x)._2 match {
          case (z :: zs) if z == x => (zs, res + 1)
          case zs => (zs, res)
        }
    }._2 - 1)
  }

  def prefixSum(as: List[Int]) = as.scanLeft(0){ case (acc, a) => a + acc }
}

import scala.io.StdIn

object Main {
  def main(args: Array[String]) {

    val (n, m) = StdIn.readLine.split(" ").map(_.toInt) match {
      case xy: Array[Int] => (xy(0), xy(1))
    }

    val x = StdIn.readLine.split(" ").map(_.toInt)
    val y = StdIn.readLine.split(" ").map(_.toInt)

    var z = 0
    val xc = x.map(a => {
      z += a
      z
    })

    z = 0
    val yc = y.map(a => {
      z += a
      z
    })

    var ans = 0

    var (i, j) = (0, 0)
    while (i < n && j < m) {
      val small = Math.min(xc(i), yc(j))

      if (xc(i) == yc(j)) ans += 1
      if (xc(i) == small) i += 1
      else j += 1
    }

    println(ans)
  }
}

object B extends App {
  val Array(n, m) = scala.io.StdIn.readLine().split(" ").map(_.toInt)
  val x = scala.io.StdIn.readLine().split(" ").map(_.toInt)
  val y = scala.io.StdIn.readLine().split(" ").map(_.toInt)

  var i = 0
  var j = 0
  var p = 0
  var q = 0
  var c = 1
  while (i < n && j < m) {
    if (p != 0 && q != 0 && p == q) {
      c += 1
      p = 0
      q = 0
    }
    if (p > q) {
      q += y(j)
      j += 1
    } else if (q > p) {
      p += x(i)
      i += 1
    } else {
      p += x(i)
      i += 1
      q += y(j)
      j += 1
    }
  }

  println(c)
}

object Solution {
  def main(args: Array[String]) {
    val sc = new java.util.Scanner(System.in)
    val n, m = sc.nextInt
    val xs = prefixSum(List.fill(n)(sc.nextInt))
    val ys = prefixSum(List.fill(m)(sc.nextInt))

    println(xs.foldLeft((ys, 0)) {
      case ((Nil, res), _) => (Nil, res)
      case ((z :: zs, res), x) => if (x == z) (zs, res + 1) else ((z :: zs).dropWhile(_ < x), res)
    }._2)
  }

  def prefixSum(as: List[Int]) = as.scanLeft(0){ case (acc, a) => a + acc }
}


There are $$$n$$$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from $$$1$$$ to $$$n$$$.It is known that, from the capital (the city with the number $$$1$$$), you can reach any other city by moving along the roads.The President of Berland plans to improve the country's road network. The budget is enough to repair exactly $$$n-1$$$ roads. The President plans to choose a set of $$$n-1$$$ roads such that:  it is possible to travel from the capital to any other city along the $$$n-1$$$ chosen roads,  if $$$d_i$$$ is the number of roads needed to travel from the capital to city $$$i$$$, moving only along the $$$n-1$$$ chosen roads, then $$$d_1 + d_2 + \dots + d_n$$$ is minimized (i.e. as minimal as possible). In other words, the set of $$$n-1$$$ roads should preserve the connectivity of the country, and the sum of distances from city $$$1$$$ to all cities should be minimized (where you can only use the $$$n-1$$$ chosen roads).The president instructed the ministry to prepare $$$k$$$ possible options to choose $$$n-1$$$ roads so that both conditions above are met.Write a program that will find $$$k$$$ possible ways to choose roads for repair. If there are fewer than $$$k$$$ ways, then the program should output all possible valid ways to choose roads.

The first line of the input contains integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 2\cdot10^5, n-1 \le m \le 2\cdot10^5, 1 \le k \le 2\cdot10^5$$$), where $$$n$$$ is the number of cities in the country, $$$m$$$ is the number of roads and $$$k$$$ is the number of options to choose a set of roads for repair. It is guaranteed that $$$m \cdot k \le 10^6$$$. The following $$$m$$$ lines describe the roads, one road per line. Each line contains two integers $$$a_i$$$, $$$b_i$$$ ($$$1 \le a_i, b_i \le n$$$, $$$a_i \ne b_i$$$) — the numbers of the cities that the $$$i$$$-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.

Print $$$t$$$ ($$$1 \le t \le k$$$) — the number of ways to choose a set of roads for repair. Recall that you need to find $$$k$$$ different options; if there are fewer than $$$k$$$ of them, then you need to find all possible different valid options. In the following $$$t$$$ lines, print the options, one per line. Print an option as a string of $$$m$$$ characters where the $$$j$$$-th character is equal to '1' if the $$$j$$$-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the $$$t$$$ lines should be different. Since it is guaranteed that $$$m \cdot k \le 10^6$$$, the total length of all the $$$t$$$ lines will not exceed $$$10^6$$$. If there are several answers, output any of them.

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  case class Edge(to: Int, pos: Int)
  type WUGraph = Array[Array[Edge]]

  def packWUGraph(n: Int, from: Array[Int], to: Array[Int]): WUGraph = {
    val g = new Array[Array[Edge]](n)
    val p = new Array[Int](n)
    val m = from.length
    REP(m)(i => p(from(i)) += 1)
    REP(m)(i => p(to(i)) += 1)
    REP(n)(i => g(i) = new Array(p(i)))
    REP(m) { i =>
      p(from(i)) -= 1
      g(from(i))(p(from(i))) = Edge(to(i), i)
      p(to(i)) -= 1
      g(to(i))(p(to(i))) = Edge(from(i), i)
    }
    g
  }

  def solve(): Unit = {
    val N, M, K = ni()
    val (from, to) = na2(M, -1)
    val g = packWUGraph(N, from, to)
    val edges = traceBfs(g)

    val roads = Array.ofDim[Int](M)
    val digits = Array.ofDim[Int](N)

    def mkRoads(): Unit = {
      import java.util
      util.Arrays.fill(roads, 0)
      REP(N - 1, 1) { i =>
        val e = edges(i)(digits(i))
        roads(e.pos) = 1
      }
    }

    def incrDigits(k: Int): Unit = {
      if (digits(k) == edges(k).length - 1) {
        digits(k) = 0
        incrDigits(k - 1)
      } else {
        digits(k) += 1
      }
    }

    val ans = ArrayBuffer[String]()
    var i = 0
    while(i < K && digits(0) == 0) {
      mkRoads()
      ans += roads.mkString
      incrDigits(N - 1)
      i += 1
    }

    out.println(i)
    ans.foreach(out.println)
  }

  /**
    * @return (depth, parent, queue)
    */
  def traceBfs(g: WUGraph, rt: Int = 0) = {
    val n = g.length
    val INF = 1e9.toInt + 10
    val q, p = Array.ofDim[Int](n)

    val edges = Array.fill[ArrayBuffer[Edge]](n)(ArrayBuffer())

    q(0) = rt
    p(rt) = -1
    val d = Array.fill[Int](n)(INF)
    d(rt) = 0
    var cur = 0
    var last = 1
    while (cur < last) {
      val v = q(cur)
      REP(g(v).length) { i =>
        val e = g(v)(i)
        val u = e.to

        // 距離が同じならまた訪れる
        if (d(u) == INF) {
          d(u) = d(v) + 1
          p(u) = v
          q(last) = u
          last += 1
          edges(u) += e

        // 距離が同じならエッジを追加
        } else if (d(u) == d(v) + 1) {
          edges(u) += e
        }
      }
      cur += 1
    }

    edges
//    (d, p, q)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}


David was given a red checkered rectangle of size $$$n \times m$$$. But he doesn't like it. So David cuts the original or any other rectangle piece obtained during the cutting into two new pieces along the grid lines. He can do this operation as many times as he wants.As a result, he will get a set of rectangles. Rectangles $$$1 \times 1$$$ are forbidden.David also knows how to paint the cells blue. He wants each rectangle from the resulting set of pieces to be colored such that any pair of adjacent cells by side (from the same piece) have different colors.What is the minimum number of cells David will have to paint?

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The next lines contain descriptions of test cases. The only line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n, m \leq 3 \cdot 10^4$$$, $$$n \cdot m \geq 2$$$).

For each test case print a single integer — the minimum number of cells David will have to paint blue.

NoteThe following pictures show how the initial rectangle can be split and cells colored blue.In the first test case:  In the second test case:  In the third test case:  In the fourth test case:

import java.io._
import java.util.StringTokenizer
import scala.+:
import scala.collection.mutable
import scala.collection.mutable._
import scala.collection.immutable._
import scala.util.Sorting.quickSort
import scala.math._

object Main extends App {
    val writer =  new PrintWriter(System.out, false)
    val reader = new BufferedReader(new InputStreamReader(System.in))
    var st = new StringTokenizer("")
    def next(): String = {while (!st.hasMoreTokens) {st = new StringTokenizer(reader.readLine())};st.nextToken()}
    def readInt(): Int = next().toInt
    def readLong(): Long = next().toLong
    def readString(): String = next()

    case class fenwick(n: Int) {
        val tree = new Array[Long](n+1)
        def query(ind: Int): Long = {
            var idx = ind
            var ans = 0L
            while (idx > 0) {
                ans += tree(idx)
                idx -= (idx & -idx)
            }
            return ans
        }
        def update(ind: Int, value: Long): Unit = {
            var idx = ind
            while (idx <= n) {
                tree(idx) += value
                idx += (idx & -idx)
            }
        }
    }

    case class pair(val a: Int, val b:  Int)extends Ordered[pair] {
        override def compare(that: pair): Int = {
            return a.compareTo(that.a)
        }
    }

    def gcd(a: Int, b: Int): Int = b match {
        case 0 => a
        case n => gcd(b, a % b)
    }
    val rem = 1000000007
    val mp = mutable.Map[Char, Int]()

    val t = readInt()
    for (tt <- 1 to t) {
        val n = readInt()
        val m = readInt()
        var a = n
        var b = m
        if (m > n) {
            a = m
            b = n
        }
        var ans = 0
        ans = (a / 3).toInt * b
        a = a % 3
        ans += (b / 3).toInt * a
        b = b % 3
        if (a == 2 && b == 2)
            ans += 2
        else if (a != 0 && b != 0)
            ans += 1
        writer.println(ans)
    }
    writer.flush()
}

import scala.io.Source

object Main extends App {
  val input = Source.fromInputStream(System.in).getLines()
  val t = input.next().toInt
  Range(0, t).map(_ => input.next().split(" "))
    .map(x => (x(0).toInt, x(1).toInt))
    .foreach(nm => {
      val (n, m) = nm
      val c3 = if (n * m % 3 == 1) (n * m) / 3 - 1 else (n * m) / 3
      val c2 = (n * m - c3 * 3) / 2
      println(c3 + c2)
    })
}


An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, where ai is the number of details of the i-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has m weapons, the i-th weapon can affect all the droids in the army by destroying one detail of the i-th type (if the droid doesn't have details of this type, nothing happens to it). A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most k shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?

The first line contains three integers n, m, k (1 ≤ n ≤ 105, 1 ≤ m ≤ 5, 0 ≤ k ≤ 109) — the number of droids, the number of detail types and the number of available shots, respectively. Next n lines follow describing the droids. Each line contains m integers a1, a2, ..., am (0 ≤ ai ≤ 108), where ai is the number of details of the i-th type for the respective robot.

Print m space-separated integers, where the i-th number is the number of shots from the weapon of the i-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length. If there are multiple optimal solutions, print any of them.  It is not necessary to make exactly k shots, the number of shots can be less.

NoteIn the first test the second, third and fourth droids will be destroyed. In the second test the first and second droids will be destroyed.

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }

          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty) {
            all(mm).addFirst(toAdd)
          } else if (toAdd >= all(mm).getFirst) {
            while (!all(mm).isEmpty && toAdd > all(mm).getFirst) {
              all(mm).removeFirst()
            }
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd >= all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 ) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
       // all(mm).add(matrix(e)(mm))
      }

      val total = maxes.foldLeft(0L)(_ + _)

      if (total > bullets) {

        (0 until m).foreach { mm =>
          if ( !all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst)
            all(mm).removeFirst()
          if (all(mm).isEmpty)
            maxes(mm) = 0
          else {
            maxes(mm) = all(mm).getFirst
          }

        }

        s = s +1
      } else {

        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (e -s + 1 >= size) {
        size = e-s+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          } else if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getLast) {
            all(mm).removeLast()
          }

          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            if (all(mm).isEmpty || toAdd > all(mm).getLast)
              all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd >= all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          } else if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getLast) {
            all(mm).removeLast()
          }

          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty) {
            all(mm).addFirst(toAdd)
          } else if (toAdd > all(mm).getFirst) {
            while (!all(mm).isEmpty && toAdd > all(mm).getFirst) {
              all(mm).removeFirst()
            }
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          } else if (!all(mm).isEmpty) {
            all(mm).removeLast()
          }

          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    var s, e = 0
    while ( s <= n-1 ) {

      val prev = maxes.clone()
      (0 until m).foreach{ mm =>
        if (matrix(e)(mm) > maxes(mm))
          maxes(mm) = matrix(e)(mm)
      }

      val total = maxes.foldLeft(0L)(_ + _)
      if (total > bullets || e == n-1) {
        if (e-s >= size) {
          size = e - s
          if ( e == n -1 ) {
            ansMax = maxes.clone()
          } else {
            ansMax = prev
          }
        }

        (0 until m).foreach{ mm =>
          maxes(mm) = 0
        }

        s = s + 1
        e = s
      } else {
        if (e < n-1)
          e = e + 1
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 ) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
       // all(mm).add(matrix(e)(mm))
      }

      val total = maxes.foldLeft(0L)(_ + _)

      if (total > bullets) {

        (0 until m).foreach { mm =>
          if ( !all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst)
            all(mm).removeFirst()
          if (all(mm).isEmpty)
            maxes(mm) = 0
          else {
            maxes(mm) = all(mm).getFirst
          }

        }

        s = s +1
      } else {

        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd >= all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (e -s + 1 > size) {
        size = e-s+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    var s, e = 0
    var loop = true
    while ( s <= n-1 && loop ) {

      val prev = maxes.clone()
      (0 until m).foreach{ mm =>
        if (matrix(e)(mm) > maxes(mm))
          maxes(mm) = matrix(e)(mm)
      }

      val total = maxes.foldLeft(0L)(_ + _)
      if (total > bullets || e == n-1) {
        if (e-s >= size) {
          size = e - s
          if (e == n -1 &&  total <=bullets ) {
            ansMax = maxes.clone()
            loop = false
          } else {
            ansMax = prev
          }
        }

        (0 until m).foreach{ mm =>
          maxes(mm) = 0
        }

        s = e+1
        e = s
      } else {
        if (e < n-1)
          e = e + 1
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    var s, e = 0
    while ( s <= n-1 ) {

      val prev = maxes.clone()
      (0 until m).foreach{ mm =>
        if (matrix(e)(mm) > maxes(mm))
          maxes(mm) = matrix(e)(mm)
      }

      val total = maxes.foldLeft(0L)(_ + _)
      if (total > bullets || e == n-1) {
        if (e-s >= size) {
          size = e - s
          if (e == n -1 &&  total <=bullets ) {
            ansMax = maxes.clone()
          } else {
            ansMax = prev
          }
        }

        (0 until m).foreach{ mm =>
          maxes(mm) = 0
        }

        s = e+1
        e = s
      } else {
        if (e < n-1)
          e = e + 1
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && s < e) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
       // all(mm).add(matrix(e)(mm))
      }

      val total = maxes.foldLeft(0L)(_ + _)

      if (total > bullets) {

        (0 until m).foreach { mm =>
          if ( !all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst)
            all(mm).removeFirst()
          if (all(mm).isEmpty)
            maxes(mm) = 0
          else {
            maxes(mm) = all(mm).getFirst
          }

        }

        s = s +1
      } else {

        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (e -s + 1 >= size) {
        size = e-s+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    var s, e = 0
    while (e <= n-1) {

      val prev = maxes.clone()
      (0 until m).foreach{ mm =>
        if (matrix(e)(mm) > maxes(mm))
          maxes(mm) = matrix(e)(mm)
      }

      val total = maxes.foldLeft(0L)(_ + _)
      if (total > bullets) {
        if (e-s > size) {
          size = e - s
          ansMax = prev
        }

        (0 until m).foreach{ mm =>
          maxes(mm) = 0
        }

        s = s + 1
        e = s
      } else {
        e += 1
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          } else if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getLast) {
            all(mm).removeLast()
          }

          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty) {
            all(mm).addFirst(toAdd)
          } else if (toAdd > all(mm).getFirst) {
            all(mm).removeFirst()
            all(mm).addFirst(toAdd)
          } else {
            while  (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd >= all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (all(mm).size > 1 && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          } else if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getLast) {
            all(mm).removeLast()
          }

          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty) {
            all(mm).addFirst(toAdd)
          } else if (toAdd >= all(mm).getFirst) {
            while (!all(mm).isEmpty && toAdd > all(mm).getFirst) {
              all(mm).removeFirst()
            }
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty ) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner
import java.util.PriorityQueue

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 ) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
       // all(mm).add(matrix(e)(mm))
      }

      val total = maxes.foldLeft(0L)(_ + _)

      if (total > bullets) {

        (0 until m).foreach { mm =>
          if (matrix(s)(mm) >= all(mm).getFirst)
            all(mm).removeFirst()
          if (all(mm).isEmpty)
            maxes(mm) = 0
          else {
            maxes(mm) = all(mm).getFirst
          }

        }
        s = s +1
      } else {

        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd >= all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

    }

    println(maxes.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          maxes(mm) = matrix(s+1)(mm)
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty) {
            all(mm).addFirst(toAdd)
          } else if (toAdd >= all(mm).getFirst) {
            while (!all(mm).isEmpty && toAdd > all(mm).getFirst) {
              all(mm).removeFirst()
            }
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) == all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
       // all(mm).add(matrix(e)(mm))
      }

      val total = maxes.foldLeft(0L)(_ + _)

      if (total > bullets) {

        (0 until m).foreach { mm =>
          if ( !all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst)
            all(mm).removeFirst()
          if (all(mm).isEmpty)
            maxes(mm) = 0
          else {
            maxes(mm) = all(mm).getFirst
          }

        }

        s = s +1
      } else {

        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (e -s + 1 >= size) {
        size = e-s+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          } else if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getLast) {
            all(mm).removeLast()
          }

          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty) {
            all(mm).addFirst(toAdd)
          } else if (toAdd > all(mm).getFirst) {
            all(mm).removeFirst()
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          } else if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getLast) {
            all(mm).removeLast()
          }

          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while  (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd >= all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>

      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 ) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
       // all(mm).add(matrix(e)(mm))
      }

      val total = maxes.foldLeft(0L)(_ + _)

      if (total > bullets) {

        (0 until m).foreach { mm =>
          if (matrix(s)(mm) >= all(mm).getFirst)
            all(mm).removeFirst()
          if (all(mm).isEmpty)
            maxes(mm) = 0
          else {
            maxes(mm) = all(mm).getFirst
          }

        }

        if (e -s + 1 > size) {
          size = e-s+1
          ansMax = maxes.clone()
        }

        s = s +1
      } else {

        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd >= all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            while (!all(mm).isEmpty && all(mm).getLast <= toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        //if (e -s + 1 > size) {
          //size = e-s+1
          ansMax = maxes.clone()
        //}
        e = e + 1
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}

import java.util
import java.util.Scanner

/**
 * Created by dimitar on 2/16/15.
 */
object Droids {

  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt()
    val m = sc.nextInt()
    val bullets = sc.nextLong()

    val matrix = Array.ofDim[Long](n,m)

    (0 until n) foreach { col =>
      (0 until m) foreach {row =>
        matrix(col)(row) = sc.nextLong()
      }
    }

    val maxes = Array.tabulate[Long](m)(_ => 0L)
    var size = 0
    var ansMax = new Array[Long](m)

    val all = Array.tabulate[util.ArrayDeque[Long]](m)(_ => new util.ArrayDeque[Long])

    var s, e = 0
    while ( e <= n-1 && s <= n-1 && e - s >= 0) {

      (0 until m).foreach { mm =>
        if (matrix(e)(mm) > maxes(mm)) {
          maxes(mm) = matrix(e)(mm)
        }
      }

      val total = maxes.foldLeft(0L)(_ + _)
      val i = s
      val j = e

      if (total > bullets && e > s) {

        (0 until m).foreach { mm =>
          if (!all(mm).isEmpty && matrix(s)(mm) >= all(mm).getFirst) {
            all(mm).removeFirst()
          }
          if (all(mm).isEmpty) {
            maxes(mm) = 0
          } else {
            maxes(mm) = all(mm).getFirst
          }
        }
        s = s +1
      } else {
        (0 until m).foreach { mm =>
          val toAdd = matrix(e)(mm)
          if (all(mm).isEmpty || toAdd > all(mm).getFirst) {
            all(mm).addFirst(toAdd)
          } else {
            if (!all(mm).isEmpty && all(mm).getLast < toAdd) {
              all(mm).removeLast()
            }
            all(mm).addLast(toAdd)
          }
        }
        e = e + 1
      }

      if (j - i + 1 >= size && total <= bullets) {
        size = j-i+1
        ansMax = maxes.clone()
      }

    }

    println(ansMax.toBuffer.mkString(" "))
  }

}


The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.

The first line contains an integer n (1 ≤ n ≤ 100) — the number of boys. The second line contains sequence a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is the i-th boy's dancing skill. Similarly, the third line contains an integer m (1 ≤ m ≤ 100) — the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 ≤ bj ≤ 100), where bj is the j-th girl's dancing skill.

Print a single number — the required maximum possible number of pairs.

object Solution {

  import scala.math._

  def main(args: Array[String]) {

    val sc = new java.util.Scanner(System.in)

    val n = sc.nextInt()
    val boys = Array.fill(n)(sc.nextInt()).sorted

    val m = sc.nextInt()
    val girls = Array.fill(m)(sc.nextInt()).sorted

    val dp = Array.fill(n)(Array.fill(m)(0))

    dp(0)(0) = if(abs(boys(0)-girls(0))<2) 1 else 0

    for (i <- (1 until m)) {
      if (dp(0)(i-1) == 1) dp(0)(i) = 1
      else if (abs(boys(0)-girls(i)) < 2) dp(0)(i) = 1
    }
    for (i <- (1 until n)) {
      if (dp(i-1)(0) == 1) dp(i)(0) = 1
      else if (abs(boys(i)-girls(0)) < 2) dp(i)(0) = 1
    }

    for (i <- (1 until n)) {
      for (j <- (1 until m)) {
        if (abs(boys(i)-girls(j)) < 2)
          dp(i)(j) = List(dp(i-1)(j-1)+1, dp(i-1)(j), dp(i)(j-1)).max
        else
          dp(i)(j) = max(dp(i-1)(j), dp(i)(j-1))
      }
    }

    println(dp(n-1)(m-1))
  }
}

object SB_2 {
  def main(args:Array[String]):Unit={
    val n:Int=readInt()
    var arr1:Array[Int]=new Array[Int](n)

    val l1=readLine()
    val line1:Array[String]=l1.split(" ")
    for(i<-0 until arr1.length)
      arr1(i)=line1(i).toInt
    arr1=arr1.sortBy(x=>x)

    val m:Int=readInt()
    var arr2:Array[Int]=new Array[Int](m)

    val l2=readLine()
    val line2:Array[String]=l2.split(" ")
    for(i<-0 until arr2.length)
      arr2(i)=line2(i).toInt
    arr2=arr2.sortBy(x=>x)

    var i:Int=0
    var j:Int=0
    var flagi:Boolean=true
    var flagj:Boolean=true
    var sum:Int=0

    while(flagi&&flagj){
      while(flagj&&flagi){
        if(Math.abs(arr1(i)-arr2(j))<=1){
          i=i+1
          j=j+1
          sum=sum+1

          if(i>=arr1.length)
            flagi=false
          if(j>=arr2.length)
            flagj=false
        }
        else{
          if(arr1(i)>arr2(j))
            j=j+1
          else
            i=i+1

          if(i>=arr1.length)
            flagi=false
          if(j>=arr2.length)
            flagj=false
        }

        if(i>=arr1.length)
          flagi=false
        if(j>=arr2.length)
          flagj=false
      }
    }

    println(sum)

  }

}

object SB_2 {
  def main(args:Array[String]):Unit={
    val n:Int=readInt()
    var arr1:Array[Int]=new Array[Int](n)

    val l1=readLine()
    val line1:Array[String]=l1.split(" ")
    for(i<-0 until arr1.length)
      arr1(i)=line1(i).toInt
    arr1=arr1.sortBy(x=>x)

    val m:Int=readInt()
    var arr2:Array[Int]=new Array[Int](m)

    val l2=readLine()
    val line2:Array[String]=l2.split(" ")
    for(i<-0 until arr2.length)
      arr2(i)=line2(i).toInt
    arr2=arr2.sortBy(x=>x)

    var i:Int=0
    var j:Int=0
    var flagi:Boolean=true
    var flagj:Boolean=true
    var sum:Int=0

    while(flagi&&flagj){
      while(flagj&&flagi){
        if(Math.abs(arr1(i)-arr2(j))<=1){
          i=i+1
          j=j+1
          sum=sum+1
        }
        else{
          if(arr1(i)>arr2(j))
            j=j+1
          else
            i=i+1
        }

        if(i>=arr1.length)
          flagi=false
        if(j>=arr2.length)
          flagj=false
      }
    }

    println(sum)

  }

}

object B489 {
  @inline def read = scala.io.StdIn.readLine

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

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

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

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

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val boys = readInts(n).sortBy(identity)
    val Array(m) = readInts(1)
    val girls = readInts(m).sortBy(identity)

    var res = 0
    var j = 0
    var lastj = 0
    for(i <- boys.indices) {
      var break = false
      while(!break && j < girls.length && girls(j) <= boys(i)+1) {
        if(math.abs(boys(i) - girls(j)) <= 1) {
          res += 1
          lastj = j
          break = true
        }
        j += 1
      }
      if(break) j = lastj+1
    }

    println(res)
  }
}

/**
  * Created by yousack on 2017/03/01.
  */

import scala.io.StdIn.readLine

object App {
  def main(args: Array[String]): Unit = {
    val n = readLine().toInt
    var a = readLine().split(" ").map(_.toInt).sorted
    val m = readLine().toInt
    var b = readLine().split(" ").map(_.toInt).sorted
    var ans = 0

    while (!a.isEmpty && !b.isEmpty) {
      if (math.abs(a{0} - b{0}) <= 1) {
        ans += 1
        a = a.drop(1)
        b = b.drop(1)
      } else if (a{0} < b{0}) {
        a = a.drop(1)
      } else {
        b = b.drop(1)
      }
    }

    print(ans)
  }
}

/**
  * Created by yousack on 2017/03/01.
  */

import scala.io.StdIn.readLine

object App {
  def main(args: Array[String]): Unit = {
    val n = readLine().toInt
    val a = readLine().split(" ").map(_.toInt).sorted
    val m = readLine().toInt
    val b = readLine().split(" ").map(_.toInt).sorted

    print(solve(a, b, 0))
  }

  def solve(a: Array[Int], b: Array[Int], p: Int): Int = {
    if (a.isEmpty || b.isEmpty) {
      return p
    }

    if (math.abs(a{0} - b{0}) <= 1) {
      solve(a.drop(1), b.drop(1), p + 1)
    } else if (a{0} < b{0}) {
      solve(a.drop(1), b, p)
    } else {
      solve(a, b.drop(1), p)
    }
  }
}

import scala.io.StdIn._

object Main2 extends App {
  val boyNumber = readInt()
  val boySkills = readLine().split(" ").map(_.toInt).sorted
  val girlNumber = readInt()
  val girlSkills = readLine().split(" ").map(_.toInt).sorted
  var boyIndex: Int = 0
  var girlIndex: Int = 0
  var ans = 0
  while(boyIndex < boyNumber && girlIndex < girlNumber) {
    val difference = boySkills(boyIndex) - girlSkills(girlIndex)
    difference match {
      case d if Math.abs(d) < 2 =>
        boyIndex += 1
        girlIndex += 1
        ans += 1
      case d if d < 0 => boyIndex += 1
      case d if d > 0 => girlIndex += 1
    }
  }
  println(ans)
}

object B {

  def main(args: Array[String]): Unit = {
    readInt()
    val a = readLine().split(" ").toList.map(_.toInt).sorted
    readInt()
    val b = readLine().split(" ").toList.map(_.toInt).sorted
    println(dancing(a, b, Map())._1)
  }

  type Memo = Map[(Int, Int), Int]

  def dancing(a: List[Int], b: List[Int], memo: Memo): (Int, Memo) = (a, b) match {
    case (Nil, _) => (0, memo)
    case (_, Nil) => (0, memo)
    case (x :: xs, y :: ys) =>
      if (memo.contains((a.length, b.length))) {
        (memo.get((a.length, b.length)).get, memo)
      } else if (math.abs(x - y) <= 1) {
        val z = dancing(xs, ys, memo)
        (z._1 + 1, z._2 + ((a.length, b.length) -> (z._1 + 1)))
      } else {
        val z1 = dancing(a, ys, memo)
        val z2 = dancing(xs, b, z1._2)
        val res = math.max(z1._1, z2._1)
        (res, z2._2 + ((a.length, b.length) -> res))
      }
  }
}

object B489 {
  @inline def read = scala.io.StdIn.readLine

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

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

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

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

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val boys = readInts(n).sortBy(identity)
    val Array(m) = readInts(1)
    val girls = readInts(m).sortBy(identity)

    var res = 0
    var j = 0
    var lastj = 0
    for(i <- boys.indices) {
      var break = false
      while(!break && j < girls.length && girls(j) <= boys(i)+1) {
        if(math.abs(boys(i) - girls(j)) <= 1) {
          res += 1
          lastj = j
          break = true
        }
        j += 1
      }
      j = lastj+1
    }

    println(res)
  }
}

object B {

  def main(args: Array[String]): Unit = {
    readInt()
    val a = readLine().split(" ").toList.map(_.toInt).sorted
    readInt()
    val b = readLine().split(" ").toList.map(_.toInt).sorted
    println(dancing(a, b, Map())._1)
  }

  type Memo = Map[(Int, Int), Int]

  def dancing(a: List[Int], b: List[Int], memo: Memo): (Int, Memo) = (a, b) match {
    case (Nil, _) => (0, memo)
    case (_, Nil) => (0, memo)
    case (x :: xs, y :: ys) =>
      if (memo.contains((x, y))) {
        (memo.get((x, y)).get, memo)
      } else if (math.abs(x - y) <= 1) {
        val z = dancing(xs, ys, memo)
        (z._1 + 1, z._2 + ((x, y) -> (z._1 + 1)))
      } else {
        val z1 = dancing(a, ys, memo)
        val z2 = dancing(xs, b, z1._2)
        val res = math.max(z1._1, z2._1)
        (res, z2._2 + ((x, y) -> res))
      }
  }
}