Let's consider all integers in the range from $$$1$$$ to $$$n$$$ (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of $$$\mathrm{gcd}(a, b)$$$, where $$$1 \leq a < b \leq n$$$.The greatest common divisor, $$$\mathrm{gcd}(a, b)$$$, of two positive integers $$$a$$$ and $$$b$$$ is the biggest integer that is a divisor of both $$$a$$$ and $$$b$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^6$$$).

For each test case, output the maximum value of $$$\mathrm{gcd}(a, b)$$$ among all $$$1 \leq a < b \leq n$$$.

NoteIn the first test case, $$$\mathrm{gcd}(1, 2) = \mathrm{gcd}(2, 3) = \mathrm{gcd}(1, 3) = 1$$$.In the second test case, $$$2$$$ is the maximum possible value, corresponding to $$$\mathrm{gcd}(2, 4)$$$.

import scala.io.StdIn

object Main {

  def main(args: Array[String]): Unit = {
    val t = StdIn.readInt()
    for (_ <- Range(0, t)) {
      val n = StdIn.readInt()
      println(n / 2)
    }
  }
}

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

    for (_ <- 1 to nextInt) {
      val n = nextInt

      out.println(n/2)
      out.flush()
    }

  }

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

object A extends App {
  import java.io._
  import java.nio.file.{Files, Path}
  import java.util.StringTokenizer

  import scala.io.Codec

  /* Implicit methods */
  implicit def intWithTimes(n: Int) = {
    new {
      def times(f: => Unit): Unit = 1.to(n).foreach { _ => f }
    }
  }
  /* Implicit methods */

//    val in = new CustomScanner(Source.fromResource("input.txt").mkString)
  val in  = new CustomScanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val n = in.nextInt()

    n.times {
      val m = in.nextInt()

      println(m / 2)

    }
  }

  try {
    solve()
  } catch {
    case ex: Exception =>
      println(ex)
      sys.exit(9999)
  } finally {
    out.flush()
    out.close()
  }

  /**
    * Util methods
    */

  def readMatrix(rows: Int, cols: Int) = {
    (1.to(rows))
      .map(_ => {
        (1.to(cols)
          .map(_ => {
            in.nextInt()
          }))
      })
  }

  def printArr[T](in: Array[T]) = {
    in.foreach(print)
    println()
  }

  /**
    * Scala implementation of a faster java.util.Scanner
    * See: http://codeforces.com/blog/entry/7018
    */
  class CustomScanner(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {

    def this(reader: BufferedReader) = this(new LineNumberReader(reader))

    def this(reader: Reader) = this(new BufferedReader(reader))

    def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))

    def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))

    def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)

    def this(str: String) = this(new StringReader(str))

    private[this] val tokenizers = {
      Iterator
        .continually(reader.readLine())
        .takeWhile(_ != null)
        .map(new StringTokenizer(_))
        .filter(_.hasMoreTokens)
    }

    private[this] var current: Option[StringTokenizer] = None

    @inline private[this] def tokenizer(): Option[StringTokenizer] =
      current.find(_.hasMoreTokens) orElse {
        current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
        current
      }

    /**
      * Unlike Java's scanner which returns till end of current line, this actually returns the next line
      * @see line() if you want the Java behaviour
      */
    def nextLine(): String = {
      current = None // reset
      reader.readLine()
    }

    def lineNumber: Int = reader.getLineNumber

    def line(): String = tokenizer().get.nextToken("\n\r")

    def nextString(): String = next()

    def nextChar(): Char = next().ensuring(_.length == 1).head

    def nextBoolean(): Boolean = next().toBoolean

    def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)

    def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)

    def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)

    def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)

    def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)

    def nextFloat(): Float = next().toFloat

    def nextDouble(): Double = next().toDouble

    def nextBigDecimal(): BigDecimal = BigDecimal(next())

    override def next(): String = tokenizer().get.nextToken()

    override def hasNext: Boolean = tokenizer().nonEmpty

    override def close(): Unit = reader.close()
  }
}


You are given an array a1, a2, ..., an and m sets S1, S2, ..., Sm of indices of elements of this array. Let's denote Sk = {Sk, i} (1 ≤ i ≤ |Sk|). In other words, Sk, i is some element from set Sk.In this problem you have to answer q queries of the two types:  Find the sum of elements with indices from set Sk: . The query format is "? k".  Add number x to all elements at indices from set Sk: aSk, i is replaced by aSk, i + x for all i (1 ≤ i ≤ |Sk|). The query format is "+ k x". After each first type query print the required sum.

The first line contains integers n, m, q (1 ≤ n, m, q ≤ 105). The second line contains n integers a1, a2, ..., an (|ai| ≤ 108) — elements of array a.  Each of the following m lines describes one set of indices. The k-th line first contains a positive integer, representing the number of elements in set (|Sk|), then follow |Sk| distinct integers Sk, 1, Sk, 2, ..., Sk, |Sk| (1 ≤ Sk, i ≤ n) — elements of set Sk. The next q lines contain queries. Each query looks like either "? k" or "+ k x" and sits on a single line. For all queries the following limits are held: 1 ≤ k ≤ m, |x| ≤ 108. The queries are given in order they need to be answered. It is guaranteed that the sum of sizes of all sets Sk doesn't exceed 105.

After each first type query print the required sum on a single line. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

import scala.collection._

object C extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }
  def readLongs(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toLong) }

  val LIM = 316

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  val Array(n, m, q) = readInts(3)
  val as = readLongs(n)
  val ss = Array.fill(m){
    val t = tokenizeLine
    val sn = t.nextToken.toInt
    Array.fill(sn)(t.nextToken.toInt - 1).sorted
  }

  val larges = (0 until m).filter(ss(_).size > LIM)
  val sset = Array.tabulate(larges.size)(i => new mutable.BitSet(ss(larges(i)).size))
  for (i <- larges.indices) sset(i) ++= ss(larges(i))

  val isLarge = new mutable.BitSet(m)
  isLarge ++= larges

  val largeIncr = Array.fill(m)(0L)
  val largeSum = Array.fill(m)(0L)
  for (i <- larges) largeSum(i) = ss(i).map(as(_)).sum

  val overlaps = Array.tabulate(m)(i => {
    Array.tabulate(larges.size)(j => {
      var k, cnt = 0
      while (k < ss(i).size) {
        if (sset(j)(ss(i)(k))) cnt += 1
        k += 1
      }
      cnt
    }
  )})

  for (i <- 0 until q) {
    val t = tokenizeLine
    if (t.nextToken == "?") {
      val k = t.nextToken.toInt - 1
      var ans = 0L
      if (isLarge(k)) ans += largeSum(k)
      else {
        var j = 0
        while (j < ss(k).size) {
          ans += as(ss(k)(j))
          j += 1
        }
      }
      var j = 0
      while (j < larges.size) {
        ans += largeIncr(larges(j)) * overlaps(k)(j)
        j += 1
      }
	  println(ans)
    } else { // +
      val k = t.nextToken.toInt - 1
      val x = t.nextToken.toInt
      if (isLarge(k)) {
        largeIncr(k) += x
      } else {
        var j = 0
        while (j < ss(k).size) {
          as(ss(k)(j)) += x
          j += 1
        }
        j = 0
   		while (j < larges.size) {
   		  largeSum(larges(j)) += x * overlaps(k)(j)
   		  j += 1
   		}
      }
    }
  }

  Console.flush
}

import scala.collection._

object C extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }
  def readLongs(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toLong) }

  val LIM = 316

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  val Array(n, m, q) = readInts(3)
  val as = readLongs(n)
  val ss = Array.fill(m){
    val t = tokenizeLine
    val sn = t.nextToken.toInt
    Array.fill(sn)(t.nextToken.toInt - 1).sorted
  }

  val larges = (0 until m).filter(ss(_).size > LIM)
  val sset = Array.tabulate(larges.size)(i => new mutable.BitSet(ss(larges(i)).size))
  for (i <- larges.indices) sset(i) ++= ss(larges(i))

  val isLarge = new mutable.BitSet(m)
  isLarge ++= larges

  val largeIncr = Array.fill(m)(0L)
  val largeSum = Array.fill(m)(0L)
  for (i <- larges) largeSum(i) = ss(i).map(as(_)).sum

  val overlaps = Array.tabulate(m)(i =>
    Array.tabulate(larges.size)(j => ss(i).count(sset(j)(_))
  ))

  for (i <- 0 until q) {
    val t = tokenizeLine
    if (t.nextToken == "?") {
      val k = t.nextToken.toInt - 1
      var ans = 0L
      if (isLarge(k)) ans += largeSum(k)
      else {
        var j = 0
        while (j < ss(k).size) {
          ans += as(ss(k)(j))
          j += 1
        }
      }
      var j = 0
      while (j < larges.size) {
        ans += largeIncr(larges(j)) * overlaps(k)(j)
        j += 1
      }
	  println(ans)
    } else { // +
      val k = t.nextToken.toInt - 1
      val x = t.nextToken.toInt
      if (isLarge(k)) {
        largeIncr(k) += x
      } else {
        var j = 0
        while (j < ss(k).size) {
          as(ss(k)(j)) += x
          j += 1
        }
        j = 0
   		while (j < larges.size) {
   		  largeSum(larges(j)) += x * overlaps(k)(j)
   		  j += 1
   		}
      }
    }
  }

  Console.flush
}

import scala.collection._

object C extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }
  def readLongs(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toLong) }
  def readBigs(n: Int) = { val t = tokenizeLine; Array.fill(n)(BigInt(t.nextToken)) }

  val Array(n, m, q) = readInts(3)
  val as = readLongs(n)
  val ss = Array.fill(m){
    val t = tokenizeLine
    val sn = t.nextToken.toInt
    Array.fill(sn)(t.nextToken.toInt - 1).sorted
  }

  val larges = (0 until m).filter(ss(_).size > 100)

  val isLarge = new mutable.BitSet(m)
  isLarge ++= larges

  val largeIncr = Array.fill(m)(0L)
  val largeSum = Array.fill(m)(0L)
  for (i <- larges) largeSum(i) = ss(i).sum

  val overlaps = for (i <- larges)
    yield for (j <- 0 until m) yield {
      var a, b, cnt = 0
      while (a < ss(i).size && b < ss(j).size) {
        val ssa = ss(i)(a)
        val ssb = ss(j)(b)
        if (ssa == ssb) {
          cnt += 1
          a += 1
          b += 1
        } else if (ssa < ssb) a += 1
        else b += 1
      }
      cnt
    }

  for (i <- 0 until q) {
    val t = tokenizeLine
    if (t.nextToken == "?") {
      val k = t.nextToken.toInt - 1
      var ans = 0L
      if (isLarge(k)) ans += largeSum(k)
      else for (j <- ss(k)) ans += as(j)
      for (j <- larges) ans += largeIncr(j) * overlaps(k)(j)
	  println(ans)
    } else { // +
      val k = t.nextToken.toInt - 1
      val x = t.nextToken.toInt
      if (isLarge(k)) {
        largeIncr(k) += x
      } else {
        for (j <- ss(k)) as(j) += x
   		for (j <- larges) largeSum(j) += x * overlaps(k)(j)
      }
    }
  }
}

import scala.collection._

object C extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }
  def readLongs(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toLong) }
  def readBigs(n: Int) = { val t = tokenizeLine; Array.fill(n)(BigInt(t.nextToken)) }

  val Array(n, m, q) = readInts(3)
  val as = readInts(n)
  val ss = Array.fill(m){
    val t = tokenizeLine
    val sn = t.nextToken.toInt
    Array.fill(sn)(t.nextToken.toInt - 1)
  }

  val larges = (0 until m).filter(ss(_).size > 100)

  val isLarge = new mutable.BitSet(m)
  isLarge ++= larges

  val largeIncr = Array.fill(m)(0)
  val largeSum = Array.fill(m)(0)
  for (i <- larges) largeSum(i) = ss(i).sum

  val overlaps = for (i <- larges)
    yield for (j <- 0 until m; intersect = ss(i).intersect(ss(j))) yield intersect.size

  for (i <- 0 until q) {
    val t = tokenizeLine
    if (t.nextToken == "?") {
      val k = t.nextToken.toInt - 1
      var ans = 0L
      if (isLarge(k)) {
        ans += largeSum(k)
        for (j <- larges) ans += (largeIncr(j) * overlaps(k)(j))
      } else {
        for (j <- ss(k)) ans += as(j)
        for (j <- larges) ans += (largeIncr(j) * overlaps(k)(j))
      }
	  println(ans)
    } else { // +
      val k = t.nextToken.toInt - 1
      val x = t.nextToken.toInt
      if (isLarge(k)) {
        largeIncr(k) += x
      } else {
        for (j <- ss(k)) as(j) += x
   		for (j <- larges) largeSum(j) += (x * overlaps(k)(j))
      }
    }
  }
}


Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of n coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum x with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding x. Obviously, if among the coins' face values exists the face value 1, any sum x can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values {1, 3, 4} and it is asked to collect the sum 6, greedy algorithm will represent the sum as 4 + 1 + 1, while the optimal representation is 3 + 3, containing one coin less. By the given set of face values find out if there exist such a sum x that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums.

The first line contains an integer n (1 ≤ n ≤ 400) — the amount of the coins' face values. The second line contains n integers ai (1 ≤ ai ≤ 109), describing the face values. It is guaranteed that a1 > a2 > ... > an and an = 1.

If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way.

import java.util.Scanner

object GreedyChoice3 extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (0 until n).map(x => s.nextLong()).toArray
  coins(n - 1) = 1
  val g = Array.ofDim[Int](n)
  var cnt = 0
  var min = Long.MaxValue
  val arr = Array.ofDim[Long](n)
  var len = 0
  var i = 1
  var j = 0
  while (i < n) {
    j = 0
    val v1 = greedy(coins(i - 1) - 1).map(x => x)
    while (j < n) {
      val(s, vs) = dot(v1, j)
      val w = greedy(s)
      val ws = svv()
      if (ws > vs && s < min) {
        min = s
      }
      j += 1
    }
    i += 1
  }

  def svv(): Long = {
    var i = 0
    var sum = 0L
    while (i < len + 1) {
      sum += arr(i)
      i += 1
    }
    sum
  }

  if (min == Long.MaxValue) {
    println("-1")
  } else {
    println(min)
  }

  def dot(a: Array[Long], j: Int): (Long, Long) = {
    if (j == 0) {
      return (0, Long.MaxValue)
    }
    var i = 0
    var sum = 0L
    var sum1 = 0L
    while (i < j) {
      sum += coins(i) * a(i)
      sum1 += (a(i))
      i += 1
    }
    sum += coins(j) * (a(j) + 1)
    sum1 += a(j) + 1
    (sum, sum1)
  }

  def greedy(v: Long): Array[Long] = {
    var s = v
    var i = 0
    i = 0
   /* while (i < n) {
      arr(i) = 0
      i += 1
    }*/
    i = 0
    arr(i) = 0
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
        arr(i) = 0
      } else {
        val mod = s % coins(i)
        arr(i) += (s / coins(i))
        s = mod
      }
    }
    len = i
    arr
  }

}

import java.util.Scanner

object GreedyChoice2 extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (0 until n).map(_ => s.nextInt())
  val g = Array.ofDim[Int](n)
  var cnt = 0
  var min = Integer.MAX_VALUE

  var i = 1
  var j = 0
  while (i < n) {
    j = 0
    val v = greedy(coins(i - 1) - 1)
    val vs = v.sum + 1
    while (j < n) {
      val s = dot(v, j)
      val w = greedy(s)
      val ws = w.sum
      if (ws > vs && s < min) {
        min = s
      }
      j += 1
    }
    i += 1
  }

  if (min == Integer.MAX_VALUE) {
    println("-1")
  } else {
    println(min)
  }

  def dot(a: Array[Int], j: Int): Int = {
    var i = 0
    var sum = 0
    while (i < j) {
      sum += coins(i) * a(i)
      i += 1
    }
    sum += coins(j) * (a(j) + 1)
    sum
  }

  def greedy(v: Int): Array[Int] = {
    val arr = Array.ofDim[Int](n)
    var s = v
    var i = 0
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
      } else {
        s -= coins(i)
        arr(i) += 1
      }
    }
    arr
  }

}

//package me.jetblack.cf

import java.util.Scanner

object GreedyChoice extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (Integer.MAX_VALUE :: (0 until n).map(_ => s.nextInt()).toList)

  if (n == 1) {
    println("-1")
  } else {
    val ans = (1 until n + 1).map(check)
    if (ans.forall(_ == true)) {
      println("-1")
    } else {
      val t = ans.zipWithIndex.reverse.find(_._1 == false).get._2
      val ceil = math.ceil(coins(t - 1).toDouble / coins(t).toDouble).toInt
      val result = ceil * coins(t)
      println(result)
    }
  }

  def ceil(t: Int): Int = math.ceil(coins(t - 1).toDouble / coins(t).toDouble).toInt

  def check(t: Int): Boolean = {
    val mt = math.ceil(coins(t - 1).toDouble / coins(t).toDouble).toInt
    val st = mt * coins(t)
    var s = st - coins(t - 1)
    var g = 0
    var i = t
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
      } else {
        s -= coins(i)
        g += 1
      }
    }
    (g <= mt - 1)
  }

}

import java.util.Scanner

object GreedyChoice2 extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (0 until n).map(_ => s.nextInt())
  val g = Array.ofDim[Int](n)
  var cnt = 0
  var min = Long.MaxValue

  var i = 1
  var j = 0
  while (i < n) {
    j = 0
    val v = greedy(coins(i - 1) - 1)
    while (j < n) {
      val s = dot(v, j)
      val vs = v.take(j).sum + 1
      val w = greedy(s)
      val ws = w.sum
      if (ws > vs && s < min) {
        min = s
      }
      j += 1
    }
    i += 1
  }

  if (min == Long.MaxValue) {
    println("-1")
  } else {
    println(min)
  }

  def dot(a: Array[Long], j: Int): Long = {
    var i = 0
    var sum = 0L
    while (i < j) {
      sum += coins(i) * a(i)
      i += 1
    }
    sum += coins(j) * (a(j) + 1)
    sum
  }

  def greedy(v: Long): Array[Long] = {
    val arr = Array.ofDim[Long](n)
    var s = v
    var i = 0
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
      } else {
        val mod = s % coins(i)
        arr(i) += (s / coins(i))
        s = mod
      }
    }
    arr
  }

}

//package me.jetblack.cf

import java.util.Scanner

object GreedyChoice extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (0 until n).map(_ => s.nextInt())

  if (n == 1) {
    println("-1")
  } else {
    val ans = (1 until n).map(check)
    if (ans.forall(_ == true)) {
      println("-1")
    } else {
      val t = ans.zipWithIndex.find(_._1 == false).get._2 + 1
      val result = math.ceil(coins(t - 1).toDouble / coins(t).toDouble).toInt * coins(t)
      println(result)
    }
  }

  def check(t: Int): Boolean = {
    val mt = math.ceil(coins(t - 1).toDouble / coins(t).toDouble).toInt
    val st = mt * coins(t)
    var s = st - coins(t - 1)
    var g = 0
    var i = t
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
      } else {
        s -= coins(i)
        g += 1
      }
    }
    (g <= mt - 1)
  }

}

import java.util.Scanner

object GreedyChoice3 extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (0 until n).map(x => s.nextLong()).toArray
  coins(n - 1) = 1
  val g = Array.ofDim[Int](n)
  var cnt = 0
  var min = Long.MaxValue
  val arr = Array.ofDim[Long](n)

  var i = 1
  var j = 0
  while (i < n) {
    j = 0
    val v1 = greedy(coins(i - 1) - 1).map(x => x)
    while (j < n) {
      val(s, vs) = dot(v1, j)
      val w = greedy(s)
      val ws = w.sum
      if (ws > vs && s < min) {
        min = s
      }
      j += 1
    }
    i += 1
  }

  if (min == Long.MaxValue) {
    println("-1")
  } else {
    println(min)
  }

  def dot(a: Array[Long], j: Int): (Long, Long) = {

    if (j == 0) {
      return (0, Long.MaxValue)
    }
    var i = 0
    var sum = 0L
    var sum1 = 0L
    while (i < j) {
      sum += coins(i) * a(i)
      sum1 += (a(i))
      i += 1
    }
    sum += coins(j) * (a(j) + 1)
    sum1 += 1
    (sum, sum1)
  }

  def greedy(v: Long): Array[Long] = {
    var s = v
    var i = 0
    while (i < n) {
      arr(i) = 0
      i += 1
    }
    i = 0
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
      } else {
        val mod = s % coins(i)
        arr(i) += (s / coins(i))
        s = mod
      }
    }
    arr
  }

}

import java.util.Scanner

object GreedyChoice2 extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (0 until n).map(_ => s.nextInt())
  val g = Array.ofDim[Int](n)
  var cnt = 0
  var min = Long.MaxValue

  var i = 1
  var j = 0
  while (i < n) {
    j = 0
    val v = greedy(coins(i - 1) - 1)
    val vs = v.sum + 1
    while (j < n) {
      val s = dot(v, j)
      val w = greedy(s)
      val ws = w.sum
      if (ws > vs && s < min) {
        min = s
      }
      j += 1
    }
    i += 1
  }

  if (min == Long.MaxValue) {
    println("-1")
  } else {
    println(min)
  }

  def dot(a: Array[Long], j: Int): Long = {
    var i = 0
    var sum = 0L
    while (i < j) {
      sum += coins(i) * a(i)
      i += 1
    }
    sum += coins(j) * (a(j) + 1)
    sum
  }

  def greedy(v: Long): Array[Long] = {
    val arr = Array.ofDim[Long](n)
    var s = v
    var i = 0
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
      } else {
        s -= coins(i)
        arr(i) += 1
      }
    }
    arr
  }

}

import java.util.Scanner

object GreedyChoice2 extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (0 until n).map(_ => s.nextInt())
  val g = Array.ofDim[Int](n)
  var cnt = 0
  var min = Long.MaxValue

  var i = 1
  var j = 0
  while (i < n) {
    j = 0
    val v = greedy(coins(i - 1) - 1)
    val vs = v.sum + 1
    while (j < n) {
      val s = dot(v, j)
      val w = greedy(s)
      val ws = w.sum
      if (ws > vs && s < min) {
        min = s
      }
      j += 1
    }
    i += 1
  }

  if (min == Long.MaxValue) {
    println("-1")
  } else {
    println(min)
  }

  def dot(a: Array[Long], j: Int): Long = {
    var i = 0
    var sum = 0L
    while (i < j) {
      sum += coins(i) * a(i)
      i += 1
    }
    sum += coins(j) * (a(j) + 1)
    sum
  }

  def greedy(v: Long): Array[Long] = {
    val arr = Array.ofDim[Long](n)
    var s = v
    var i = 0
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
      } else {
        val mod = s % coins(i)
        arr(i) += (s / coins(i))
        s = mod
      }
    }
    arr
  }

}

//package me.jetblack.cf

import java.util.Scanner

object GreedyChoice extends App {

  val s = new Scanner(System.in)
  val n = s.nextInt()
  val coins = (0 until n).map(_ => s.nextInt())

  if (n == 1) {
    println("-1")
  } else {
    val ans = (1 until n).map(check)
    if (ans.forall(_ == true)) {
      println("-1")
    } else {
      val t = ans.zipWithIndex.reverse.find(_._1 == true).get._2
      val result = math.ceil(coins(t - 1).toDouble / coins(t).toDouble).toInt * coins(t)
      println(result)
    }
  }

  def check(t: Int): Boolean = {
    val mt = math.ceil(coins(t - 1).toDouble / coins(t).toDouble).toInt
    val st = mt * coins(t)
    var s = st - coins(t - 1)
    var g = 0
    var i = t
    while (s != 0) {
      if (coins(i) > s) {
        i += 1
      } else {
        s -= coins(i)
        g += 1
      }
    }
    (g <= mt - 1)
  }

}


You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Print the only number — distance between two nearest minimums in the array.

/**
  * @see http://codeforces.com/contest/911/problem/A
  */

object ANearestMinimums extends App {
  def distance(i: Int = 0, j: Int = 0, m: Int = Int.MaxValue, d: Int = Int.MaxValue): Int =
    if (i == n) d
    else {
      val h = a(i)
      if (h < m) distance(i + 1, i, h, Int.MaxValue)
      else if (h == m) distance(i + 1, i, m, d min (i - j))
      else distance(i + 1, j, m, d)
    }

  val n = scala.io.StdIn.readInt()
  val a = scala.io.StdIn.readLine().split(" ").map(_.toInt)

  println(distance())
}

object A {
  import scala.io.StdIn
  import scala.collection.mutable

  def main(args: Array[String]): Unit = {
    StdIn.readLine() // skip the 1st line
    val lastPos = mutable.HashMap[Int, (Int, Int)]()
    for ((x, cur) <- StdIn.readLine().split(' ').toStream.map(_.toInt).zipWithIndex) {
      lastPos(x) = (cur, lastPos.get(x) match {
        case Some((prv, min)) => Math.min(cur-prv, min)
        case _ => Int.MaxValue
      })

    }
    println(lastPos(lastPos.keys.min)._2)
  }
}

object CF911A extends App {

  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt
  val i = Array.fill(n)(sc.nextInt).iterator

  var min = Int.MaxValue
  var mindist = Int.MaxValue
  var dist = 1
  while (i.hasNext) {
    i.next match {
      case c if c < min => min = c; mindist = Int.MaxValue; dist = 1
      case c if c == min => mindist = Math.min(mindist, dist); dist = 1
      case _ => dist += 1
    }
  }
  println(mindist)
}

import java.io.FileReader
import java.util
import java.util.Comparator

import collection.Searching._
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn

object Solution {
  //Console.setIn(new FileReader(new java.io.File("in")))
  //Console.setOut(new PrintStream(new java.io.File("output")))
  var caseNo = 1
  val magicMod = 1000000007

  def doCase() : Unit = {
    val Array(n) = readIntLine()
    val nums = readIntLine()
    val min = nums.min
    val idxs = nums.zipWithIndex.filter(_._1 == min).map(_._2)
    val diffs = idxs.sliding(2).map { case Array(a, b) => b - a}
    println(diffs.min)
  }

  def main(args: Array[String]): Unit = {
    val noCases = 1
    for (i <- 0 until noCases) {
      doCase()
    }
  }

  def readIntLine(): Array[Int] = {
    StdIn.readLine().split(" ").map(_.toInt)
  }

  def readLongLine(): Array[Long] = {
    StdIn.readLine().split(" ").map(_.toLong)
  }

  def readBigIntLine(): Array[BigInt] = {
    StdIn.readLine().split(" ").map(BigInt.apply)
  }
}

object A {
  import scala.io.StdIn
  import scala.collection.mutable

  def main(args: Array[String]): Unit = {
    StdIn.readLine() // skip the 1st line
    val lastPos = mutable.HashMap[Int, Int]()
    var min = Int.MaxValue
    for ((x, cur) <- StdIn.readLine().split(' ').toStream.map(_.toInt).zipWithIndex) {
      lastPos.get(x) match {
        case Some(prv) => min = Math.min(cur - prv, min)
        case _ =>
      }
      lastPos(x) = cur
    }
    println(min)
  }
}

object CF911A extends App {

  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt
  val i = Array.fill(n)(sc.nextInt).iterator

  var min = Int.MaxValue
  var mindist = Int.MaxValue
  var dist = 1
  while (i.hasNext) {
    i.next match {
      case c if c < min => min = c; mindist = Int.MaxValue
      case c if c == min => mindist = Math.min(mindist, dist); dist = 1
      case _ => dist += 1
    }
  }
  println(mindist)
}

object CF911A extends App {

  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt
  val i = Array.fill(n)(sc.nextInt).iterator

  var min = Int.MaxValue
  var mindist = Int.MaxValue
  var dist = 1
  while (i.hasNext) {
    i.next match {
      case c if c < min => min = c; mindist = 0
      case c if c == min => mindist = Math.min(mindist, dist); dist = 1
      case _ => dist += 1
    }
  }
  println(mindist)
}

object CF911A extends App {

  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt
  val i = Array.fill(n)(sc.nextInt).iterator

  var min = Int.MaxValue
  var mindist = Int.MaxValue
  var dist = 1
  while (i.hasNext) {
    i.next match {
      case c if c < min => min = c; mindist = Int.MaxValue; dist = 1
      case c if c == min => mindist = Math.min(mindist, dist); dist = 1
      case _ => dist += 1
    }
    println("%d %d %d".format(min,mindist,dist))
  }
  println(mindist)
}

object CF911A extends App {

  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt
  val i = Array.fill(n)(sc.nextInt).iterator

  var min = Int.MaxValue
  var mindist = 0
  var dist = 1
  while (i.hasNext) {
    i.next match {
      case c if c < min => min = c; mindist = 0
      case c if c == min => mindist = Math.min(mindist, dist); dist = 1
      case _ => dist += 1
    }
  }
  println(mindist)
}


The Greatest Secret Ever consists of n words, indexed by positive integers from 1 to n. The secret needs dividing between k Keepers (let's index them by positive integers from 1 to k), the i-th Keeper gets a non-empty set of words with numbers from the set Ui = (ui, 1, ui, 2, ..., ui, |Ui|). Here and below we'll presuppose that the set elements are written in the increasing order.We'll say that the secret is safe if the following conditions are hold:  for any two indexes i, j (1 ≤ i < j ≤ k) the intersection of sets Ui and Uj is an empty set;  the union of sets U1, U2, ..., Uk is set (1, 2, ..., n);  in each set Ui, its elements ui, 1, ui, 2, ..., ui, |Ui| do not form an arithmetic progression (in particular, |Ui| ≥ 3 should hold). Let us remind you that the elements of set (u1, u2, ..., us) form an arithmetic progression if there is such number d, that for all i (1 ≤ i < s) fulfills ui + d = ui + 1. For example, the elements of sets (5), (1, 10) and (1, 5, 9) form arithmetic progressions and the elements of sets (1, 2, 4) and (3, 6, 8) don't.Your task is to find any partition of the set of words into subsets U1, U2, ..., Uk so that the secret is safe. Otherwise indicate that there's no such partition.

The input consists of a single line which contains two integers n and k (2 ≤ k ≤ n ≤ 106) — the number of words in the secret and the number of the Keepers. The numbers are separated by a single space.

If there is no way to keep the secret safe, print a single integer "-1" (without the quotes). Otherwise, print n integers, the i-th of them representing the number of the Keeper who's got the i-th word of the secret. If there are multiple solutions, print any of them.

import collection.mutable.Map
import collection.mutable.ArrayBuffer

object test6 extends App {
    val Array(n,k)=readLine.split(" ").map(_.toInt)
    if(n<k*3) {println(-1);exit(0)}
    val arr=new Array[Int](n)
    var i=0
    var start=0
    while(i<n){
        start%=2
        for(j<-0 until k if i<n){
            arr(i)=(j+start)%k+1
            i+=1
        }

        start+=1
    }

    println(arr.mkString(" "))
}

import collection.mutable.Map
import collection.mutable.ArrayBuffer

object test6 extends App {
    val Array(n,k)=readLine.split(" ").map(_.toInt)
    if(n<k*3) {println(-1);exit(0)}
    var i=0
    var start=0
    while(i<n){
        start%=k
        for(j<-0 until k if i<n){
            print((j+start)%k+1+" ")
            i+=1
        }

        start+=1
    }
}


Nauuo is a girl who loves playing chess.One day she invented a game by herself which needs $$$n$$$ chess pieces to play on a $$$m\times m$$$ chessboard. The rows and columns are numbered from $$$1$$$ to $$$m$$$. We denote a cell on the intersection of the $$$r$$$-th row and $$$c$$$-th column as $$$(r,c)$$$.The game's goal is to place $$$n$$$ chess pieces numbered from $$$1$$$ to $$$n$$$ on the chessboard, the $$$i$$$-th piece lies on $$$(r_i,\,c_i)$$$, while the following rule is satisfied: for all pairs of pieces $$$i$$$ and $$$j$$$, $$$|r_i-r_j|+|c_i-c_j|\ge|i-j|$$$. Here $$$|x|$$$ means the absolute value of $$$x$$$.However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small.She wants to find the smallest chessboard on which she can put $$$n$$$ pieces according to the rules.She also wonders how to place the pieces on such a chessboard. Can you help her?

The only line contains a single integer $$$n$$$ ($$$1\le n\le 1000$$$) — the number of chess pieces for the game.

The first line contains a single integer — the minimum value of $$$m$$$, where $$$m$$$ is the length of sides of the suitable chessboard. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$r_i$$$ and $$$c_i$$$ ($$$1\le r_i,c_i\le m$$$) — the coordinates of the $$$i$$$-th chess piece. If there are multiple answers, print any.

NoteIn the first example, you can't place the two pieces on a $$$1\times1$$$ chessboard without breaking the rule. But you can place two pieces on a $$$2\times2$$$ chessboard like this:In the second example, you can't place four pieces on a $$$2\times2$$$ chessboard without breaking the rule. For example, if you place the pieces like this:then $$$|r_1-r_3|+|c_1-c_3|=|1-2|+|1-1|=1$$$, $$$|1-3|=2$$$, $$$1<2$$$; and $$$|r_1-r_4|+|c_1-c_4|=|1-2|+|1-2|=2$$$, $$$|1-4|=3$$$, $$$2<3$$$. It doesn't satisfy the rule.However, on a $$$3\times3$$$ chessboard, you can place four pieces like this:

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 val oj = System.getenv("ATCODER_DEBUG") == null
  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  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()
    }
  }
  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  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 = 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) { i =>
      f(i)
    }
  }
  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
  }
}

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

  // toIntとか+7とかするならvalにしろ
  @inline private def MOD = 1000000007

  def solve(): Unit = {
    val N = ni()
    val m = (N  - 1 + 2 - 1) / 2 + 1
    val ans = Array.ofDim[(Int, Int)](N)

    REP(N / 2) { i =>
      ans(i) = (1, i + 1)
    }
    REP(N / 2) { i =>
      ans(N - 1 - i) = (m, m - i)
    }
   if (N % 2 == 1) ans(N / 2) = (1, N / 2 + 1)

    debug(s"m:$m")
    debug(ans.mkString(" "))

    out.println(m)
    REP(N) { i =>
      out.println(s"${ans(i)._1} ${ans(i)._2}")
    }
  }
}

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 val oj = System.getenv("ATCODER_DEBUG") == null
  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  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()
    }
  }
  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  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 = 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) { i =>
      f(i)
    }
  }
  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
  }
}

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

  // toIntとか+7とかするならvalにしろ
  @inline private def MOD = 1000000007

  def solve(): Unit = {
    val N = ni()
    val m = (N  - 1 + 2) / 2 + 1
    val ans = Array.ofDim[(Int, Int)](N)
    REP(N / 2) { i =>
      ans(i) = (1, i + 1)
    }
    REP(N / 2) { i =>
      ans(N - 1 - i) = (m, m - i)
    }
    if (N % 2 == 1) ans(N / 2) = (1, N / 2 + 1)

    debug(s"m:$m")
    debug(ans.mkString(" "))

    out.println(m)
    REP(N) { i =>
      out.println(s"${ans(i)._1} ${ans(i)._2}")
    }
  }
}


In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed:  a1 ≤ a2 ≤ a3 ≤ a4  a1 = a2  a3 = a4 A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7.Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?

The first line of the input contains a positive integer n (1 ≤ n ≤ 105) — the number of the available sticks. The second line of the input contains n positive integers li (2 ≤ li ≤ 106) — the lengths of the sticks.

The first line of the output must contain a single non-negative integer — the maximum total area of the rectangles that Ilya can make from the available sticks.

import java.io._

import scala.collection.mutable.ArrayBuffer

object C {

  def main(args: Array[String]) {
    new C().run()
  }
}

class C {

  var in: StreamTokenizer = _

  var out: PrintWriter = _

  def nextInt(): Int = {
    in.nextToken()
    in.nval.toInt
  }
  def nextLong():Long ={
    in.nextToken()
    in.nval.toLong
  }

  def nextDouble(): Double = {
    in.nextToken()
    in.nval
  }

  def next(): String = {
    in.ordinaryChars('0', '9')
    in.wordChars('0', '9')
    in.nextToken()
    in.sval
  }

  def run() {
    in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)))
    out = new PrintWriter(new OutputStreamWriter(System.out))
    solve()
    out.flush()
  }

  def solve() {
    val n:Long = nextLong()
    val aa = (for(i<-0L.until(n)) yield nextLong()).toBuffer
    val a = aa.sortWith(_>_)
    var b = new ArrayBuffer[(Long,Long)]()
    var i = 0
    while(i<n-1){
      if(a(i)==a(i+1)) {
        b += ((a(i),a(i+1)))
        i+=1
      } else if(a(i)-1==a(i+1)) {
        b += ((a(i)-1,a(i+1)))
        i+=1
      }
      i+=1
    }
    i = 0
    var s = 0L
    while(i+1<b.length){
      s+=b(i)._1*b(i+1)._1
      i+=2
    }
    out.println(s)

  }
}

object C{
  //import scala.collection.mutable.PriorityQueue
  //import scala.collection.mutable.ArrayBuffer
  import util.control.Breaks._
  import java.io.{PrintWriter,InputStream,BufferedReader,InputStreamReader}
  import java.util.{Locale, Scanner,StringTokenizer}
  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
    val reader=new BufferedReader(new InputStreamReader(stream), 32768);

    def next():String = {
        while (!st.hasMoreTokens()){
          val currentLine = reader.readLine
          st=new StringTokenizer(currentLine)
        }
        return st.nextToken
    }

    def nextInt():Int = {
        return next.toInt
    }

    def nextLong():Long = {
    	return next.toLong
    }
    def nextDouble:Double = {
      return next.toDouble
    }
  }

  def main(args: Array[String]){

    val in = new InputReader(System.in)
    val out = new PrintWriter(System.out)

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    //code starts here
    val n=nextInt
    val l=Array.fill(n)(0L)
    for(i<-0 until n){l(i)=nextLong}
    scala.util.Sorting.quickSort(l)
    var lastPair=0L
    var sum=0L
    var i=n-1
    while(i>0){
      if(l(i)==l(i-1)||l(i)-1==l(i-1)){

        if(lastPair==0){
          lastPair=l(i-1)

        }else{
          sum+=lastPair*l(i-1)
          lastPair=0
        }

        i-=2
      }
      else{
        i-=1
      }
    }
    out.println(sum)

    //code ends here
    out.flush
    out.close
  }
}

object CodeForces extends App {
  val n = readLine toInt
  val sticks = readLine split (' ') map (s => s.toLong)
  scala.util.Sorting.quickSort(sticks)

  val result = sticks.foldRight((0L, -1L, -1L))((stick, state) => state match {
    case (area, -1, oneside) => (area, stick, oneside)
    case (area, last, -1) => if (last - stick <= 1) (area, -1, stick) else (area, stick, -1)
    case (area, last, oneside) => {
      if (last - stick <= 1) (area + (stick * oneside), -1, -1) else (area, stick, oneside)
    }
  })

  println(result._1)
}

import scala.collection.mutable.ArrayBuffer

object Solve extends App with fastIO{
  val n:Long = nextLong
  val aa = (for(i<-0L.until(n)) yield nextLong).toBuffer
  val a = aa.sortWith(_>_)
  var b = new ArrayBuffer[(Long,Long)]()
  var i = 0
  while(i<n-1){
    if(a(i)==a(i+1)) {
      b += ((a(i),a(i+1)))
      i+=1
    } else if(a(i)-1==a(i+1)) {
      b += ((a(i)-1,a(i+1)))
      i+=1
    }
    i+=1
  }
  i = 0
  var s = 0L
  while(i+1<b.length){
    s+=b(i)._1*b(i+1)._1
    i+=2
  }
  println(s)
  flush
}

trait fastIO {
  import java.io._
  val in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)))
  val out = new PrintWriter(new OutputStreamWriter(System.out))
  def next: String = {
    in.ordinaryChars('0', '9')
    in.wordChars('0', '9')
    in.nextToken()
    in.sval
  }
  def nextInt: Int = {
    in.nextToken()
    in.nval.toInt
  }
  def nextLong:Long ={
    in.nextToken()
    in.nval.toLong
  }
  def nextDouble: Double = {
    in.nextToken()
    in.nval
  }
  def println(o:Any) = out.println(o)
  def print(o:Any) = out.print(o)
  def printf(s:String,o:Any*) = out.printf(s,o)
  def flush() = out.flush()
}

import scala.collection.mutable.ArrayBuffer

object Solve extends App{
  val n:Long = fastIO.nextLong
  val aa = (for(i<-0L.until(n)) yield fastIO.nextLong).toBuffer
  val a = aa.sortWith(_>_)
  var b = new ArrayBuffer[(Long,Long)]()
  var i = 0
  while(i<n-1){
    if(a(i)==a(i+1)) {
      b += ((a(i),a(i+1)))
      i+=1
    } else if(a(i)-1==a(i+1)) {
      b += ((a(i)-1,a(i+1)))
      i+=1
    }
    i+=1
  }
  i = 0
  var s = 0L
  while(i+1<b.length){
    s+=b(i)._1*b(i+1)._1
    i+=2
  }
  fastIO.println(s)
  fastIO.flush
}
object fastIO {
  import java.io._
  val in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)))
  val out = new PrintWriter(new OutputStreamWriter(System.out))
  def next: String = {
    in.ordinaryChars('0', '9')
    in.wordChars('0', '9')
    in.nextToken()
    in.sval
  }
  def nextInt: Int = {
    in.nextToken()
    in.nval.toInt
  }
  def nextLong:Long ={
    in.nextToken()
    in.nval.toLong
  }
  def nextDouble: Double = {
    in.nextToken()
    in.nval
  }
  def println(o:Any) = out.println(o)
  def print(o:Any) = out.print(o)
  def printf(s:String,o:Any*) = out.printf(s,o)
  def flush() = out.flush()
}

object Main extends App {
   println(readLine().split(" ").map(_.toInt).sum)
}


Vasya has found a piece of paper with a coordinate system written on it. There are n distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to n. It turned out that points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square.Vasya wants to find such integer point (with integer coordinates) of the plane, that belongs to exactly k drawn squares. We'll say that a point belongs to a square, if the point is located either inside the square, or on its boundary. Help Vasya find a point that would meet the described limits.

The first line contains two space-separated integers n, k (1 ≤ n, k ≤ 50). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). It is guaranteed that all given squares are distinct.

In a single line print two space-separated integers x and y (0 ≤ x, y ≤ 109) — the coordinates of the point that belongs to exactly k squares. If there are multiple answers, you are allowed to print any of them.  If there is no answer, print "-1" (without the quotes).

import java.util.StringTokenizer

object _263B extends App {
  var token = new StringTokenizer("")
  def next = {
    while (!token.hasMoreTokens()) token = new StringTokenizer(readLine)
    token.nextToken()
  }
  val n = next.toInt
  val k = next.toInt
  val a = (1 to n).map(i => next.toInt).groupBy(i => i).toArray.map(i => (i._1, i._2.size)).sortBy(i => -i._1)
  val b = a.unzip._2.scanLeft(0)(_ + _).drop(1)
  val index = b.indexWhere(_ == k)
  if (index == -1) println(-1)
  else println(a(index)._1 + " " + a(index)._1)
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  var Array(n, k) = in.next().split(" ").map(_.toInt)
  val data = in.next().split(" ").map(_.toInt).sorted.reverse
  if (k > n)
    println(-1)
  else {
    if (k == n)
      println("0 0")
    else if (data(k - 1) == data(k))
      println(-1)
    else
      println(s"${data(k - 1)} ${data(k - 1)}")
  }

}

object Contest extends App{
  val Array(n, k) = readLine().split(" ").map(_.toInt)
  val as = readLine().split(" ").map(_.toInt)
  util.Sorting.stableSort(as)
  if (k > n) println("-1")
  else if (k == 0) println((as(n-1) + 1) + " " + (as(n-1) + 1))
  else println(as(n-k) + " " + as(n-k))
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P263B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N, K = sc.nextInt
  val a = List.fill(N)(sc.nextInt).sorted.reverse
  val answer: String =
    if (K > N) "-1"
    else {
      val x = a(K - 1)
      x + " " + x
    }

  out.println(answer)
  out.close
}

object Main {
  def main(args: Array[String]) {
    val nk = readLine().split(" ").map(_.toInt)
    val n = nk(0)
    val k = nk(1)
    val a = readLine().split(" ").map(_.toInt)
    if (k > n) println("-1")
    else if (k == n) println("0 0")
    else {
      val (head, tail) = a.sorted.reverse.splitAt(k)
      if (head.last == tail.head) println("-1")
      else println(head.last + " " + head.last)
    }
  }
}

import scala.Math.pow;

object App {
    def readInts:Array[Int] = (readLine split " ") map (_.toInt)
                                                  //> readInts: => Array[Int]                              //> solve: (n: Int, m: Int, prev: Int, a: Int, b: Int)(Int, Int)

    def f1_io() = {
        val Array(n, k) = readInts
        val source = readInts.sorted
        val p = n - k
        if (p == 0) print(source(p) + " " + source(p))
        else if (p < 0) print(-1)
        else if (source(p) == source(p-1)) print(-1)
        else print(source(p) + " " + source(p))
    }
  def main(args: Array[String]): Unit = {
    f1_io
  }

}

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

    val (n, k)={
      val sp=readLine.split(" ")
      (sp(0).toInt,sp(1).toInt)
    }

    val a=readLine.split(" ").map(_.toInt).sorted

    if(k>n){
      println("-1")
    }else{
      println(a(n-k)+" "+a(n-k))
    }
  }
}

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

    val (n, k)={
      val sp=readLine.split(" ")
      (sp(0).toInt,sp(1).toInt)
    }

    val a=readLine.split(" ").sorted

    if(k>n){
      println("-1")
    }else{
      println(a(n-k)+" "+a(n-k))
    }
  }
}


Vasya has become interested in wrestling. In wrestling wrestlers use techniques for which they are awarded points by judges. The wrestler who gets the most points wins.When the numbers of points of both wrestlers are equal, the wrestler whose sequence of points is lexicographically greater, wins.If the sequences of the awarded points coincide, the wrestler who performed the last technique wins. Your task is to determine which wrestler won.

The first line contains number n — the number of techniques that the wrestlers have used (1 ≤ n ≤ 2·105).  The following n lines contain integer numbers ai (|ai| ≤ 109, ai ≠ 0). If ai is positive, that means that the first wrestler performed the technique that was awarded with ai points. And if ai is negative, that means that the second wrestler performed the technique that was awarded with ( - ai) points. The techniques are given in chronological order.

If the first wrestler wins, print string "first", otherwise print "second"

NoteSequence x  =  x1x2... x|x| is lexicographically larger than sequence y  =  y1y2... y|y|, if either |x|  >  |y| and x1  =  y1,  x2  =  y2, ... ,  x|y|  =  y|y|, or there is such number r (r  <  |x|, r  <  |y|), that x1  =  y1,  x2  =  y2,  ... ,  xr  =  yr and xr  +  1  >  yr  +  1.We use notation |a| to denote length of sequence a.

object Solution extends App {

  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = Range(0, n).map(_ => in.next().toLong)
  val first = data.filter(_ > 0)
  val second = data.filter(_ < 0).map(-_)
  val sum1 = first.sum
  val sum2 = second.sum
  if (sum1 != sum2)
    if (sum1 > sum2)
      println("first")
    else
      println("second")
  else if (first.size != second.size || first.zip(second).exists(t => t._1 != t._2)) {
    val t = first.zip(second).find(t => t._1 != t._2)
    if (t.isEmpty)
      if (first.size > second.size)
        println("first")
      else
        println("second")
    else {
      if (t.get._1 > t.get._2)
        println("first")
      else
        println("second")
    }
  }
  else if (data.last > 0)
    println("first")
  else
    println("second")
}

import java.io.InputStream
import java.io.PrintStream
import java.util.Scanner

object B_VasyaAndWrestling {

  def main(args: Array[String]) {
    parseSolveAndPrint(System.in, System.out)
  }

  def parseSolveAndPrint(in: InputStream, out: PrintStream): Unit = {
    val scanner = new Scanner(in)
    val n = scanner.nextInt()

    val input = for (i <- 1 to n) yield {
      scanner.nextLong()
    }

    val firstPoints = input.filter(_ > 0)
    val secondPoints = input.filter(_ < 0).map(-_)

    if (firstPoints.sum != secondPoints.sum) {
      out.println(if (firstPoints.sum > secondPoints.sum) "first" else "second")
    } else {
      val firstNotNull = firstPoints.zip(secondPoints).map(x => x._1 - x._2).find(_ != 0)
      firstNotNull match {
        case Some(x) =>
          out.println(if (x > 0) "first" else "second")
        case None =>
          out.println(if (input.last > 0) "first" else "second")
      }
    }
  }
}

object Main extends App {

  def lexOrder(first: List[Long], second: List[Long]): Int = {
    (first, second) match {
      case (Nil, Nil) => 0
      case (Nil, _) => -1
      case (_, Nil) => 1
      case (h1 :: tail1, h2 :: tail2) =>
        if(h1 == h2) lexOrder(tail1, tail2)
        else if (h1 < h2) -1
        else 1
    }
  }

  val n = readLine.toLong
  val start = (List[Long](), List[Long](), 0L, 0L)
  val (first, second, lastFirst, lastSecond) = (1L to n).foldLeft(start)({
    case ((f, s, lf, ls), index) =>
       val next = readLine.toLong
       if(next > 0) {
         (next :: f, s, index+1, ls)
       } else {
         (f, Math.abs(next) :: s, lf, index+1)
       }
  })

  val fs = first.sum
  val ss = second.sum

  if(fs < ss) {
    println("second")
  } else if (fs > ss) {
    println("first")
  } else {
     val fr = first.reverse
     val sr = second.reverse
     val lex = lexOrder(fr, sr)
     if(lex > 0) {
       println("first")
     } else if (lex < 0) {
       println("second")
     } else {
       if(lastFirst > lastSecond) println("first") else println("second")
     }
  }

}

object Solution extends App {

  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = Range(0, n).map(_ => in.next().toLong)
  val first = data.filter(_ > 0)
  val second = data.filter(_ < 0).map(-_)
  val sum1 = first.sum
  val sum2 = second.sum
  if (sum1 != sum2)
    if (sum1 > sum2)
      println("first")
    else
      println("second")
  else if (first.head != second.head)
    if (first.head > second.head)
      println("first")
    else
      println("second")
  else if (data.last > 0)
    println("first")
  else
    println("second")
}

object Solution extends App {

  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = Range(0, n).map(_ => in.next().toInt)
  val first = data.filter(_ > 0)
  val second = data.filter(_ < 0).map(-_)
  val sum1 = first.sum
  val sum2 = second.sum
  val str = first.mkString
  val str2 = second.mkString
  if (sum1 != sum2)
    if (sum1 > sum2)
      println("first")
    else
      println("second")
  else if (str != str)
    if (str > str2)
      println("first")
    else
      println("second")
  else if (data.last > 0)
    println("first")
  else
    println("second")
}

object Solution extends App {

  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = Range(0, n).map(_ => in.next().toLong)
  val first = data.filter(_ > 0)
  val second = data.filter(_ < 0).map(-_)
  val sum1 = first.sum
  val sum2 = second.sum
  val str = first.mkString
  val str2 = second.mkString
  if (sum1 != sum2)
    if (sum1 > sum2)
      println("first")
    else
      println("second")
  else if (str != str2)
    if (str > str2)
      println("first")
    else
      println("second")
  else if (data.last > 0)
    println("first")
  else
    println("second")
}

object Solution extends App {

  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = Range(0, n).map(_ => in.next().toInt)
  val first = data.filter(_ > 0)
  val second = data.filter(_ < 0)
  val sum1 = first.sum
  val sum2 = second.sum
  val str = first.mkString
  val str2 = second.map(-_).mkString
  if (sum1 != sum2)
    if (sum1 > sum2)
      println("first")
    else
      println("second")
  else if (str != str)
    if (str > str2)
      println("first")
    else
      println("second")
  else if (data.last > 0)
    println("first")
  else
    println("second")
}

object Solution extends App {

  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = Range(0, n).map(_ => in.next().toInt)
  val first = data.filter(_ > 0)
  val second = data.filter(_ < 0).map(-_)
  val sum1 = first.sum
  val sum2 = second.sum
  val str = first.mkString
  val str2 = second.mkString
  if (sum1 != sum2)
    if (sum1 > sum2)
      println("first")
    else
      println("second")
  else if (str != str2)
    if (str > str2)
      println("first")
    else
      println("second")
  else if (data.last > 0)
    println("first")
  else
    println("second")
}

import java.io.InputStream
import java.io.PrintStream
import java.util.Scanner

object B_VasyaAndWrestling {

  def main(args: Array[String]) {
    parseSolveAndPrint(System.in, System.out)
  }

  def parseSolveAndPrint(in: InputStream, out: PrintStream): Unit = {
    val scanner = new Scanner(in)
    val n = scanner.nextInt()

    val input = for (i <- 1 to n) yield {
      scanner.nextInt()
    }

    val firstPoints = input.filter(_ > 0)
    val secondPoints = input.filter(_ < 0).map(-_)

    if (firstPoints.sum != secondPoints.sum) {
      out.println(if (firstPoints.sum > secondPoints.sum) "first" else "second")
    } else {
      val firstNotNull = firstPoints.zip(secondPoints).map(x => x._1 - x._2).find(_ != 0)
      firstNotNull match {
        case Some(x) =>
          out.println(if (x > 0) "first" else "second")
        case None =>
          out.println(if (input.last > 0) "first" else "second")
      }
    }
  }
}

object Main extends App {

  def lexOrder(first: List[Int], second: List[Int]): Int = {
    (first, second) match {
      case (Nil, Nil) => 0
      case (Nil, _) => -1
      case (_, Nil) => 1
      case (h1 :: tail1, h2 :: tail2) =>
        if(h1 == h2) lexOrder(tail1, tail2)
        else if (h1 < h2) -1
        else 1
    }
  }

  val n = readLine.toInt
  val start = (List[Int](), List[Int](), 0, 0)
  val (first, second, lastFirst, lastSecond) = (1 to n).foldLeft(start)({
    case ((f, s, lf, ls), index) =>
       val next = readLine.toInt
       if(next > 0) {
         (next :: f, s, index+1, ls)
       } else {
         (f, Math.abs(next) :: s, lf, index+1)
       }
  })

  val fs = first.sum
  val ss = second.sum

  if(fs < ss) {
    println("second")
  } else if (fs > ss) {
    println("first")
  } else {
     val fr = first.reverse
     val sr = second.reverse
     val lex = lexOrder(fr, sr)
     if(lex > 0) {
       println("first")
     } else if (lex < 0) {
       println("second")
     } else {
       if(lastFirst > lastSecond) println("first") else println("second")
     }
  }

}

object Main extends App {

  def lexOrder(first: List[Int], second: List[Int]): Int = {
    20
  }

  val n = readLine.toInt
  val start = (List[Int](), List[Int](), 0, 0)
  val (first, second, lastFirst, lastSecond) = (1 to n).foldLeft(start)({
    case ((f, s, lf, ls), index) =>
       val next = readLine.toInt
       if(next > 0) {
         (next :: f, s, index+1, ls)
       } else {
         (f, next :: s, lf, index+1)
       }
  })

  val fs = first.sum
  val ss = second.sum
  if(fs < ss) {
    println("second")
  } else if (fs > ss) {
    println("first")
  } else {
     val fr = first.reverse
     val sr = second.reverse
     val lex = lexOrder(fr, sr)
     if(lex > 0) {
       println("first")
     } else if (lex < 0) {
       println("second")
     } else {
       if(lastFirst > lastSecond) println("first") else println("second")
     }
  }

}