Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this array at all.Levko thinks that the beauty of the array a directly depends on value c(a), which can be calculated by the formula:  The less value c(a) is, the more beautiful the array is.It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most k array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.Help Levko and calculate what minimum number c(a) he can reach.

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2000). The second line contains space-separated integers a1, a2, ... , an ( - 109 ≤ ai ≤ 109).

A single number — the minimum value of c(a) Levko can get.

NoteIn the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.In the third sample he can get array: 1, 2, 3, 4, 5, 6.

import scala.collection._

object B extends App {

  def tokenizeLine = new java.util.StringTokenizer(readLine)
  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 Array(n, k) = readInts(2)
  val as = readLongs(n)

  def can(c: Long): Boolean = {
    val dp = Array.tabulate(n)(identity)
    for (i <- 1 until n) {
      for (
        j <- 0 until i;
        if math.abs(as(i) - as(j)) <= (i - j) * c
      ) dp(i) = math.min(dp(i), dp(j) + i - j - 1)
      if (dp(i) + n - i - 1 <= k) return true
    }
    false
  }

  @annotation.tailrec
  def binSearch(low: Long, hi: Long): Long = {
    if (hi < low) low
    else {
      val mid = (low + hi) >>> 1
      if (can(mid)) binSearch(low, mid - 1)
      else binSearch(mid + 1, hi)
    }
  }

  if (n == 1) println(0)
  else println(binSearch(0, 2000000001))
}

import scala.collection._

object B extends App {

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

  val Array(n, k) = readInts(2)
  val as = readInts(n)

  def can(c: Int): Boolean = {
    val dp = Array.tabulate(n)(identity)
    for (i <- 1 until n) {
      for (
        j <- 0 until i;
        if math.abs(as(i) - as(j)) <= (i - j) * c
      ) dp(i) = math.min(dp(i), dp(j) + i - j - 1)
      if (dp(i) + n - i - 1 <= k) return true
    }
    false
  }

    @annotation.tailrec
    def binSearch(low: Int, hi: Int): Int = {
      if (hi < low) low
      else {
        val mid = (low + hi) >>> 1
        if (can(mid)) binSearch(low, mid - 1)
        else binSearch(mid + 1, hi)
      }
    }

  if (n == 1) println(0)
  else println(binSearch(0, 2000000001))
}


Salem gave you $$$n$$$ sticks with integer positive lengths $$$a_1, a_2, \ldots, a_n$$$.For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from $$$a$$$ to $$$b$$$ is $$$|a - b|$$$, where $$$|x|$$$ means the absolute value of $$$x$$$.A stick length $$$a_i$$$ is called almost good for some integer $$$t$$$ if $$$|a_i - t| \le 1$$$.Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer $$$t$$$ and the total cost of changing is minimum possible. The value of $$$t$$$ is not fixed in advance and you can choose it as any positive integer. As an answer, print the value of $$$t$$$ and the minimum cost. If there are multiple optimal choices for $$$t$$$, print any of them.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of sticks. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 100$$$) — the lengths of the sticks.

Print the value of $$$t$$$ and the minimum possible cost. If there are multiple optimal choices for $$$t$$$, print any of them.

NoteIn the first example, we can change $$$1$$$ into $$$2$$$ and $$$10$$$ into $$$4$$$ with cost $$$|1 - 2| + |10 - 4| = 1 + 6 = 7$$$ and the resulting lengths $$$[2, 4, 4]$$$ are almost good for $$$t = 3$$$.In the second example, the sticks lengths are already almost good for $$$t = 2$$$, so we don't have to do anything.

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)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    var ans = 1e9.toInt + 10
    var ansT = -1
    REP(100, 1) { t =>
      var cost = 0
      REP(N) { i =>
        if (abs(A(i) - t) > 1) {
          cost += abs(A(i) - t) - 1
        }
      }
      if (cost < ans) {
        ans = cost
        ansT = t
      }
    }
    out.println(s"$ansT $ans")
  }

  def debug(as: Array[Boolean]): Unit = {
    System.err.println(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(s: String): Unit = {
    System.err.println(s)
  }

  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
  }
}

import scala.io.StdIn

object SalemsGame {

  def step(l: List[Int], sum: Long, n: Int): (Int, Long) =
    if (absSum(l) >= sum) (n - 1, sum)
    else step(l.map(_ - 1), absSum(l), n + 1)

  def absSum(l: List[Int]): Long = l.toStream
    .filter(Math.abs(_) > 1)
    .map(Math.abs)
    .map(_ - 1)
    .sum

  def main(args: Array[String]): Unit = {
    val skip = StdIn.readLine()
    val res = StdIn.readLine().split(" ").map(_.toInt).toList
    val (a, b) = step(res.map(_ - 1), Long.MaxValue, 1)
    println(s"$a $b")
  }
}

import scala.io.StdIn

object SalemsGame {

  def step(l: List[Int], sum: Long, n: Int): (Int, Long) =
    if (absSum(l) >= sum) (n - 1, sum)
    else step(l.map(_ - 1), absSum(l), n + 1)

  def absSum(l: List[Int]): Long = l.toStream
    .filter(Math.abs(_) > 1)
    .map(Math.abs)
    .map(_ - 1)
    .sum

  def main(args: Array[String]): Unit = {
    val skip = StdIn.readLine()
    val res = StdIn.readLine().split(" ").map(_.toInt).toList
    val (a, b) = step(res, Long.MaxValue, 1)
    println(s"$a $b")
  }
}

import scala.io.StdIn

object SalemsGame {

  def step(l: List[Int], sum: Long, n: Int): (Int, Long) =
    if (absSum(l) >= sum) (n - 1, sum)
    else step(l.map(_ - 1), absSum(l), n + 1)

  def absSum(l: List[Int]): Long = l.toStream
    .filter(Math.abs(_) > 1)
    .map(Math.abs)
    .map(_ - 1)
    .sum

  def main(args: Array[String]): Unit = {
    val skip = StdIn.readLine()
    val res = StdIn.readLine().split(" ").map(_.toInt).toList
    val (a, b) = step(res, Long.MaxValue, 0)
    println(s"$a $b")
  }
}


Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has an array consisting of n numbers. He wants to perform m operations of two types:   add l r d — add an integer d to all elements whose indexes belong to the interval from l to r, inclusive (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ 104);  count l r — find and print on the screen how many lucky numbers there are among elements with indexes that belong to the interval from l to r inclusive (1 ≤ l ≤ r ≤ n). Each lucky number should be counted as many times as it appears in the interval. Petya has a list of all operations. The operations are such that after all additions the array won't have numbers that would exceed 104. Help Petya write a program that would perform these operations.

The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of numbers in the array and the number of operations correspondingly. The second line contains n positive integers, none of which exceeds 104 — those are the array numbers. Next m lines contain operations, one per line. They correspond to the description given in the statement. It is guaranteed that after all operations are fulfilled each number in the array will not exceed 104.

For each operation of the second type print the single number on the single line — the number of lucky numbers in the corresponding interval.

3 6
2 3 4
count 1 3
count 1 2
add 1 3 2
count 1 3
add 2 3 3
count 1 3

4 5
4 4 4 4
count 1 4
add 1 4 3
count 1 4
add 2 3 40
count 1 4

NoteIn the first sample after the first addition the array will look in the following manner:4 5 6After the second addition:4 8 9The second sample after the first addition:7 7 7 7After the second addition:7 47 47 7

import java.io._
import java.util.StringTokenizer

import scala.collection.immutable
import scala.collection.Searching._
import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new C091E(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 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
  }
}

class C091E(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import fScanner._
  import Main._

  def solve(): Unit = {
    val n = ni()
    val m = ni()

    val a = na(n)

    val luckys: immutable.IndexedSeq[Int] = (1 to 10000).filter(isLucky)
//    out.println(isLucky(4))
    val t = new SegmentTree(n, a, luckys)
    t.build(1)
    REP(m) { _ =>
     ns() match {
       case "count" =>
         t.print()
         out.println(t.get(1, l = ni() - 1, r = ni() - 1))
       case "add" =>
         t.update(1, l = ni() -1, r= ni() -1, nVal = ni())
     }
    }
  }

  def isLucky(u: Int): Boolean = {
    var x = u
    while (x > 0) {
      val y = x % 10
      if(y != 4 && y != 7) return false
      x = x / 10
    }
    true
  }

  private class SegmentTree(n: Int, a: Array[Int], luckys: IndexedSeq[Int]) {
    val tree: Array[Int] = Array.ofDim[Int](4 * n)
    val min: Array[Int] = Array.ofDim[Int](4 * n)

    val laz: Array[Int] = Array.fill[Int](4 * n)(0)

    def print() = {
//      println(laz.length)
    }

    def combine(u: Int, c1: Int, c2: Int): Unit = {
      tree(u) = tree(c1) + tree(c2)
      min(u) = Math.min(min(c1), min(c2))
    }

    def push(u: Int) = {
      laz(u << 1) += laz(u)
      min(u << 1) -= laz(u)
      laz(u << 1 | 1) += laz(u)
      min(u << 1 | 1) -= laz(u)
      laz(u) = 0
    }

    def build(v: Int, tl: Int = 0, tr: Int = n-1): Unit = {
      if(tl == tr) {
        val iP = luckys.search(a(tl))
        if(iP.insertionPoint == luckys.length) {
          tree(v) = 0
          min(v) = Int.MaxValue
        } else {
          min(v) = luckys(iP.insertionPoint) - a(tl)
          tree(v) = if(min(v) == 0) 1 else 0
        }
      } else {
        val tm = tl + (tr - tl) / 2
        build(v << 1, tl, tm)
        build(v << 1 | 1, tm+1, tr)
        combine(v, v << 1, v << 1 | 1)
      }
    }

    def update(v: Int, tl: Int = 0, tr: Int = n-1, l: Int, r: Int, nVal: Int): Unit = {
      if(l > r) {}
      else if(l == tl && r == tr && min(v) > nVal) {
        min(v) -= nVal
        laz(v) += nVal
      } else if(tl == tr) {
        a(tl) += laz(v) + nVal
        laz(v) = 0
        val iP = luckys.search(a(tl))
        if(iP.insertionPoint == luckys.length) {
          tree(v) = 0
          min(v) = Int.MaxValue
        } else {
          min(v) = luckys(iP.insertionPoint) - a(tl)
          tree(v) = if(min(v) == 0) 1 else 0
        }
      } else {
        push(v)
        val tm = tl + (tr - tl) / 2
        update(v << 1, tl, tm, l, Math.min(r, tm), nVal)
        update(v << 1 | 1, tm + 1, tr, Math.max(l, tm + 1), r, nVal)
        combine(v, v << 1, v << 1 | 1)
      }
    }

    def get(v: Int, tl: Int = 0, tr: Int = n-1, l: Int, r: Int): Int = {
      if(l > r) 0
      else if(l == tl && r == tr) tree(v)
      else {
        val tm = tl + (tr - tl) / 2
        get(v << 1, tl, tm, l, Math.min(r, tm)) + get(v << 1 | 1, tm + 1, tr, Math.max(l, tm + 1), r)
      }
    }
  }
}

import java.io._
import java.util.StringTokenizer

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new C091E(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 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
  }
}

class C091E(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val n = ni()
    val m = ni()

    val a = na(n)
    val t = new SegmentTree(n, a)
    t.build(1, 0, n-1)
    REP(m) { _ =>
      ns() match {
        case "count" =>
          out.println(t.get(1, 0, n-1, ni()-1, ni()-1).count)
          t.print()
        case "add" =>
          t.update(1, 0, n-1, ni()-1, ni()-1, ni())
          t.print()
      }
    }
  }

  case class Node(count: Int, add: Int, marked: Boolean)

  private class SegmentTree(n: Int, a: Array[Int]) {
    private val tree: Array[Node] = Array.ofDim[Node](4 * n)

    private def isLucky(u: Int): Int = {
      var v = u
      var countI = 0
      var countT = 0
      while (v > 0) {
        val g = v % 10
        if(g == 4 || g == 7) countI += 1
        v = v / 10
        countT += 1;
      }
      if(countI == countT) 1 else 0
    }

    def print(): Unit = {
//      a.foreach(out.println)
    }

    private def combine(x: Node, y: Node): Node = {
      Node(x.count + y.count, 0, x.marked || y.marked)
    }

    def build(v: Int, tl: Int, tr: Int): Unit = {
      if(tl == tr) {
        tree(v) = Node(isLucky(a(tl)), 0, false)
      } else {
        val tm = tl + (tr - tl) / 2
        build(v << 1, tl ,tm)
        build(v << 1 | 1, tm + 1, tr)
        tree(v) = combine(tree(v << 1), tree(v << 1 | 1))
      }
    }

    def push(v: Int): Unit = {
      if(tree(v).marked) {
        val x = tree(v)
        val y = tree(v << 1)
        val z = tree(v << 1 | 1)

        tree(v << 1) = Node(y.count, y.add + x.add, true)
        tree(v << 1 | 1) = Node(z.count, z.add + x.add, true)
        tree(v) = Node(x.count, 0, true)
      }
    }

    def update(v: Int, tl: Int, tr: Int, l: Int, r: Int, nAdd: Int): Unit = {
      if(l > r) {}
      else if(tl == tr && tree(v).marked) {
        a(tl) += tree(v).add
        tree(v) = Node(isLucky(a(tl)), 0, false)
      }
      else if(l == tl && r == tr) {
        val x = tree(v)
        tree(v) = Node(x.count, x.add + nAdd, marked = true)
      } else {
        push(v)
        val tm = tl + (tr - tl) / 2
        update(v << 1, tl, tm, l, Math.min(tm, r), nAdd)
        update(v << 1 | 1, tm + 1, tr, Math.max(tm + 1, l), r, nAdd)
        tree(v) = combine(tree(v << 1), tree(v << 1 | 1))
      }
    }

    def get(v: Int, tl: Int, tr: Int, l: Int, r: Int): Node = {
      if(l > r) Node(0, 0, false)
      else if(l == tl && r == tr && !tree(v).marked) {
        tree(v)
      } else if(tl == tr && tree(v).marked) {
        a(tl) += tree(v).add
        tree(v) = Node(isLucky(a(tl)), 0, false)
        tree(v)
      } else {
        push(v)
        val tm = tl + (tr - tl) / 2
        combine(get(v << 1, tl, tm, l, Math.min(tm, r)),
          get(v << 1 | 1, tm + 1, tr, Math.max(tm + 1, l), r))
      }
    }
  }
}

import java.io._
import java.util.StringTokenizer

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new C091E(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 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
  }
}

class C091E(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val n = ni()
    val m = ni()

    val a = na(n)
    val t = new SegmentTree(n, a)
    t.build(1, 0, n-1)
    REP(m) { _ =>
      ns() match {
        case "count" =>
          out.println(t.get(1, 0, n-1, ni()-1, ni()-1).count)
        case "add" =>
          t.update(1, 0, n-1, ni()-1, ni()-1, ni())
      }
    }
  }

  case class Node(count: Int, add: Int, marked: Boolean)

  private class SegmentTree(n: Int, a: Array[Int]) {
    private val tree: Array[Node] = Array.ofDim[Node](4 * n)

    private def isLucky(u: Int): Int = {
      var v = u
      var countI = 0
      var countT = 0
      while (v > 0) {
        val g = v % 10
        if(g == 4 || g == 7) countI += 1
        v = v / 10
        countT += 1;
      }
      if(countI == countT) 1 else 0
    }

    private def combine(x: Node, y: Node): Node = {
      Node(x.count + y.count, 0, x.marked || y.marked)
    }

    def build(v: Int, tl: Int, tr: Int): Unit = {
      if(tl == tr) {
        tree(v) = Node(isLucky(a(tl)), 0, false)
      } else {
        val tm = tl + (tr - tl) / 2
        build(v << 1, tl ,tm)
        build(v << 1 | 1, tm + 1, tr)
        tree(v) = combine(tree(v << 1), tree(v << 1 | 1))
      }
    }

    def push(v: Int): Unit = {
      if(tree(v).marked) {
        val x = tree(v)
        val y = tree(v << 1)
        val z = tree(v << 1 | 1)

        tree(v << 1) = Node(y.count, y.add + x.add, true)
        tree(v << 1 | 1) = Node(z.count, z.add + x.add, true)
        tree(v) = Node(x.count, 0, false)
      }
    }

    def update(v: Int, tl: Int, tr: Int, l: Int, r: Int, nAdd: Int): Unit = {
      if(l > r) {}
      else if(tl == tr && tree(v).marked) {
        a(tl) += tree(v).add
        tree(v) = Node(isLucky(a(tl)), 0, false)
      }
      else if(l == tl && r == tr) {
        val x = tree(v)
        tree(v) = Node(x.count, x.add + nAdd, marked = true)
      } else {
        push(v)
        val tm = tl + (tr - tl) / 2
        update(v << 1, tl, tm, l, Math.min(tm, r), nAdd)
        update(v << 1 | 1, tm + 1, tr, Math.max(tm + 1, l), r, nAdd)
        tree(v) = combine(tree(v << 1), tree(v << 1 | 1))
      }
    }

    def get(v: Int, tl: Int, tr: Int, l: Int, r: Int): Node = {
      if(l > r) Node(0, 0, false)
      else if(l == tl && r == tr && !tree(v).marked) {
        tree(v)
      } else if(tl == tr && tree(v).marked) {
        a(tl) += tree(v).add
        tree(v) = Node(isLucky(a(tl)), 0, false)
        tree(v)
      } else {
        push(v)
        val tm = tl + (tr - tl) / 2
        combine(get(v << 1, tl, tm, l, Math.min(tm, r)),
          get(v << 1 | 1, tm + 1, tr, Math.max(tm + 1, l), r))
      }
    }
  }
}

import java.io._
import java.util.StringTokenizer

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new C091E(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 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
  }
}

class C091E(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val n = ni()
    val m = ni()

    val a = na(n)
    val t = new SegmentTree(n, a)
    t.build(1, 0, n-1)
    REP(m) { _ =>
      ns() match {
        case "count" =>
          out.println(t.get(1, 0, n-1, ni()-1, ni()-1).count)
          t.print()
        case "add" =>
          t.update(1, 0, n-1, ni()-1, ni()-1, ni())
          t.print()
      }
    }
  }

  case class Node(count: Int, add: Int, marked: Boolean)

  private class SegmentTree(n: Int, a: Array[Int]) {
    private val tree: Array[Node] = Array.ofDim[Node](4 * n)

    private def isLucky(u: Int): Int = {
      var v = u
      var countI = 0
      var countT = 0
      while (v > 0) {
        val g = v % 10
        if(g == 4 || g == 7) countI += 1
        v = v / 10
        countT += 1;
      }
      if(countI == countT) 1 else 0
    }

    def print(): Unit = {
//      a.foreach(out.println)
    }

    private def combine(x: Node, y: Node): Node = {
      Node(x.count + y.count, 0, x.marked || y.marked)
    }

    def build(v: Int, tl: Int, tr: Int): Unit = {
      if(tl == tr) {
        tree(v) = Node(isLucky(a(tl)), 0, false)
      } else {
        val tm = tl + (tr - tl) / 2
        build(v << 1, tl ,tm)
        build(v << 1 | 1, tm + 1, tr)
        tree(v) = combine(tree(v << 1), tree(v << 1 | 1))
      }
    }

    def push(v: Int): Unit = {
      if(tree(v).marked) {
        val x = tree(v)
        val y = tree(v << 1)
        val z = tree(v << 1 | 1)

        tree(v << 1) = Node(y.count, y.add + x.add, true)
        tree(v << 1 | 1) = Node(z.count, z.add + x.add, true)
        tree(v) = Node(x.count, 0, false)
      }
    }

    def update(v: Int, tl: Int, tr: Int, l: Int, r: Int, nAdd: Int): Unit = {
      if(l > r) {}
      else if(tl == tr && tree(v).marked) {
        a(tl) += tree(v).add
        tree(v) = Node(isLucky(a(tl)), 0, false)
      }
      else if(l == tl && r == tr) {
        val x = tree(v)
        tree(v) = Node(x.count, x.add + nAdd, marked = true)
      } else {
        push(v)
        val tm = tl + (tr - tl) / 2
        update(v << 1, tl, tm, l, Math.min(tm, r), nAdd)
        update(v << 1 | 1, tm + 1, tr, Math.max(tm + 1, l), r, nAdd)
        tree(v) = combine(tree(v << 1), tree(v << 1 | 1))
      }
    }

    def get(v: Int, tl: Int, tr: Int, l: Int, r: Int): Node = {
      if(l > r) Node(0, 0, false)
      else if(l == tl && r == tr && !tree(v).marked) {
        tree(v)
      } else if(tl == tr && tree(v).marked) {
        a(tl) += tree(v).add
        tree(v) = Node(isLucky(a(tl)), 0, false)
        tree(v)
      } else {
        push(v)
        val tm = tl + (tr - tl) / 2
        val x = combine(get(v << 1, tl, tm, l, Math.min(tm, r)),
          get(v << 1 | 1, tm + 1, tr, Math.max(tm + 1, l), r))
        tree(v) = combine(tree(v << 1), tree(v << 1 | 1))
        x
      }
    }
  }
}


You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.

The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.

For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.

Note12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.8 = 4 + 4, 6 can't be split into several composite summands.1, 2, 3 are less than any composite number, so they do not have valid splittings.

import scala.io.StdIn
import scala.math.pow
import scala.collection.mutable._

object Main {
  implicit def bool2int(b: Boolean): Int = { if(b) 1 else 0 }

  def main(args: Array[String]): Unit = {
    val memoized = memoizedSolution
    (1 to readInt).foreach { _ => println(memoized(readInt)) }
  }

  def memoizedSolution: Int => Int = {
    def querySolution(i: Int) = {
      i match {
        case i if i % 4 == 0 => i / 4
        case i if i >= 6  && (i - 6)  % 4 == 0 => (i - 6) / 4 + 1
        case i if i >= 9  && (i - 9)  % 4 == 0 => (i - 9) / 4 + 1
        case i if i >= 15 && (i - 15) % 4 == 0 => (i - 15) / 4 + 2
        case _ => -1
      }
    }

    val map = Map[Int, Int]()

    i => map.getOrElse(i, {
      val solution = querySolution(i)
      map.put(i, solution)
      solution
    })
  }
}

import scala.io.StdIn
import scala.math.pow
import scala.collection.mutable._

object Main {
  implicit def bool2int(b: Boolean): Int = { if(b) 1 else 0 }

  def main(args: Array[String]): Unit = {
    (1 to readInt).foreach { _ => println(querySolution(readInt)) }
  }

  def querySolution(i: Int): Int = {
    i match {
      case i if i % 4 == 0 => i / 4
      case i if i >= 6  && (i - 6)  % 4 == 0 => (i - 6) / 4 + 1
      case i if i >= 9  && (i - 9)  % 4 == 0 => (i - 9) / 4 + 1
      case i if i >= 15 && (i - 15) % 4 == 0 => (i - 15) / 4 + 2
      case _ => -1
    }
  }
}

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  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)) }

  val Array(n) = readInts(1)
  val qs = Array.fill(n){ readLine.toInt }

  val res = for {
    q <- qs
    r1 = if (q >= 9 && (q - 9) % 4 == 0) 1 + (q - 9) / 4 else -1
    r2 = if (q >= 6 && (q - 6) % 4 == 0) 1 + (q - 6) / 4 else -1
    r3 = if (q >= 15 && (q - 15) % 4 == 0) 2 + (q - 15) / 4 else -1
    r4 = if (q % 4 == 0) q / 4 else -1
  } yield Seq(r1, r2, r3, r4).max

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

  println(res.mkString("\n"))

  Console.flush
}


Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$:   Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$1 \leq a_i \leq 1000$$$) — the elements of the array $$$a$$$.

For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any.

import scala.io.StdIn._

object Main {
  def main(args: Array[String]): Unit = {
    val t = readInt
    (0 until t).foreach { i =>
      val n = readInt
      val arrWithIndex = readLine.split(" ").map(_.toInt).zipWithIndex.toList
      val even = arrWithIndex.filter(x => (x._1 % 2) == 0)
      val evenG = even.grouped(2).toList
      val odd = arrWithIndex.filter(x => (x._1 % 2) != 0)
      val oddG = odd.grouped(2).toList
      val evenPairs = (if ((even.size % 2) != 0) evenG.dropRight(1) else evenG) map { case List((x1, x2), (y1, y2)) => (x2, y2) }
      val oddPairs = (if ((odd.size % 2) != 0) oddG.dropRight(1) else oddG) map { case List((x1, x2), (y1, y2)) => (x2, y2) }
      val result = evenPairs ++ oddPairs
      val finalResult = if (result.size > n - 1) result.dropRight(1) else result
      finalResult foreach { k => println(s"${k._1 + 1} ${k._2 + 1}") }
    }
  }
}

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

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

    for (_ <- 1 to nextInt) {
      val n = nextInt
      val as = nextInts(2 * n).zipWithIndex

      val (even, odd) = as.partition(_._1 % 2 == 0)

      val evenQ = mutable.Queue(even: _*)
      val oddQ = mutable.Queue(odd: _*)

      val res = ArrayBuffer.empty[String]
      while (oddQ.size > 2) {
        val a, b = oddQ.dequeue()
        res += s"${a._2 + 1} ${b._2 + 1}"
      }

      val lim = if (odd.isEmpty) 2 else 1
      while (evenQ.size > lim) {
        val a, b = evenQ.dequeue()
        res += s"${a._2 + 1} ${b._2 + 1}"
      }

      out.println(res.mkString("\n"))

      out.flush()
    }
  }

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

object B extends App {
  import java.io._
  import java.nio.file.{Files, Path}
  import java.util.StringTokenizer
  import scala.io.Source
  import scala.io.Codec
  import scala.collection.mutable
  import scala.collection.mutable.ArrayBuffer

  /* 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()

      var oddIndex  = mutable.ArrayBuffer[Int]()
      var evenIndex = mutable.ArrayBuffer[Int]()

      1.to(m * 2)
        .foreach(i => {
          val d = in.nextInt()
          if (d % 2 == 0) {
            evenIndex :+= i
          } else {
            oddIndex :+= i
          }
        })

      if (oddIndex.size % 2 == 1 && evenIndex.size % 2 == 1) {
        printGrouped(oddIndex.drop(1))
        printGrouped(evenIndex.drop(1))
      } else {
        if (oddIndex.size >= 2) {
          printGrouped(oddIndex.drop(2))
          printGrouped(evenIndex)
        } else {
          printGrouped(evenIndex.drop(2))
          printGrouped(oddIndex)
        }
      }
    }

    def printGrouped(list: ArrayBuffer[Int]) = {
      if (list.nonEmpty) {
        for (x <- list.grouped(2)) {
          println(s"${x(0)} ${x(1)}")
        }
      }
    }
  }

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

  /**
    * Util methods
    */

  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()
  }
}

object B extends App {
  import java.io._
  import java.nio.file.{Files, Path}
  import java.util.StringTokenizer
  import scala.io.Source
  import scala.io.Codec
  import scala.collection.mutable
  import scala.collection.mutable.ArrayBuffer

  /* 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()

      var oddIndex  = mutable.ArrayBuffer[Int]()
      var evenIndex = mutable.ArrayBuffer[Int]()

      1.to(m * 2)
        .foreach(i => {
          val d = in.nextInt()
          if (d % 2 == 0) {
            evenIndex :+= i
          }
          if (d % 2 == 1) {
            oddIndex :+= i
          }
        })

      if (oddIndex.size % 2 == 1 && evenIndex.size % 2 == 1) {
        printGrouped(oddIndex.take(oddIndex.size - 1))
        printGrouped(evenIndex.take(evenIndex.size - 1))
      } else {
        if (oddIndex.size >= 2) {
          printGrouped(oddIndex.take(oddIndex.size - 2))
          printGrouped(evenIndex)
        } else {
          printGrouped(evenIndex.take(oddIndex.size - 2))
          printGrouped(oddIndex)
        }
      }
    }

    def printGrouped(list: ArrayBuffer[Int]) = {
      for (x <- list.grouped(2)) {
        println(s"${x(0)} ${x(1)}")
      }
    }
  }

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

  /**
    * Util methods
    */

  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()
  }
}


Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.

The first line contains integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree. The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.

Print a single number — the maximum number of trees that you can cut down by the given rules.

NoteIn the first sample you can fell the trees like that:   fell the 1-st tree to the left — now it occupies segment [ - 1;1]  fell the 2-nd tree to the right — now it occupies segment [2;3]  leave the 3-rd tree — it occupies point 5  leave the 4-th tree — it occupies point 10  fell the 5-th tree to the right — now it occupies segment [19;20] In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].

object C545 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  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)
    case class Tree(x: Long, h: Long)
    val input = new Array[Tree](n)
    for(i <- 0 until n) {
      val Array(x, h) = readLongs(2)
      input(i) = Tree(x, h)
    }
    var ret = if(input.length > 1) 2 else 1
    for(i <- 1 until n-1) {
      if(input(i).x - input(i).h > input(i-1).x){
        ret +=1
      } else if (input(i).x + input(i).h < input(i+1).x) {
        input(i) = Tree(input(i).x + input(i).h, input(i).h)
        ret += 1
      }
    }
    println(ret)
  }
}

/**
 *
 * Created by ronaflx on 15-4-3.
 */

import java.io._
import java.util.StringTokenizer

import scala.Ordering.Implicits._
import scala.collection.immutable.Queue
import scala.collection.mutable.ArrayBuffer

object Codeforces {
  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

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

  def nextInt: Int = {
    Integer.parseInt(next)
  }

  def nextLong: Long = {
    java.lang.Long.parseLong(next)
  }

  def lowerBound[T: Ordering](array: Array[T], value: T) = {
    var low = 0
    var high = array.length
    var res = array.length
    while (low <= high) {
      val mid = (low + high) / 2
      if (!(array(mid) < value)) {
        res = mid
        high = mid - 1
      } else {
        low = mid + 1
      }
    }
    res
  }

  def upperBound[T: Ordering](array: Array[T], value: T) = {
    var low = 0
    var high = array.length
    var res = array.length
    while (low <= high) {
      val mid = (low + high) / 2
      if (value < array(mid)) {
        res = mid
        high = mid - 1
      } else {
        low = mid + 1
      }
    }
    res
  }

  def solve(): Unit = {
    val n = nextInt
    val position = new Array[Int](n)
    val height = new Array[Int](n)
    val dp = Array.ofDim[Int](n, 2)

    for (i <- 0 until n) {
      position(i) = nextInt
      height(i) = nextInt
    }
    dp(0)(0) = 1
    if (n != 1 && position(0) + height(0) < position(1)) {
      dp(0)(1) = 1
    }
    for (i <- 1 until n) {
      if (position(i - 1) + height(i - 1) + height(i) < position(i)) {
        dp(i)(0) = math.max(dp(i)(0), dp(i - 1)(1) + 1)
      }
      if (position(i - 1) + height(i) < position(i)) {
        dp(i)(0) = math.max(dp(i)(0), dp(i - 1)(0) + 1)
      }
      if (i + 1 >= n ||
        position(i) + height(i) < position(i + 1)) {
        dp(i)(1) = math.max(dp(i - 1)(0), dp(i - 1)(1)) + 1
      }
      dp(i)(0) = math.max(dp(i - 1)(0), dp(i)(0))
      dp(i)(0) = math.max(dp(i - 1)(1), dp(i)(0))
    }

    println(dp(n - 1).max)
  }

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve()
    out.close
  }
}

object _545C extends CodeForcesApp {
  import scala.collection.mutable

  override type Input = IndexedSeq[(Long, Long)]
  override type Output = Int

  override def read(scanner: java.util.Scanner) = {
    import scanner._
    IndexedSeq.fill(nextInt)((nextLong, nextLong))
  }

  override def solve(input: Input) = {
    var count = 0
    var curX = -2e9

    def chop(from: Long, to: Long) = {
      //debug("Chopped", from, to)
      curX = to
      count += 1
    }

    for {
      ((x, h), i) <- input.zipWithIndex
    } {
      if(curX < x - h) {
        chop(x-h, x)
      } else {
        if (i == input.length - 1) {
          chop(x, x+h)
        } else {
          val (nextX, _) = input(i+1)
          if (x+h < nextX) {
            chop(x, x+h)
          } else {
            curX = x
          }
        }
      }
    }

    count
  }

  override def format(result: Output) = super.format(result)
}

/****************************** IGNORE BELOW *********************************/
abstract class CodeForcesApp extends App {
  /********************************[ I/O ]************************************/
  import java.util.Scanner
  val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: Any): Unit = if (!isOnlineJudge) println(x)
  def apply(problem: String): String  = apply(new Scanner(problem))
  def apply(scanner: Scanner): String = format(solve(read(scanner)))
  println(apply(new Scanner(System.in)))
  /********************************[ lib ]************************************/
  import scala.collection.mutable
  case class Memo[I <% K, K, O](f: I => O) extends (I => O) {
    val cache = mutable.Map.empty[K, O]
    override def apply(x: I): O = cache.getOrElseUpdate(x, f(x))
  }
  type m_=>[I, O] = Memo[I, I, O]
  def when[A](condition: Boolean)(f: => A): Option[A] = if (condition) Some(f) else None
  /******************************[ solution ]*********************************/
  type Input
  type Output
  def read(scanner: Scanner): Input
  def solve(input: Input): Output
  def format(result: Output): String = result.toString
  def timeLimitSecond = 1
}

import scala.io.StdIn

object Main {
  case class Tree (x:Int, h:Int) {
    def x_left = x - h;
    def x_centre = x;
    def x_right = x + h;

    var dp_left = 0;
    var dp_center = 0;
    var dp_right = 0;
  }

  def main(args: Array[String]) {
    val trees : Array[Tree] =
      (1 to StdIn.readInt()).map(_ =>
          StdIn.readLine.split(" ").map(Integer.parseInt) match {
              case Array(f, s) => new Tree(f, s)
          }).toArray;

    trees(0).dp_left = 1;

    def handle_trees = (tr1 : Tree, tr2 : Tree) =>
      Seq((tr1.x_centre, tr1.dp_left), (tr1.x_centre, tr1.dp_center), (tr1.x_right, tr1.dp_right)) map {
        case (x, dp) =>
          if(x < tr2.x_left)
            tr2.dp_left = math.max(dp + 1, tr2.dp_left);
          else if(x < tr2.x_centre) {
            tr2.dp_center = math.max(dp, tr2.dp_center);
            tr2.dp_right = math.max(dp + 1, tr2.dp_right);
          }
      }

    for(i <- trees.indices.tail) {
      handle_trees(trees(i-1), trees(i));
    }

    println(math.max(trees.last.dp_left, trees.last.dp_right));
  }
}

import scala.io.StdIn._

object C extends App {
  val n = readInt()
  val Trees = new Array[(Int, Int)](n)

  for (i <- 0 until n) {
    val line = readLine().split(" ").map(_.toInt)
    Trees(i) = (line(0), line(1))
  }

  def solve(): Int = {
    var result = 0
    var lastOccupied = 0
    for (i <- 0 until n) {
      if (i == 0) {
        result += 1
        lastOccupied = Trees(0)._1
      }
      else if (i == n - 1) result += 1
      else {
        val (x, h) = Trees(i)
        if (lastOccupied < x - h) {
//          println(s"left fall $i")
          result += 1
          lastOccupied = x
        } else if (x + h < Trees(i + 1)._1) {
//          println(s"fall $i")
          result += 1
          lastOccupied = x + h
        } else {
          lastOccupied = x
        }

      }
    }
    result
  }

  println(solve())
}

object C{
  def main(args: Array[String]){
    val in=io.Source.stdin.getLines()
    val n=in.next().toInt
    var x= new Array[Int](n)
    var h=new Array[Int](n)
    for(i <- 0 until n){
      val t=in.next().split(" ")
      x(i) = t(0).toInt

      h(i) = t(1).toInt

    }
    var (nl,nr)=(1,1)

    if(n>1){
      nl=1
      nr=if(x(0)+h(0)<x(1)) 1 else 0
    }
    for(i <- 1 until n){
      val nl0=nl
      val nr0=nr
      nl=0
      nr=0
      if(x(i)-h(i)>x(i-1)){
        nl=nl0+1
      }
      else{
        nl=nl0
      }
      if(nr0>nl){
        nl=nr0
      }
      if(x(i)-h(i)>x(i-1)+h(i-1)){
        if(nr0+1>nl){
          nl=nr0+1
        }
      }
      if(i==n-1||x(i)+h(i)<x(i+1)){
        if(nl0>nr0){
          nr=nl0+1
        }
        else{
          nr=nr0+1
        }
      }
    }
    println(nr)
  }
}

object C545 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  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)
    case class Tree(x: Long, h: Long)
    val input = new Array[Tree](n)
    for(i <- 0 until n) {
      val Array(x, h) = readLongs(2)
      input(i) = Tree(x, h)
    }
    var ret = 2
    for(i <- 1 until n-1) {
      if((input(i).x - input(i).h > input(i-1).x) || (input(i).x + input(i).h < input(i+1).x)) {
        ret += 1
      }
    }
    println(ret)
  }
}

object C545 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  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)
    case class Tree(x: Long, h: Long)
    val input = new Array[Tree](n)
    for(i <- 0 until n) {
      val Array(x, h) = readLongs(2)
      input(i) = Tree(x, h)
    }
    var ret = 2
    for(i <- 1 until n-1) {
      if(input(i).x - input(i).h > input(i-1).x){
        ret +=1
      } else if (input(i).x + input(i).h < input(i+1).x) {
        input(i) = Tree(input(i).x + input(i).h, input(i).h)
        ret += 1
      }
    }
    println(ret)
  }
}

/**
 *
 * Created by ronaflx on 15-4-3.
 */

import java.io._
import java.util.StringTokenizer

import scala.Ordering.Implicits._
import scala.collection.immutable.Queue
import scala.collection.mutable.ArrayBuffer

object Codeforces {
  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

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

  def nextInt: Int = {
    Integer.parseInt(next)
  }

  def nextLong: Long = {
    java.lang.Long.parseLong(next)
  }

  def lowerBound[T: Ordering](array: Array[T], value: T) = {
    var low = 0
    var high = array.length
    var res = array.length
    while (low <= high) {
      val mid = (low + high) / 2
      if (!(array(mid) < value)) {
        res = mid
        high = mid - 1
      } else {
        low = mid + 1
      }
    }
    res
  }

  def upperBound[T: Ordering](array: Array[T], value: T) = {
    var low = 0
    var high = array.length
    var res = array.length
    while (low <= high) {
      val mid = (low + high) / 2
      if (value < array(mid)) {
        res = mid
        high = mid - 1
      } else {
        low = mid + 1
      }
    }
    res
  }

  def solve(): Unit = {
    val n = nextInt
    val position = new Array[Int](n)
    val height = new Array[Int](n)
    val dp = Array.ofDim[Int](n, 2)

    for (i <- 0 until n) {
      position(i) = nextInt
      height(i) = nextInt
    }
    dp(0)(0) = 1
    if (position(0) + height(0) < position(1)) {
      dp(0)(1) = 1
    }
    for (i <- 1 until n) {
      if (position(i - 1) + height(i - 1) + height(i) < position(i)) {
        dp(i)(0) = math.max(dp(i)(0), dp(i - 1)(1) + 1)
      }
      if (position(i - 1) + height(i) < position(i)) {
        dp(i)(0) = math.max(dp(i)(0), dp(i - 1)(0) + 1)
      }
      if (i + 1 < n) {
        if (position(i) + height(i) < position(i + 1)) {
          dp(i)(1) = math.max(dp(i - 1)(0), dp(i - 1)(1)) + 1
        }
      } else {
        dp(i)(1) = math.max(dp(i - 1)(0), dp(i - 1)(1)) + 1
      }
      dp(i)(0) = math.max(dp(i - 1)(0), dp(i)(0))
      dp(i)(1) = math.max(dp(i - 1)(1), dp(i)(1))
    }
    println(dp(n - 1).max)
  }

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve()
    out.close
  }
}

import scala.io.StdIn._

object C extends App {
  val n = readInt()
  val Trees = new Array[(Int, Int)](n)

  for (i <- 0 until n) {
    val line = readLine().split(" ").map(_.toInt)
    Trees(i) = (line(0), line(1))
  }

  def solve(): Int = {
    var result = 0
    var lastOccupied = 0
    for (i <- 0 until n) {
      if (i == 0) {
        result += 1
        lastOccupied = Trees(0)._1
      }
      else if (i == n - 1) result += 1
      else {
        val (x, h) = Trees(i)
        if (lastOccupied < x - h) {
          println(s"left fall $i")
          result += 1
          lastOccupied = x
        } else if (x + h < Trees(i + 1)._1) {
          println(s"fall $i")
          result += 1
          lastOccupied = x + h
        } else {
          lastOccupied = x
        }

      }
    }
    result
  }

  println(solve())
}

import scala.io.StdIn._

object C extends App {
  val n = readInt()
  val Trees = new Array[(Int, Int)](n)

  for (i <- 0 until n) {
    val line = readLine().split(" ").map(_.toInt)
    Trees(i) = (line(0), line(1))
  }

  def solve(): Int = {
    var result = 0
    var last = 0
    for (i <- 0 until n) {
      if (i == 0) result += 1
      else if (i == n - 1) result +=1
      else {
        val (x, h) = Trees(i)
        if (last < x - h) {
          result += 1
          last = x
        } else if (x + h < Trees(i + 1)._1) {
          result += 1
          last = x + h
        }

      }
    }
    result
  }

  println(solve())
}


Because of budget cuts one IT company established new non-financial reward system instead of bonuses.Two kinds of actions are rewarded: fixing critical bugs and suggesting new interesting features. A man who fixed a critical bug gets "I fixed a critical bug" pennant on his table. A man who suggested a new interesting feature gets "I suggested a new feature" pennant on his table.Because of the limited budget of the new reward system only 5 "I fixed a critical bug" pennants and 3 "I suggested a new feature" pennants were bought.In order to use these pennants for a long time they were made challenge ones. When a man fixes a new critical bug one of the earlier awarded "I fixed a critical bug" pennants is passed on to his table. When a man suggests a new interesting feature one of the earlier awarded "I suggested a new feature" pennants is passed on to his table.One man can have several pennants of one type and of course he can have pennants of both types on his table. There are n tables in the IT company. Find the number of ways to place the pennants on these tables given that each pennant is situated on one of the tables and each table is big enough to contain any number of pennants.

The only line of the input contains one integer n (1 ≤ n ≤ 500) — the number of tables in the IT company.

Output one integer — the amount of ways to place the pennants on n tables.

import java.util.Scanner

object Main {

  def sqr(x: Long) = x * x

  def powmod(x: Long, n: Long, p: Long): Long = {
    if (n == 1) x
    else if (n % 2 == 1) (powmod(x, n - 1, p) * x) % p
    else sqr(powmod(x, n / 2, p)) % p
  }

  def C(n: Long, k: Long) = {
    var res = BigInt(1)
    for (i <- n - k + 1 to n) {
      res = res * i
    }
    for (i <- 1 to k.toInt) {
      res = res / i
    }
    res
  }

  def main(args: Array[String]): Unit = {
    val in = new Scanner(System.in)

    val n = in.nextInt()

    println(C(n - 1 + 3, 3) * C(n - 1 + 5, 5))
  }
}

import java.io._
import java.util.ArrayList
import java.util.StringTokenizer
import scala.annotation.tailrec
//import java.math.BigDecimal

object Bitz_G {

  def main(args: Array[String]): Unit = {

    //COMMENT ME !
//    is = new ByteArrayInputStream(Test.t6.trim.getBytes("ISO-8859-1"))
//    debugV = true

    //---------------------------- parameters reading --------------------------
    br = setupInputOutput();
    val line1 = readLine
    val n = line1.int

    println(comb(n, 5) * comb(n, 3))

    //---------------------------- parameters reading end ----------------------

  }

  def comb(n: Int, m: Int) = {
    factLast(n+m-1, m)  / fact(m)
  }

  def fact(v: Long): Long = v match {
    case 1 => 1
    case x => x * fact(x - 1)
  }

  def factLast(v:Int, k:Int):BigDecimal = {
    var res = BigDecimal(1);
    for (i <- (v-k+1) to v) {
      res *= i
//      db(res)
    }
    res
  }

  //---------------------------- service code ----------------------------------

  def readLine() = new Line
  class Line {
    val tok = new StringTokenizer(br.readLine(), " ")
    def int = tok.nextToken().toInt
    def long = tok.nextToken().toLong
    def double = tok.nextToken().toDouble
    def string = tok.nextToken()
  }

  var debugV = false
  var is = System.in
  var br = setupInputOutput();
  def db(x: => String) = if (debugV) println(x) // nullary function
  def setupInputOutput(): BufferedReader = {
    val devEnv = this.getClass.getCanonicalName.contains(".")
    if (!devEnv) {
      is = System.in
      debugV = false
    }
    val bis = new BufferedInputStream(is);
    new BufferedReader(new InputStreamReader(bis));
  }

//============================================================================================
object Test {

val sa1 = """
321
"""//66715035255088

val t6 = """
624
"""//7147161340917624

}

}


We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$.  The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$) — the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$) — the initial position of point $$$A$$$ and desirable absolute difference.

For each test case, print the minimum number of steps to make point $$$B$$$ exist.

NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.

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

  val t = readInt()

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

    val ans =
      if (n <= k) k - n
      else (n % 2 - k % 2).abs

    println(ans)
  }
}

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

  val t = readInt()

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

    val ans =
      if (n <= k) k - n
      else 2 * math.ceil((n + k) / 2.0).toInt - k - n

    println(ans)
  }
}

object Main {
  var io = new FastIO("in.txt")
  var T, A, K = 0
  def main(args: Array[String]): Unit = {
    T = io.nextInt
    while({T-=1; T >= 0}){
      A = io.nextInt
      K = io.nextInt
      if(A < K){
        io.println(K - A)
      }else if(A > K){
        var ans = {
          (A - K) % 2 match {
            case 0 => 0
            case 1 => 1
          }
        }

        io.println(ans)

      }else{
        io.println(0)
      }
    }
    io.flush()
  }
}

import java.io._
import java.util.StringTokenizer

import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.control.Breaks.{break, breakable}
class FastIO {
  var sb: StringBuilder = new StringBuilder()
  var br: BufferedReader = null;
  var st: StringTokenizer = null;
  var ps: PrintStream = new PrintStream(System.out)

  def this(fname: String) {
    this()
    try {
      var input = new File(fname)
      if(input.exists()){
        System.setIn(new FileInputStream(fname))
      }
    }
    catch {
      case e: Exception => {
        e.printStackTrace()
      }
    }

    br = new BufferedReader(new InputStreamReader(System.in))
  }

  def next: String = {
    while ( {
      st == null || !st.hasMoreElements
    }) try st = new StringTokenizer(br.readLine)
    catch {
      case e: IOException =>
        e.printStackTrace()
    }
    st.nextToken
  }

  def nextInt: Int = next.toInt

  def nextLong: Long = next.toLong

  def nextDouble: Double = next.toDouble

  def flush(): Unit = {
    ps.print(sb.toString())
    sb.setLength(0)
  }

  def print(o: Any): Unit = {
    sb.append(o)
  }

  def println(o: Any): Unit = {
    sb.append(o)
    sb.append('\n')
  }

  def nextLine: String = {
    var str = ""
    try str = br.readLine
    catch {
      case e: IOException =>
        e.printStackTrace()
    }
    str
  }
}

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

  import scala.collection.mutable
  import scala.collection.mutable.ListBuffer
  import scala.util.control.Breaks.{break, breakable}

  var io = new FastIO("in.txt")
  var T, A, K = 0
  def main(args: Array[String]): Unit = {
    T = io.nextInt
    while({T-=1; T >= 0}){
      A = io.nextInt
      K = io.nextInt
      if(A < K){
        io.println(K - A)
      }else if(A > K){
        var ans = {
          (A - K) % 2 match {
            case 0 => 0
            case 1 => 1
          }
        }

        io.println(ans)

      }else{
        io.println(0)
      }
    }
    io.flush()
  }

  class FastIO {
    var sb: StringBuilder = new StringBuilder()
    var br: BufferedReader = null;
    var st: StringTokenizer = null;
    var ps: PrintStream = new PrintStream(System.out)

    def this(fname: String) {
      this()
      try {
        var input = new File(fname)
        if(input.exists()){
          System.setIn(new FileInputStream(fname))
        }
      }
      catch {
        case e: Exception => {
          e.printStackTrace()
        }
      }

      br = new BufferedReader(new InputStreamReader(System.in))
    }

    def next: String = {
      while ( {
        st == null || !st.hasMoreElements
      }) try st = new StringTokenizer(br.readLine)
      catch {
        case e: IOException =>
          e.printStackTrace()
      }
      st.nextToken
    }

    def nextInt: Int = next.toInt

    def nextLong: Long = next.toLong

    def nextDouble: Double = next.toDouble

    def flush(): Unit = {
      ps.print(sb.toString())
      sb.setLength(0)
    }

    def print(o: Any): Unit = {
      sb.append(o)
    }

    def println(o: Any): Unit = {
      sb.append(o)
      sb.append('\n')
    }

    def nextLine: String = {
      var str = ""
      try str = br.readLine
      catch {
        case e: IOException =>
          e.printStackTrace()
      }
      str
    }
  }
}

object DistanceAxis {
  def main(args: Array[String]): Unit = {
    import scala.io.StdIn._
    val input = readInt()
    var i = 0
    while(i < input) {
      val line = readLine().split(" ")
      val A = line(0).toInt
      val diff = line(1).toInt
      if(diff < A) {
        val result = (A - diff) % 2
        println(result)
      } else {
        println(math.abs(A-diff))
      }
      i += 1
    }
  }
}

object A extends App {
  import java.io._
  import java.nio.file.{Files, Path}
  import java.util.StringTokenizer
  import scala.io.Source
  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 inputLines = in.nextInt()

    inputLines.times {
      val a = in.nextInt()
      val k = in.nextInt()

      val sol1 = {
        if ((a + k) % 2 == 0) 0 else 1
      }
      val sol1B = (a + k + sol1) / 2

      val result1 = if (math.abs(sol1B - math.abs((a + sol1) - sol1B)) == k) sol1 else 100000000

      val sol2 = {
        if ((a - k) < 0) (a - k) * -1
        else {
          if (((a - k)) % 2 == 0) 0 else 1
        }
      }

      val sol2B = (a - k + sol2) / 2

      val result2 = if (math.abs(sol2B - math.abs((a + sol2) - sol2B)) == k) sol2 else 100000000

      println(math.min(result1, result2))
    }
  }

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

  /**
    * Util methods
    */

  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()
  }
}


Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!).Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps:  Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$;  Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$;  Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$;  Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$;  Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$;  Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$;  $$$\dots$$$  Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$;  Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \in\{1, 2, \ldots, 5 \cdot 10^8\}$$$ as described above.For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ $$$\dots$$$ $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array.Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.

The first line of the input contains one integer number $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.

Print $$$n$$$ integers — $$$b_1, b_2, \dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $$$a$$$. Note that you cannot change the order of elements in the array.

NoteThe first example is described in the problem statement.

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)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    REP(N) { i =>
      if (A(i) % 2 == 0) {
        A(i) -= 1
      }
    }
    out.println(A.mkString(" "))
  }

  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[Int](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object CF498A extends App {

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

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

  def solve = {
    val array = (new Array[Int](in.nextInt)).map(_ => in.nextInt)
    val result = array.map(x => {
      if (x%2 == 0) x-1 else x
    })
    out.println(result.mkString(" "))
  }

  solve
  out.flush
  out.close
}

import scala.io.StdIn

object AdjacentReplacements {

  def main(args: Array[String]): Unit = {
    StdIn.readLine()
    val numbers = StdIn.readLine().split(" ").map(_.toInt)
    println(solve(numbers).mkString(" "))
  }

  def solve(numbers: Seq[Int]): Seq[Int] = {
    numbers.map(n => if (n % 2 == 0) n - 1 else n)
  }
}

/**
  * @author ShankarShastri
  *         Algorithm: ProblemA
  */

import scala.io.StdIn._
import scala.annotation.tailrec

object ProblemA {
  def main(args: Array[String]): Unit = {
    val _ = readInt
    val arrList = readLine().split(" ").map(_.toInt).toList
    val res = arrList.map(x => {
      if(x%2 ==0) x-1
      else x
    })
    println(res.mkString(" "))
  }
}

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

  override def apply(io: IO): io.type = {
    val nums = io.read[Vector[Int]]
    val ans = nums.map(i => if(i%2 == 0) i-1 else i)
    io.writeAll(ans)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
/***********************************[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 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
      }
    }
  }
}

object Main2 extends App {

  import java.io._
  import java.util.{StringTokenizer}

  val in = new Reader()
  val out: PrintWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)))

  val MOD = 1000000000 + 7
  val eps = 0.00000001

  val n = in.nextInt
  val a = in.nextLine.split(' ').map(_.toInt).map(x => (x - 1) / 2 * 2 + 1)
  out.print(a.mkString(" "))
  out.flush()
  out.close()

  class Reader @throws[IOException]
  () {
    var br: BufferedReader = new BufferedReader(new InputStreamReader(System.in))
    var tok: StringTokenizer = null

    def this(file: String) {
      this()
      br = new BufferedReader(new FileReader(file))
    }

    @throws[IOException]
    def next: String = {
      while (tok == null || !tok.hasMoreElements) tok = new StringTokenizer(br.readLine)
      tok.nextToken
    }

    @throws[NumberFormatException]
    @throws[IOException]
    def nextInt: Int = Integer.valueOf(next)

    @throws[NumberFormatException]
    @throws[IOException]
    def nextLong: Long = next.toLong

    @throws[NumberFormatException]
    @throws[IOException]
    def nextDouble: Double = next.toDouble

    @throws[IOException]
    def nextLine: String = br.readLine
  }

}


Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is $$$n$$$. There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains $$$2$$$ apartments, every other floor contains $$$x$$$ apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers $$$1$$$ and $$$2$$$, apartments on the second floor have numbers from $$$3$$$ to $$$(x + 2)$$$, apartments on the third floor have numbers from $$$(x + 3)$$$ to $$$(2 \cdot x + 2)$$$, and so on.Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least $$$n$$$ apartments.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n, x \le 1000$$$) — the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).

For each test case, print the answer: the number of floor on which Petya lives.

NoteConsider the first test case of the example: the first floor contains apartments with numbers $$$1$$$ and $$$2$$$, the second one contains apartments with numbers $$$3$$$, $$$4$$$ and $$$5$$$, the third one contains apartments with numbers $$$6$$$, $$$7$$$ and $$$8$$$. Therefore, Petya lives on the third floor.In the second test case of the example, Petya lives in the apartment $$$1$$$ which is on the first floor.

//package codeforces.contests._1426

object FloorNumber {
  def main(args: Array[String]): Unit = {
    for (_ <- 1 to io.StdIn.readInt) {
      val Array(n, x) = io.StdIn.readLine.split(" ").map(_.toLong)

      val result = {
        if (n <= 2) 1
        else {
          1 + (n - 2) / x + (if ((n - 2) % x != 0) 1 else 0)
        }
      }
      println(result)
    }
  }
}

import scala.io.Source

object tasks extends App {
  for (str <- Source.stdin.getLines().drop(1)){
    val ln: List[String] = str.split(" ").toList
    val aptnm: Int = ln.head.toInt
    val aptflr: Int = ln(1).toInt
    val floor = {
      if (aptnm < 3)
        1
      else
        ((aptnm.toDouble - 2) / aptflr).ceil.toInt + 1
    }
    println(s"$floor")
  }
}

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

  val t = readInt()

  (0 until t).foreach { _ =>
    val Array(n, x) = readLine().split(" ").map(_.toInt)
    val ans         = 0.max(n - 2 + x - 1) / x + 1

    println(ans)
  }
}

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

  val t = readInt()

  (0 until t).foreach { _ =>
    val Array(n, x) = readLine().split(" ").map(_.toInt)
    val ans         = (n - 2 + x - 1) / x + 1

    println(ans)
  }
}