While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j) such that ai ≤ aj and there are exactly k integers y such that ai ≤ y ≤ aj and y is divisible by x.In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1).

The first line contains 3 integers n, x, k (1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), where n is the size of the array a and x and k are numbers from the statement. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.

Print one integer — the answer to the problem.

NoteIn first sample there are only three suitable pairs of indexes — (1, 2), (2, 3), (3, 4).In second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4).In third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25.

object B extends App {

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

  val Array(n, x, k) = readInts(3)
  val as = readInts(n).sorted

  var res = 0L
  var r1, r2 = 0

  for (l <- as.indices) {
    val a = as(l)
    val from = (a - 1) / x
    while (r1 < n && (as(r1) < as(l) || as(r1) / x < from + k)) r1 += 1
    r2 = r1 max r2
    while (r2 < n && as(r2) / x <= from + k) r2 += 1
    res += (r2 - r1)
  }

  println(res)
}

object B 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, x, k) = readInts(3)
  val as = readLongs(n)

  val untils = as.map(a => (a - 1) / x)
  val tos = as.map(a => a / x)
//println(untils.mkString(" "))
  //println(tos.mkString(" "))
  val countByTo = tos.groupBy(identity).map {
    case (kk, v) => kk -> v.length.toLong
  }

  var res = 0L

  if (x == 1) {
    for {
      i <- as.indices
    } {
      val u = untils(i)
      //println(i, as(i), u, countByTo.get(k + u))
      if (k > 0) res += countByTo.getOrElse(k + u, 0L)
    }
  } else {
    for {
      i <- as.indices
    } {
      val u = untils(i)
      //println(i, as(i), u, countByTo.get(k + u))
      res += countByTo.getOrElse(k + u, 0L)
    }
  }

  println(res)
}

object B extends App {

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

  val Array(n, x, k) = readInts(3)
  val as = readInts(n).sorted

  var res = 0L
  var r1, r2 = 0

  for (l <- as.indices) {
    val a = as(l)
    val from = (a - 1) / x
    while (r1 < n && as(r1) / x < from + k) r1 += 1
    r2 = r1
    while (r2 < n && as(r2) / x <= from + k) r2 += 1
    res += (r2 - r1)
  }

  println(res)
}

object B 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, x, k) = readInts(3)
  val as = readLongs(n)

  val untils = as.map(a => (a - 1) / x)
  val tos = as.map(a => a / x)
//println(untils.mkString(" "))
  //println(tos.mkString(" "))
  val countByTo = tos.groupBy(identity).map {
    case (kk, v) => kk -> v.length.toLong
  }

  var res = 0L

  for {
    i <- as.indices
  } {
    val u = untils(i)
    res += countByTo.getOrElse(k + u, 0L)
  }

  println(res)
}

object B 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, x, k) = readInts(3)
  val as = readInts(n)

  val untils = as.map(a => (a - 1) / x)
  val tos = as.map(a => a / x)

  val countByTo = tos.groupBy(identity).map {
    case (k, v) => k -> v.length
  }

  var res = 0L

  for {
    i <- as.indices
  } {
    val u = untils(i)
    res += countByTo.getOrElse(k + u, 0)
  }

  println(res)
}

object B 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, x, k) = readInts(3)
  val as = readLongs(n)

  val untils = as.map(a => (a - 1) / x)
  val tos = as.map(a => a / x)
//println(untils.mkString(" "))
//  println(tos.mkString(" "))
  val countByTo = tos.groupBy(identity).map {
    case (kk, v) => kk -> v.length.toLong
  }

  var res = 0L

  /*if (x == 1) {
    for {
      i <- as.indices
    } {
      val u = untils(i)
      //println(i, as(i), u, countByTo.get(k + u))
      if (k > 0) res += countByTo.getOrElse(k + u, 0L)
    }
  } else {*/
    for {
      i <- as.indices
    } {
      val u = untils(i)
      //println(i, as(i), u, countByTo.get(k + u))
      if (as(i) % x > 0 || k > 0) res += countByTo.getOrElse(k + u, 0L)
    }
  //}

  println(res)
}


You are given a string $$$s$$$ consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0for init = 0 to inf    cur = init    ok = true    for i = 1 to |s|        res = res + 1        if s[i] == '+'            cur = cur + 1        else            cur = cur - 1        if cur < 0            ok = false            break    if ok        breakNote that the $$$inf$$$ denotes infinity, and the characters of the string are numbered from $$$1$$$ to $$$|s|$$$.You have to calculate the value of the $$$res$$$ after the process ends.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The only lines of each test case contains string $$$s$$$ ($$$1 \le |s| \le 10^6$$$) consisting only of characters + and -. It's guaranteed that sum of $$$|s|$$$ over all test cases doesn't exceed $$$10^6$$$.

For each test case print one integer — the value of the $$$res$$$ after the process ends.

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

  val t = readInt()

  (0 until t).foreach { _ =>
    val s = readLine()

    val res = s.zipWithIndex
      .foldLeft((s.length.toLong, 0)) {
        case ((res, airbag), (c, i)) =>
          c match {
            case '+' => (res, airbag + 1)
            case '-' =>
              if (airbag > 0) (res, airbag - 1)
              else (res + i + 1, airbag)
          }
      }
      ._1

    println(res)
  }
}

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

  val t = readInt()

  (0 until t).foreach { _ =>
    val s = readLine()

    val res = s.zipWithIndex
      .foldLeft((s.length, 0)) {
        case ((res, airbag), (c, i)) =>
          c match {
            case '+' => (res, airbag + 1)
            case '-' =>
              if (airbag > 0) (res, airbag - 1)
              else (res + i + 1, airbag)
          }
      }
      ._1

    println(res)
  }
}


The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.Given a sequence of n integers p1, p2, ..., pn. You are to choose k pairs of integers: [l1, r1], [l2, r2], ..., [lk, rk] (1 ≤ l1 ≤ r1 < l2 ≤ r2 < ... < lk ≤ rk ≤ n; ri - li + 1 = m), in such a way that the value of sum  is maximal possible. Help George to cope with the task.

The first line contains three integers n, m and k (1 ≤ (m × k) ≤ n ≤ 5000). The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ 109).

Print an integer in a single line — the maximum possible value of sum.

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, m, k) = in.next().split(' ').map(_.toInt)
  val data = in.next().split(' ').map(_.toInt)
  val psumR = Array.ofDim[Long](n + 1)
  (1 to n).foreach { i => psumR(i) = psumR(i - 1) + data(i - 1) }
//  println(psumR.mkString(" "))
  var prev = Array.ofDim[Long](n + 1)
  var current = Array.ofDim[Long](n + 1)
  var i = 1
  while (i <= k) {
    var j = m
    prev = current
    current = Array.ofDim[Long](n + 1)
    while (j <= n) {
      current(j) = Math.max(current(j - 1), prev(j - m) + psumR(j) - psumR(j - m))
      j += 1
    }
//    println(current.mkString(" "))
    i += 1
  }
//  println(current.mkString(" "))
//  println(dp.map(_.mkString(" ")).mkString("\n"))
  println(current(n))
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, m, k) = in.next().split(' ').map(_.toInt)
  val data = in.next().split(' ').map(_.toInt)
  val psumR = Array.ofDim[Long](n)
  psumR(0) = data.head
  (1 until n).foreach { i => psumR(i) = psumR(i - 1) + data(i) }
  val dp = Array.ofDim[Long](k + 1, n)
  (1 to k).foreach { i =>
    (1 until n).foreach { j =>
      if (i * m <= j)
        dp(i)(j) = Math.max(dp(i)(j - 1), dp(i - 1)(j - 1) + psumR(j) - psumR(j - m))
    }
  }
  println(dp(k)(n - 1))

}


A team of furry rescue rangers was sitting idle in their hollow tree when suddenly they received a signal of distress. In a few moments they were ready, and the dirigible of the rescue chipmunks hit the road.We assume that the action takes place on a Cartesian plane. The headquarters of the rescuers is located at point (x1, y1), and the distress signal came from the point (x2, y2).Due to Gadget's engineering talent, the rescuers' dirigible can instantly change its current velocity and direction of movement at any moment and as many times as needed. The only limitation is: the speed of the aircraft relative to the air can not exceed  meters per second.Of course, Gadget is a true rescuer and wants to reach the destination as soon as possible. The matter is complicated by the fact that the wind is blowing in the air and it affects the movement of the dirigible. According to the weather forecast, the wind will be defined by the vector (vx, vy) for the nearest t seconds, and then will change to (wx, wy). These vectors give both the direction and velocity of the wind. Formally, if a dirigible is located at the point (x, y), while its own velocity relative to the air is equal to zero and the wind (ux, uy) is blowing, then after  seconds the new position of the dirigible will be .Gadget is busy piloting the aircraft, so she asked Chip to calculate how long will it take them to reach the destination if they fly optimally. He coped with the task easily, but Dale is convinced that Chip has given the random value, aiming only not to lose the face in front of Gadget. Dale has asked you to find the right answer.It is guaranteed that the speed of the wind at any moment of time is strictly less than the maximum possible speed of the airship relative to the air.

The first line of the input contains four integers x1, y1, x2, y2 (|x1|,  |y1|,  |x2|,  |y2| ≤ 10 000) — the coordinates of the rescuers' headquarters and the point, where signal of the distress came from, respectively.  The second line contains two integers  and t (0 < v, t ≤ 1000), which are denoting the maximum speed of the chipmunk dirigible relative to the air and the moment of time when the wind changes according to the weather forecast, respectively.  Next follow one per line two pairs of integer (vx, vy) and (wx, wy), describing the wind for the first t seconds and the wind that will blow at all the remaining time, respectively. It is guaranteed that  and .

Print a single real value — the minimum time the rescuers need to get to point (x2, y2). You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.  Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-6) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-6) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1: Double, yDir: Double): Double = {
    val vy1 = yDir * Math.sqrt(sqr(vMax) - sqr(vx1))
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, Double.MaxValue)
  }

  def getMinT2(from: Double, to: Double, yDir: Double): Double = {
    var l = from
    var r = to
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1, yDir)
      val tr1 = compute(r1, yDir)
      if (math.abs(tl1 - tr1) < 1E-7) {
        val t2 = (tl1 + tr1) / 2d
        return t + t2
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
    0d
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    val t2 = getMinT2(-vMax, 0d, 1d) min getMinT2(-vMax, 0d, -1d) min getMinT2(0, vMax, 1d) min getMinT2(0, vMax, -1d)
    println(t2)
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1: Double): Double = {
    val vy1 = Math.sqrt(sqr(vMax) - sqr(vx1))
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, 1e10)
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    var l = 0d
    var r = vMax
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1)
      val tr1 = compute(r1)
      if (math.abs(tl1 - tr1) < 1E-9) {
        val t2 = compute((l + r) / 2d)
        println(t + t2)
        System.exit(0)
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1s: Double): Double = {
    val vx1 = Math.sqrt(vx1s)
    val vy1 = Math.sqrt(sqr(vMax) - vx1s)
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, 1e10)
  }

  def getMinT2(vx1From: Double, vx2To: Double): Double = {
    var l = vx1From
    var r = vx2To
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1)
      val tr1 = compute(r1)
      if (math.abs(tl1 - tr1) < 1E-8) {
        val t2 = (tl1 + tr1) / 2d
        return t + t2
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
    0d
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    var min = Double.MaxValue
    val n = 1
    val vMaxS = sqr(vMax)
    for (i <- 0 until n) {
      val vx1From = i * vMax / n
      val vx1To = (i + 1) * vMaxS / n
      val t2 = getMinT2(vx1From, vx1To)
      if (t2 < min) min = t2
    }
    println(min)
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1: Double): Double = {
    val vy1 = Math.sqrt(sqr(vMax) - sqr(vx1))
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, Double.MaxValue)
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    var l = 0d
    var r = vMax
    while (math.abs(r - l) > 1E-8) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1)
      val tr1 = compute(r1)
      if (tl1 < tr1) r = r1
      else l = l1
    }
    val t2 = compute((l + r) / 2d)
    println(t + t2)
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-7) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-7) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1s: Double): Double = {
    val vx1 = Math.sqrt(vx1s)
    val vy1 = Math.sqrt(sqr(vMax) - vx1s)
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, 1e8)
  }

  def getMinT2(vx1From: Double, vx2To: Double): Double = {
    var l = vx1From
    var r = vx2To
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1)
      val tr1 = compute(r1)
      if (math.abs(tl1 - tr1) < 1E-7) {
        val t2 = (tl1 + tr1) / 2d
        return t + t2
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
    0d
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    var min = Double.MaxValue
    val n = 10000
    val vMaxS = sqr(vMax)
    for (i <- 0 until n) {
      val vx1From = i * vMax / n
      val vx1To = (i + 1) * vMaxS / n
      val t2 = getMinT2(vx1From, vx1To)
      if (t2 < min) min = t2
    }
    println(min)
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1s: Double): Double = {
    val vx1 = Math.sqrt(vx1s)
    val vy1 = Math.sqrt(sqr(vMax) - vx1s)
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, Double.MaxValue)
  }

  def getMinT2(vx1From: Double, vx2To: Double): Double = {
    var l = vx1From
    var r = vx2To
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1)
      val tr1 = compute(r1)
      if (math.abs(tl1 - tr1) < 1E-8) {
        val t2 = (tl1 + tr1) / 2d
        return t + t2
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
    0d
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    val n = 1000
    val vMaxS = sqr(vMax)
    var min = getMinT2(0, vMaxS)
    for (i <- 0 until n) {
      val vx1From = i * vMax / n
      val vx1To = (i + 1) * vMaxS / n
      val t2 = getMinT2(vx1From, vx1To)
      if (t2 < min) min = t2
    }
    println(min)
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1: Double): Double = {
    val vy1 = Math.sqrt(sqr(vMax) - sqr(vx1))
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, 1e10)
  }

  def getMinT2(vx1From: Double, vx2To: Double): Double = {
    var l = 0d
    var r = vMax
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1)
      val tr1 = compute(r1)
      if (math.abs(tl1 - tr1) < 1E-9) {
        val t2 = compute((l + r) / 2d)
        return t + t2
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
    0d
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    var min = Double.MaxValue
    val n = 10000
    for (i <- 0 until n) {
      val vx1From = i * vMax / n
      val vx1To = (i + 1) * vMax / n
      val t2 = getMinT2(vx1From, vx1To)
      if (t2 < min) min = t2
    }
    println(min)
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-7) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-7) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1: Double, yDir: Double): Double = {
    val vy1 = yDir * Math.sqrt(sqr(vMax) - sqr(vx1))
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, 1e8)
  }

  def getMinT2(from: Double, to: Double, yDir: Double): Double = {
    var l = from
    var r = to
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1, yDir)
      val tr1 = compute(r1, yDir)
      if (math.abs(tl1 - tr1) < 1E-7) {
        val t2 = (tl1 + tr1) / 2d
        return t + t2
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
    0d
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    val t2 = getMinT2(-vMax, 0d, 1d) min getMinT2(-vMax, 0d, -1d) min getMinT2(0, vMax, 1d) min getMinT2(0, vMax, -1d)
    println(t2)
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-8) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1s: Double): Double = {
    val vx1 = Math.sqrt(vx1s)
    val vy1 = Math.sqrt(sqr(vMax) - vx1s)
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, Double.MaxValue)
  }

  def getMinT2(vx1From: Double, vx2To: Double): Double = {
    var l = vx1From
    var r = vx2To
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1)
      val tr1 = compute(r1)
      if (math.abs(tl1 - tr1) < 1E-8) {
        val t2 = (tl1 + tr1) / 2d
        return t + t2
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
    0d
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    var min = Double.MaxValue
    val n = 1
    val vMaxS = sqr(vMax)
    for (i <- 0 until n) {
      val vx1From = i * vMax / n
      val vx1To = (i + 1) * vMaxS / n
      val t2 = getMinT2(vx1From, vx1To)
      if (t2 < min) min = t2
    }
    println(min)
  }
}

import scala.collection._

object D extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readDoubles(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toDouble) }

  val Array(x1, y1, x2, y2) = readDoubles(4)
  val Array(vMax, t) = readDoubles(2)
  val Array(vx, vy) = readDoubles(2)
  val Array(wx, wy) = readDoubles(2)

  def sqr(x: Double) = x * x

  def can1(t0: Double): Boolean = {
    sqr(x2 - x1 - t0 * vx) + sqr(y2 - y1 - t0 * vy) <= sqr(t0 * vMax)
  }

  def can2(x0: Double, y0: Double, t0: Double): Boolean = {
    sqr(x2 - x0 - t0 * wx) + sqr(y2 - y0 - t0 * wy) <= sqr(t0 * vMax)
  }

  @annotation.tailrec
  def binSearchD1(lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-7) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can1(mid)) binSearchD1(mid, hi)
      else binSearchD1(lo, mid)
    }
  }

  @annotation.tailrec
  def binSearchD2(x0: Double, y0: Double, lo: Double, hi: Double): Double = {
    if (math.abs(hi - lo) < 1E-7) lo
    else {
      val mid = (lo + hi) / 2d
      if (!can2(x0, y0, mid)) binSearchD2(x0, y0, mid, hi)
      else binSearchD2(x0, y0, lo, mid)
    }
  }

  def compute(vx1: Double): Double = {
    val vy1 = Math.sqrt(sqr(vMax) - sqr(vx1))
    val x0 = x1 + t * (vx1 + vx)
    val y0 = y1 + t * (vy1 + vy)
    binSearchD2(x0, y0, 0, 1e8)
  }

  def getMinT2(vx1From: Double, vx2To: Double): Double = {
    var l = -vMax
    var r = vMax
    while (true) {
      val l1 = l + (r - l) / 3d
      val r1 = r - (r - l) / 3d
      val tl1 = compute(l1)
      val tr1 = compute(r1)
      if (math.abs(tl1 - tr1) < 1E-7) {
        val t2 = (tl1 + tr1) / 2d
        return t + t2
      }
      if (tl1 < tr1) r = r1
      else l = l1
    }
    0d
  }

  if (can1(t)) {
    val t1 = binSearchD1(0d, t)
    println(t1)
  } else {
    var min = Double.MaxValue
    val n = 1
    for (i <- 0 until n) {
      val vx1From = i * vMax / n
      val vx1To = (i + 1) * vMax / n
      val t2 = getMinT2(vx1From, vx1To)
      if (t2 < min) min = t2
    }
    println(min)
  }
}


You are given two positive integers $$$a$$$ and $$$b$$$.In one move, you can change $$$a$$$ in the following way:  Choose any positive odd integer $$$x$$$ ($$$x > 0$$$) and replace $$$a$$$ with $$$a+x$$$;  choose any positive even integer $$$y$$$ ($$$y > 0$$$) and replace $$$a$$$ with $$$a-y$$$. You can perform as many such operations as you want. You can choose the same numbers $$$x$$$ and $$$y$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$. It is guaranteed that you can always obtain $$$b$$$ from $$$a$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case is given as two space-separated integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).

For each test case, print the answer — the minimum number of moves required to obtain $$$b$$$ from $$$a$$$ if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain $$$b$$$ from $$$a$$$.

NoteIn the first test case, you can just add $$$1$$$.In the second test case, you don't need to do anything.In the third test case, you can add $$$1$$$ two times.In the fourth test case, you can subtract $$$4$$$ and add $$$1$$$.In the fifth test case, you can just subtract $$$6$$$.

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 C624(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 C624(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val t = ni()
    REP(t) { _ =>
      val a = ni()
      val b = ni()

      if(a == b) {
        out.println("0")
      } else if(a > b) {
        val x = a - b
        if(x % 2 == 0) {
          out.println("1")
        } else out.println("2")
      } else {
        val x = b - a
        if(x % 2 != 0) {
          out.println("1")
        } else out.println("2")
      }
    }
  }
}

object A extends App {
  import java.io.{BufferedReader, InputStreamReader, StreamTokenizer}

  solve()

  def solve(): Unit = {
    val in = new StreamTokenizer(new BufferedReader(new InputStreamReader((System.in))))

    @inline def nextInt = {
      in.nextToken()
      in.nval.asInstanceOf[Int]
    }
    @inline def operations(a: Int, b: Int) = {
      val diff = b - a
      if (diff == 0) {
        0
      } else if (diff > 0) {
        if (diff % 2 == 0) 2
        else 1
      } else {
        if (diff % 2 == 0) 1
        else 2
      }
    }

    val t = nextInt
    for (_ <- 1 to t) {
      val (a, b) = (nextInt, nextInt)
      println(operations(a, b))
    }
  }

}


Let's assume that we are given a matrix b of size x × y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x × y matrix c which has the following properties:  the upper half of matrix c (rows with numbers from 1 to x) exactly matches b;  the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1). Sereja has an n × m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain?

The first line contains two integers, n and m (1 ≤ n, m ≤ 100). Each of the next n lines contains m integers — the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 1) — the i-th row of the matrix a.

In the single line, print the answer to the problem — the minimum number of rows of matrix b.

NoteIn the first test sample the answer is a 2 × 3 matrix b:001110If we perform a mirroring operation with this matrix, we get the matrix a that is given in the input:001110110001

object Solution extends App {
  val in = io.Source.stdin.getLines()
  val Array(n, m) = in.next().split(' ').map(_.toInt)

  val lines = in.take(n).toList

  def count(matrix: List[String]): Int = {
    val candidate = matrix.take(matrix.length / 2)
    if (matrix.length % 2 == 0 && candidate.zip(matrix.drop(matrix.length / 2).reverse).forall(i => i._1 == i._2)) {
      count(candidate)
    } else matrix.length
  }

  println(count(lines))
}


Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e.  or  (or both).Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).

The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively. Each of the next m lines contains a pair of integers ui and vi (1  ≤  ui,  vi  ≤  n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.

If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes). If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

NoteIn the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).In the second sample, there is no way to satisfy both Pari and Arya.

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

object _688C extends CodeForcesApp {
  override def apply(io: IO) = {import io.{read, write}
    val v, e = read[Int]
    val graph = map[Int] to mutable.Set.empty[Int]
    repeat(e) {
      val u, v = read[Int]
      graph(u) += v
      graph(v) += u
    }
    val pari, arya = mutable.Set.empty[Int]

    def dfs(i: Int, fromPari: Boolean): Unit = {
      val side = if (fromPari) pari else arya
      side += i
      //debug(i, fromPari, pari, arya)
      graph(i) foreach {
        case j if pari(j) => require(!fromPari)
        case j if arya(j) => require(fromPari)
        case j => dfs(j, !fromPari)
      }
    }

    try {
      (1 to v).foreach(i => if(!pari(i) && !arya(i)) dfs(i, fromPari = true))
      write(pari.size)
        .writeLine()
        .writeAll(pari)
        .writeLine()
        .write(arya.size)
        .writeLine()
        .writeAll(arya)
    } catch {
      case _: Throwable => write(-1)
    }
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
object Utils {
  import scala.collection.mutable, mutable.{Map => Dict}
  implicit class GenericExtensions[A](val a: A) extends AnyVal {
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }
  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](val t: C[A]) extends AnyVal {
    def onlyElement: A = t.head.ensuring(t.size == 1)
    def groupByIdentifier[K](f: A => K): Map[K, A] = t.groupBy(f).mapValues(_.onlyElement)
    def duplicates(implicit b: C of A): C[A] = {
      val elems = mutable.Set.empty[A]
      (t collect {case i if !elems.add(i) => i}).to[C]
    }
    def sumWith[B](f: A => B)(implicit n: Numeric[B]): B = t.foldLeft(n.zero) {case (p, i) => n.plus(p, f(i))}
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]
    def mapTo[B](f: A => B)(implicit b: C of (A, B)): Map[A, B] = zipWith(f).toUniqueMap
    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)
    def collectFirst[B](f: PartialFunction[A, Option[B]]): Option[B] = t.view collect f collectFirst {case Some(x) => x}
    def toMutableMultiSet: Dict[A, Int] = {
      val c = map[A] to 0
      t.foreach(i => c(i) += 1)
      c
    }
    def toMultiSet: Map[A, Int] = toMutableMultiSet.toMap withDefaultValue 0
  }
  implicit class ArrayExtensions[A <: AnyRef](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = java.util.Arrays.sort(a, from, until, cmp) //sort [from, until)
  }
  implicit class ArrayExtensions2[A](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = a.view(from, until).sorted.copyToArray(a, from)
  }
  implicit class SeqExtensions[A, C[X] <: Seq[X]](val s: C[A]) extends AnyVal {
    def isRotationOf(t: Seq[A]): Boolean = s ++ s containsSlice t
  }
  implicit class PairExtensions[A, B](val p: (A, B)) extends AnyVal {
    def key = p._1
    def value = p._2
  }
  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_.key).mapValues(_.value)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C])
    def swap(implicit b: C of (B, A)): C[(B, A)] = t.map(_.swap).to[C]
  }
  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](val m: C[K, V]) extends AnyVal {
    def lastKeyOption: Option[K] = m.lastOption.map(_.key)
    def firstKeyOption: Option[K] = m.headOption.map(_.key)
    def invert: Map[V, Set[K]] = m.toSet[(K, V)].swap.toMultiMap
    def mapKeys[K2](f: K => K2): Map[K2, V] = m map {case (k, v) => f(k) -> v}
    def toMutable: Dict[K, V] = Dict.empty[K, V] ++ m
  }
  implicit class MutableMapExtensions[K, V](val m: mutable.Map[K, V]) extends AnyVal {
    def removeLast(): Option[(K, V)] = m.lastOption.map(removeNode)
    def removeFirst(): Option[(K, V)] = m.headOption.map(removeNode)
    private def removeNode(kv: (K, V)): (K, V) = {
      m -= kv.key
      kv
    }
  }
  implicit class SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }
  implicit class IterableExtensions[A, C[A] <: Seq[A]](val s: C[A]) extends AnyVal {
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
    def foldUntil[B](z: B)(f: (B, A) => B)(c: B => Boolean): Option[(B, Int)] = s.view.scanLeft(z)(f) findWithIndex c
  }
  implicit class BooleanExtensions(val x: Boolean) extends AnyVal {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toYesNo = if(x) "yes" else "no"
  }
  implicit class IntExtensions(val x: Int) extends AnyVal {
    def bitCount: Int = java.lang.Integer.bitCount(x)
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    def bitCount: Int = java.lang.Long.bitCount(x)
  }
  implicit class NumericExtensions[A](val x: A)(implicit n: Numeric[A]) {
    import Numeric.Implicits._
    def **(i: Int): A = if (i == 0) n.one else {
      val h = x ** (i/2)
      if (i%2 == 0) h * h else h * h * x
    }
    def distance(y: A) = (x - y).abs()
    def nonNegative: Option[A] = when(n.gteq(x, n.zero))(x)
    def whenNegative(f: => A): A = nonNegative getOrElse f   //e.g. students indexOf "Greg" whenNegative Int.MaxValue
  }
  implicit class IntegralExtensions[A](val x: A)(implicit n: Integral[A]) {
    import scala.collection.immutable.NumericRange, Integral.Implicits._
    def mod(y: A): A = x - ((x / y) * y) //(x/y)*y + (x mod y) == x
    def -->(y: A): NumericRange.Inclusive[A] = NumericRange.inclusive(n.min(x, y), n.max(x, y), n.one)
    def ceilDiv(y: A): A = if (mod(y) == n.zero) x/y else n.one + (x/y)
  }
  implicit def toPoint[A](p: (A, A))(implicit n: Numeric[A]) = new java.awt.geom.Point2D.Double(n.toDouble(p._1), n.toDouble(p._2))
  def desc[A: Ordering]: Ordering[A] = asc[A].reverse  //e.g. students.sortBy(_.height)(desc)
  def asc[A](implicit order: Ordering[A]): Ordering[A] = order
  def map[K] = new {
    def to[V](default: => V): Dict[K, V] = using(_ => default)
    def using[V](f: K => V): Dict[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }
  def newMultiMap[K, V]: mutable.MultiMap[K, V] = new mutable.HashMap[K, mutable.Set[V]] with mutable.MultiMap[K, V]
  def memoize[A, B](f: A => B): A ==> B = map[A] using f
  implicit class FuzzyDouble(val x: Double) extends AnyVal {
    def  >~(y: Double) = x > y + eps
    def >=~(y: Double) = x >= y + eps
    def  ~<(y: Double) = y >=~ x
    def ~=<(y: Double) = y >~ x
    def  ~=(y: Double) = (x distance y) <= eps
  }
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None
  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  type ==>[A, B] = collection.Map[A, B]
  val mod: Int = (1e9 + 7).toInt
  val eps: Double = 1e-9
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

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

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

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

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

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

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

  def write(obj: Any, more: Any*): this.type = {
    printer.print(obj)
    more foreach {i =>
      printer.print(' ')
      printer.print(i)
    }
    this
  }
  def writeLine(obj: Any*): this.type = {
    if (obj.isEmpty) printer.println() else obj foreach printer.println
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)

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

import scala.collection.mutable

object problem_C{
  class Graph[T] {
    type Vertex = T
    type GraphMap = Map[Vertex, Set[Vertex]]
    var g:GraphMap = Map()

    def DFS: Option[mutable.Map[Vertex, Int]] = {

      val colors = mutable.Map[Vertex, Int]()
      def nextColor(color: Int) = if (color == 1) 2 else 1
      def dfs(color: Int, v: Vertex): Boolean = {
        if (colors.get(v).isDefined) {
          colors(v) == color
        }
        else {
          colors += (v -> color)
          g(v).forall(dfs(nextColor(color),_))
        }
      }

      if (g.keys.forall(v => if (colors.get(v).isEmpty) dfs(1,v) else true))
        Some(colors)
      else
        None
    }

    def toCF(ans: Option[mutable.Map[Int, Int]]) = ans match {
      case None => "-1"
      case Some(map) =>
        val (list1t, list2t) = map.toList.partition(_._2 == 1)
        val list1 = list1t.map(_._1)
        val list2 = list2t.map(_._1)

        val str = new StringBuffer("")

        str.append(list1.length).append("\n")
        list1.foreach(str.append(_).append(" "))
        str.append("\n").append(list2.length).append("\n")
        list2.foreach(str.append(_).append(" "))

        str.toString
    }

  }

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

    val scanner = new java.util.Scanner(System.in)
    val n = scanner.nextInt()
    val m = scanner.nextInt()

    val edge = new Array[(Int,Int)](m)

    0 to m-1 foreach {edge(_) = (scanner.nextInt, scanner.nextInt)}

    val intGraph = new Graph[Int]

    val adjacencyList =
      (edge ++ edge.map { case (a, b) => (b, a) })
      .groupBy[Int] {_._1}
      .map(x => x match {case (k,seq) => (k, seq.map(_._2).toSet)})

    val isolatedPoints = (1 until n+1).filter(adjacencyList.get(_).isEmpty).map((_, Set[Int]()))

    intGraph.g = adjacencyList ++ isolatedPoints

    //scary
    println(intGraph.toCF(intGraph.DFS))

  }

}

import scala.collection.mutable

object problem_C_2{
  class Graph {
    var g: Array[mutable.Set[Int]] = Array()

    def DFS: Option[mutable.Map[Int, Int]] = {

      val colors = mutable.Map[Int, Int]()
      def nextColor(color: Int) = if (color == 1) 2 else 1
      def dfs(color: Int, v: Int): Boolean = {
        if (colors.get(v).isDefined) {
          colors(v) == color
        }
        else {
          colors += (v -> color)
          g(v).forall(dfs(nextColor(color),_))
        }
      }

      if ((1 to g.length - 1) forall { i => if (colors.get(i).isEmpty) dfs(1,i) else true }) {
        Some(colors)
      }
      else{
        None
      }

    }

    def toCF(ans: Option[mutable.Map[Int, Int]]) = ans match {
      case None => "-1"
      case Some(map) =>

        //if (g.length > 50000) println("i'm here1")

        val (list1t, list2t) = map.toList.partition(_._2 == 1)
        val list1 = list1t.map(_._1)
        val list2 = list2t.map(_._1)

        val str = new StringBuffer("")

        //if (g.length > 50000) println("i'm here2")

        str.append(list1.length).append("\n")
        list1.foreach(str.append(_).append(" "))
        str.append("\n").append(list2.length).append("\n")
        list2.foreach(str.append(_).append(" "))

        //if (g.length > 50000) println("i'm here3")

        str.toString
    }

  }

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

    val scanner = new java.util.Scanner(System.in)
    val n = scanner.nextInt()
    val m = scanner.nextInt()

    val adjacencyList = Array.fill[mutable.Set[Int]](n + 1)(mutable.Set())
    0 to m-1 foreach {t =>
      val i = scanner.nextInt; val j = scanner.nextInt
      adjacencyList(i).add(j)
      adjacencyList(j).add(i)
    }

    val graph = new Graph
    graph.g = adjacencyList

    println(graph.toCF(graph.DFS))

  }

}

import java.util.Scanner

import scala.collection.immutable.HashSet
import scala.io.StdIn

object CodeForces extends App {

  val arr = StdIn.readLine().split(' ').map(_.toInt)
  val n = arr(0)
  val m = arr(1)

  val graph = Array.ofDim[List[Int]](n)

  for (_ <- 1 to m){
     val arr = StdIn.readLine().split(' ').map(_.toInt)
    val x = arr(0) - 1
    val y = arr(1) - 1
    if (graph(x) == null) graph(x) = List.empty
    if (graph(y) == null) graph(y) = List.empty

    graph(x) =  y :: graph(x)
    graph(y) =  x :: graph(y)
  }

  val used = Array.ofDim[Boolean](n)
  val set1 = collection.mutable.HashSet.empty[Int]
  val set2 = collection.mutable.HashSet.empty[Int]

  def dfs(x: Int,current: Int): Unit = {
    used(x) = true
    current match {
      case 0 => set1.add(x)
      case 1 => set2.add(x)
      case _ => throw new Exception("wtf")
    }
    graph(x)
      .filter(!used(_))
      .foreach(dfs(_, (current + 1) % 2))
  }

  for(i <- 0 until n) {
    if(!used(i) && graph(i) != null) dfs(i, 0)
  }

  if(set1.intersect(set2).isEmpty) {
    println(set1.size)
    set1.foreach({el =>
      print(el + 1)
      print(" ")
    })
    println()
    println(set2.size)
    set2.foreach({el =>
      print(el + 1)
      print(" ")
    })
  } else println(-1)
}

import scala.collection.mutable.ArrayBuffer

//https://codeforces.com/problemset/problem/687/A
object NPHardProblem {
  def main(args: Array[String]): Unit = {
    val in =scala.io.StdIn
    val arr=in.readLine().split(" ").map(_.toInt)
    val n=arr(0)
    val m=arr(1)
    val graph=Array.fill[ArrayBuffer[Int]](n)(new ArrayBuffer[Int]())
    for(i<- 0 until m){
      val edges=in.readLine().split(" ").map(_.toInt-1)
      graph(edges(0))+=(edges(1))
      graph(edges(1))+=(edges(0))
    }
    isBipartiteGraph(graph,n)
  }
  def isBipartiteGraph(graph: Array[ArrayBuffer[Int]],n:Int): Unit = {

    //The idea is to color the graph vertices in two color and two adjacent vertex can't have same color
    //Using DFS we will color the graph; if a vertex is having no color (0) then make it 1 and processed further
    //If we come across a vertex with ambiguity then we can say Bi-Partition is not possible
    val color = Array.fill[Int](n)(0)
    for (i <- 0 until n) {
      if (color(i) == 0) {
        color(i) = 1
      }
      if (!dfsToColorGraph(graph, i, color)) {
        println("-1")
        return
      }
    }

    var set1Count=0
    var set2Count=0
    var set1=ArrayBuffer[Int]()
    var set2=ArrayBuffer[Int]()
    for(i<- 0 until graph.length){
      if(color(i)==1){
        set1+=(i+1)
        set1Count+=1
      }else if(color(i)==2){
        set2+=(i+1)
        set2Count+=1
      }
    }
    println(set2Count)
    println(set2.mkString(" "))
    println(set1Count)
    println(set1.mkString(" "))
  }

  def dfsToColorGraph(graph: Array[ArrayBuffer[Int]], u: Int, color: Array[Int]): Boolean = {

    for (v <- 0 until graph(u).length) {
      if (color(graph(u)(v)) == 0) {
        color(graph(u)(v)) = (if (color(u) == 1) 2 else 1)
        if (!dfsToColorGraph(graph, graph(u)(v), color)) {
          return false
        }
      } else {
        if (color(u) == color(graph(u)(v))) {
          return false
        }
      }
    }

    return true
  }
}

import scala.collection.mutable.ArrayBuffer

object NPHardProblem {
  def main(args: Array[String]): Unit = {
    val in =scala.io.StdIn
    val arr=in.readLine().split(" ").map(_.toInt)
    val n=arr(0)
    val m=arr(1)
    val graph=Array.fill[ArrayBuffer[Int]](n)(new ArrayBuffer[Int]())
    for(i<- 0 until m){
      val edges=in.readLine().split(" ").map(_.toInt-1)
      graph(edges(0))+=(edges(1))
      graph(edges(1))+=(edges(0))
    }
    isBipartiteGraph(graph,n)
  }
  def isBipartiteGraph(graph: Array[ArrayBuffer[Int]],n:Int): Unit = {

    //The idea is to color the graph vertices in two color and two adjacent vertex can't have same color
    //Using DFS we will color the graph; if a vertex is having no color (0) then make it 1 and processed further
    //If we come across a vertex with ambiguity then we can say Bi-Partition is not possible
    val color = Array.fill[Int](n)(0)
    for (i <- 0 until n) {
      if (color(i) == 0) {
        color(i) = 1
      }
      if (!dfsToColorGraph(graph, i, color)) {
        println("-1")
        return
      }
    }

    var set1Count=0
    var set2Count=0
    var set1=ArrayBuffer[Int]()
    var set2=ArrayBuffer[Int]()
    for(i<- 0 until graph.length){
      if(color(i)==1){
        set1+=(i+1)
        set1Count+=1
      }else if(color(i)==2){
        set2+=(i+1)
        set2Count+=1
      }
    }
    println(set2Count)
    println(set2.mkString(" "))
    println(set1Count)
    println(set1.mkString(" "))
  }

  def dfsToColorGraph(graph: Array[ArrayBuffer[Int]], u: Int, color: Array[Int]): Boolean = {

    for (v <- 0 until graph(u).length) {
      if (color(graph(u)(v)) == 0) {
        color(graph(u)(v)) = (if (color(u) == 1) 2 else 1)
        if (!dfsToColorGraph(graph, graph(u)(v), color)) {
          return false
        }
      } else {
        if (color(u) == color(graph(u)(v))) {
          return false
        }
      }
    }

    return true
  }
}

object Solution extends App {
  val in = io.Source.stdin.getLines()
  val Array(n, m) = in.next().split(' ').map(_.toInt)
  val graph = Array.fill(n){List.empty[Int]}
  val taken = Array.ofDim[Int](n)
  (1 to m).foreach { _ =>
    val Array(from, to) = in.next().split(' ').map(_.toInt - 1)
    graph(from) ::= to
    graph(to) ::= from
  }
  var stop = false
  val isGood = graph.indices.foldLeft(true){
    case (false, _) => false
    case (true, i) =>
      if (taken(i) == 0) {
        taken(i) = 1
        var expand = graph(i)
        var shouldBe = 2
        var good = true
        while (expand.nonEmpty) {
          if (expand.exists(j => taken(j) != 0 && taken(j) != shouldBe)) {
            expand = Nil
            good = false
          } else {
            expand = expand.filter(j => taken(j) == 0)
            expand.foreach(j => taken(j) = shouldBe)
            shouldBe = if (shouldBe == 1) 2 else 1
            expand = expand.flatMap(graph)
          }
        }
        good
      }
      else true
  }
  if (isGood) {
    val first = taken.indices.filter(i => taken(i) == 1)
    val second = taken.indices.filter(i => taken(i) == 2)
    println(first.size)
    println(first.map(i => i + 1).mkString(" "))
    println(second.size)
    println(second.map(i => i + 1).mkString(" "))
  } else {
    println(-1)
  }
}

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

import scala.collection.mutable.ListBuffer
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 C360A(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 C360A(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

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

    g = Array.fill[ListBuffer[Int]](n)(new ListBuffer[Int])

    REP(m) { _ =>
      val u = ni()
      val v = ni()

      g(u-1) += v-1
      g(v-1) += u-1
    }

    marked = Array.fill(n)(-1)
    REP(n) { i =>
      if(marked(i) == -1) {
        marked(i) = 0
        fill(i)
      }
    }

    var yes = true
    REP(n) { i =>
      val c = marked(i)
      yes = yes & g(i).forall(p => marked(p) == (c^1))
    }
    if(!yes) {
      out.println(-1)
    } else {
      val x = marked.zipWithIndex.filter(_._1 == 0).map(_._2 + 1)
      out.println(x.length)
      out.println(x.mkString(" "))
      val y = marked.zipWithIndex.filter(_._1 == 1).map(_._2 + 1)
      out.println(y.length)
      out.println(y.mkString(" "))
    }
  }

  var g: Array[ListBuffer[Int]] = _
  var marked: Array[Int] = _

  def fill(u: Int) {
    val x = marked(u)
    g(u).foreach { f =>
      if(marked(f) == -1) {
        marked(f) = x^1
        fill(f)
      }
    }
  }
}

object A687 {
  def mark(
    edges: Array[Seq[Int]],
    color: Array[Int],
    now: Int,
    colorToAssign: Int
  ): Boolean = {
    //println(s"Coloring $now the color $colorToAssign")
    color(now) = colorToAssign

    val nextColor = 3 - colorToAssign

    edges(now).map {
      case next if (color(next) == 0) =>
        if (!mark(edges, color, next, nextColor)) return false
      case next if (color(next) != nextColor) =>
        return false
      case _ => // no op
    }

    return true
  }

  def main(args: Array[String]) = {
    val n :: m :: rest = readLine.split(' ').map { _.toInt }.toList
    val edges = Array.ofDim[Seq[Int]](n)

    (0 until n).map { case node => edges(node) = Seq[Int]()}

    (1 to m).map { case l =>
      val u :: v :: rest = readLine.split(' ').map { _.toInt}.toList
      edges(u - 1) = edges(u - 1) :+ (v - 1)
      edges(v - 1) = edges(v - 1) :+ (u - 1)
    }

    val color = Array.ofDim[Int](n)

    val result = (0 until n).map { case node =>
      if (color(node) == 0) mark(edges, color, node, 1)
    }
    .exists(_ == false)

    if (result) {
      println(-1)
    } else {
      println(color.count(_ == 1))
      println(color
        .zipWithIndex
        .filter{ case(c, n) => c == 1 }
        .map{ case(_, n) => n + 1 }
        .mkString(" "))
      println(color.count(_ == 2))
      println(color
        .zipWithIndex
        .filter{ case(c, n) => c == 2 }
        .map{ case(_, n) => n + 1}
        .mkString(" "))
    }
  }
}

object A687 {
  def mark(
    edges: Array[Seq[Int]],
    color: Array[Int],
    now: Int,
    colorToAssign: Int
  ): Boolean = {
    //println(s"Coloring $now the color $colorToAssign")
    color(now) = colorToAssign

    val nextColor = 3 - colorToAssign

    edges(now).map {
      case next if (color(next) == 0) =>
        if (!mark(edges, color, next, nextColor)) return false
      case next if (color(next) != nextColor) =>
        return false
      case _ => // no op
    }

    return true
  }

  def main(args: Array[String]) = {
    val n :: m :: rest = readLine.split(' ').map { _.toInt }.toList
    val edges = Array.ofDim[Seq[Int]](n)

    (0 until n).map { case node => edges(node) = Seq[Int]()}

    (1 to m).map { case l =>
      val u :: v :: rest = readLine.split(' ').map { _.toInt}.toList
      edges(u - 1) = edges(u - 1) :+ (v - 1)
      edges(v - 1) = edges(v - 1) :+ (u - 1)
    }

    val color = Array.ofDim[Int](n)

    val result = (0 until n).map { case node =>
      if (color(node) == 0) mark(edges, color, node, 1)
    }
    .exists(_ == false)

    if (result) {
      println(-1)
    } else {
      val (left, right) = color.zipWithIndex.partition { case (c, i) => c == 1 }
      println(left.length)
      println(left.map { case (_, i) => i + 1}.mkString(" "))
      println(right.length)
      println(right.map { case (_, i) => i + 1}.mkString(" "))
    }
  }
}

object problem_C{
  def main(args: Array[String]) {
    val scanner = new java.util.Scanner(System.in)
    val n = scanner.nextInt()
    val m = scanner.nextInt()

    val colors: Array[Int] = new Array[Int](n + 1)
    val set = Array.fill[collection.mutable.Set[Int]](n + 1)(collection.mutable.Set())

    1 to m foreach {i =>
      val i = scanner.nextInt; val j = scanner.nextInt
      set(i).add(j)
      set(j).add(i)
    }

    var color = 1
    def dfs(v: Int): Boolean ={
      if (colors(v) != 0) {
        //println("vert" + v + " (" + colors(v) + "," + color +")")
        colors(v) == color
      }
      else {
        colors(v) = color
        color = if (color == 1) 2 else 1
        set(v).forall(dfs)
      }
    }

    def kek(): Boolean = {
      for(i <- 1 to n){
        if(colors(i) == 0)
          if(!dfs(i)) return  false
      }

      true
    }

    if (!kek()) println(-1)
    else {
      val t1 = colors.count(x => x == 1)
      println(t1)
      for(i <- 1 to n){
        if(colors(i) == 1) print(i + " ")
      }
      println()
      val t2 = colors.count(x => x == 2)
      println(t2)
      for(i <- 1 to n){
        if(colors(i) == 2) print(i + " ")
      }
    }

   // println(colors)
  }
}

import scala.collection.mutable

object problem_C_2{
  class Graph {
    var g: Array[mutable.Set[Int]] = Array()

    def DFS: Option[mutable.Map[Int, Int]] = {

      val colors = mutable.Map[Int, Int]()
      def nextColor(color: Int) = if (color == 1) 2 else 1
      def dfs(color: Int, v: Int): Boolean = {
        if (colors.get(v).isDefined) {
          colors(v) == color
        }
        else {
          colors += (v -> color)
          g(v).forall(dfs(nextColor(color),_))
        }
      }

      if ((1 to g.length - 1) forall { i => if (colors.get(i).isEmpty) dfs(1,i) else true }) {
        Some(colors)
      }
      else{
        None
      }

    }

    def toCF(ans: Option[mutable.Map[Int, Int]]) = ans match {
      case None => "-1"
      case Some(map) =>

        if (g.length > 50000) println("i'm here1")

        val (list1t, list2t) = map.toList.partition(_._2 == 1)
        val list1 = list1t.map(_._1)
        val list2 = list2t.map(_._1)

        val str = new StringBuffer("")

        if (g.length > 50000) println("i'm here2")

        str.append(list1.length).append("\n")
        list1.foreach(str.append(_).append(" "))
        str.append("\n").append(list2.length).append("\n")
        list2.foreach(str.append(_).append(" "))

        if (g.length > 50000) println("i'm here3")

        str.toString
    }

  }

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

    val scanner = new java.util.Scanner(System.in)
    val n = scanner.nextInt()
    val m = scanner.nextInt()

    val adjacencyList = Array.fill[mutable.Set[Int]](n + 1)(mutable.Set())
    0 to m-1 foreach {t =>
      val i = scanner.nextInt; val j = scanner.nextInt
      adjacencyList(i).add(j)
      adjacencyList(j).add(i)
    }

    val graph = new Graph
    graph.g = adjacencyList

    println(graph.toCF(graph.DFS))

  }

}

import scala.collection.mutable.ArrayBuffer

object NPHardProblem {
  def main(args: Array[String]): Unit = {
    val in =scala.io.StdIn
    val arr=in.readLine().split(" ").map(_.toInt)
    val n=arr(0)
    val m=arr(1)
    val graph=Array.fill[Int](n,n)(0)
    for(i<- 0 until m){
      val edges=in.readLine().split(" ").map(_.toInt-1)
      graph(edges(0))(edges(1))=1
    }
    isBipartiteGraph(graph)
  }
  def isBipartiteGraph(graph: Array[Array[Int]]): Unit = {

    //The idea is to color the graph vertices in two color and two adjacent vertex can't have same color
    //Using DFS we will color the graph; if a vertex is having no color (0) then make it 1 and processed further
    //If we come across a vertex with ambiguity then we can say Bi-Partition is not possible
    val color = Array.fill[Int](graph.length)(0)
    for (i <- 0 until graph.length) {
      if (color(i) == 0) {
        color(i) = 1
      }
      if (!dfsToColorGraph(graph, i, color)) {
        println("-1")
        return
      }
    }

    var set1Count=0
    var set2Count=0
    var set1=ArrayBuffer[Int]()
    var set2=ArrayBuffer[Int]()
    for(i<- 0 until graph.length){
      if(color(i)==1){
        set1+=(i+1)
        set1Count+=1
      }else if(color(i)==2){
        set2+=(i+1)
        set2Count+=1
      }
    }
    println(set2Count)
    println(set2.mkString(" "))
    println(set1Count)
    println(set1.mkString(" "))
  }

  def dfsToColorGraph(graph: Array[Array[Int]], n: Int, color: Array[Int]): Boolean = {

    for (i <- 0 until graph.length if (graph(n)(i) == 1)) {
      if (color(i) == 0) {
        color(i) = (if (color(n) == 1) 2 else 1)
        if (!dfsToColorGraph(graph, i, color)) {
          return false
        }
      } else {
        if (color(n) == color(i)) {
          return false
        }
      }
    }

    return true
  }
}

import scala.collection.mutable.ArrayBuffer

object NPHardProblem {
  def main(args: Array[String]): Unit = {
    val in =scala.io.StdIn
    val arr=in.readLine().split(" ").map(_.toInt)
    val n=arr(0)
    val m=arr(1)
    val graph=Array.fill[Int](n,n)(0)
    for(i<- 0 until m){
      val edges=in.readLine().split(" ").map(_.toInt-1)
      graph(edges(0))(edges(1))=1
      graph(edges(1))(edges(0))=1
    }
    isBipartiteGraph(graph)
  }
  def isBipartiteGraph(graph: Array[Array[Int]]): Unit = {

    //The idea is to color the graph vertices in two color and two adjacent vertex can't have same color
    //Using DFS we will color the graph; if a vertex is having no color (0) then make it 1 and processed further
    //If we come across a vertex with ambiguity then we can say Bi-Partition is not possible
    val color = Array.fill[Int](graph.length)(0)
    for (i <- 0 until 2) {
      if (color(i) == 0) {
        color(i) = 1
      }
      if (!dfsToColorGraph(graph, i, color)) {
        println("-1")
        println(color.mkString(", "))
        return
      }
    }

    var set1Count=0
    var set2Count=0
    var set1=ArrayBuffer[Int]()
    var set2=ArrayBuffer[Int]()
    for(i<- 0 until graph.length){
      if(color(i)==1){
        set1+=(i+1)
        set1Count+=1
      }else if(color(i)==2){
        set2+=(i+1)
        set2Count+=1
      }
    }
    println(set2Count)
    println(set2.mkString(" "))
    println(set1Count)
    println(set1.mkString(" "))
  }

  def dfsToColorGraph(graph: Array[Array[Int]], n: Int, color: Array[Int]): Boolean = {

    for (i <- 0 until graph.length if (graph(n)(i) == 1)) {
      if (color(i) == 0) {
        color(i) = (if (color(n) == 1) 2 else 1)
        if (!dfsToColorGraph(graph, i, color)) {
          return false
        }
      } else {
        if (color(n) == color(i)) {
          return false
        }
      }
    }

    return true
  }
}

import scala.collection.mutable.ArrayBuffer

object NPHardProblem {
  def main(args: Array[String]): Unit = {
    val in =scala.io.StdIn
    val arr=in.readLine().split(" ").map(_.toInt)
    val n=arr(0)
    val m=arr(1)
    val graph=Array.fill[Int](n,n)(0)
    for(i<- 0 until m){
      val edges=in.readLine().split(" ").map(_.toInt-1)
      graph(edges(0))(edges(1))=1
    }
    isBipartiteGraph(graph)
  }
  def isBipartiteGraph(graph: Array[Array[Int]]): Unit = {

    //The idea is to color the graph vertices in two color and two adjacent vertex can't have same color
    //Using DFS we will color the graph; if a vertex is having no color (0) then make it 1 and processed further
    //If we come across a vertex with ambiguity then we can say Bi-Partition is not possible
    val color = Array.fill[Int](graph.length)(0)
    for (i <- 0 until 2) {
      if (color(i) == 0) {
        color(i) = 1
      }
      if (!dfsToColorGraph(graph, i, color)) {
        println("-1")
        return
      }
    }

    var set1Count=0
    var set2Count=0
    var set1=ArrayBuffer[Int]()
    var set2=ArrayBuffer[Int]()
    for(i<- 0 until graph.length){
      if(color(i)==1){
        set1+=(i+1)
        set1Count+=1
      }else if(color(i)==2){
        set2+=(i+1)
        set2Count+=1
      }
    }
    println(set2Count)
    println(set2.mkString(" "))
    println(set1Count)
    println(set1.mkString(" "))
  }

  def dfsToColorGraph(graph: Array[Array[Int]], n: Int, color: Array[Int]): Boolean = {

    for (i <- 0 until graph.length if (graph(n)(i) == 1)) {
      if (color(i) == 0) {
        color(i) = (if (color(n) == 1) 2 else 1)
        if (!dfsToColorGraph(graph, i, color)) {
          return false
        }
      } else {
        if (color(n) == color(i)) {
          return false
        }
      }
    }

    return true
  }
}

import scala.collection.mutable.ArrayBuffer

//https://codeforces.com/problemset/problem/687/A
object NPHardProblem {
  def main(args: Array[String]): Unit = {
    val in =scala.io.StdIn
    val arr=in.readLine().split(" ").map(_.toInt)
    val n=arr(0)
    val m=arr(1)
    val graph=Array.fill[ArrayBuffer[Int]](n)(new ArrayBuffer[Int]())
    for(i<- 0 until m){
      val edges=in.readLine().split(" ").map(_.toInt-1)
      graph(edges(0))+=(edges(1))
      graph(edges(1))+=(edges(0))
    }
    isBipartiteGraph(graph,n)
  }
  def isBipartiteGraph(graph: Array[ArrayBuffer[Int]],n:Int): Unit = {

    //The idea is to color the graph vertices in two color and two adjacent vertex can't have same color
    //Using DFS we will color the graph; if a vertex is having no color (0) then make it 1 and processed further
    //If we come across a vertex with ambiguity then we can say Bi-Partition is not possible
    val color = Array.fill[Int](n)(0)
    for (i <- 0 until 2) {
      if (color(i) == 0) {
        color(i) = 1
      }
      if (!dfsToColorGraph(graph, i, color)) {
        println("-1")
        return
      }
    }

    var set1Count=0
    var set2Count=0
    var set1=ArrayBuffer[Int]()
    var set2=ArrayBuffer[Int]()
    for(i<- 0 until graph.length){
      if(color(i)==1){
        set1+=(i+1)
        set1Count+=1
      }else if(color(i)==2){
        set2+=(i+1)
        set2Count+=1
      }
    }
    println(set2Count)
    println(set2.mkString(" "))
    println(set1Count)
    println(set1.mkString(" "))
  }

  def dfsToColorGraph(graph: Array[ArrayBuffer[Int]], u: Int, color: Array[Int]): Boolean = {

    for (v <- 0 until graph(u).length) {
      if (color(graph(u)(v)) == 0) {
        color(graph(u)(v)) = (if (color(u) == 1) 2 else 1)
        if (!dfsToColorGraph(graph, graph(u)(v), color)) {
          return false
        }
      } else {
        if (color(u) == color(graph(u)(v))) {
          return false
        }
      }
    }

    return true
  }
}

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

import scala.collection.mutable.ListBuffer
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 C360A(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 C360A(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

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

    g = Array.fill[ListBuffer[Int]](n)(new ListBuffer[Int])

    REP(m) { _ =>
      val u = ni()
      val v = ni()

      g(u-1) += v-1
      g(v-1) += u-1
    }

    marked = Array.fill(n)(-1)
    REP(n) { i =>
      if(marked(i) == -1) {
        marked(i) = 0
        fill(i)
      }
    }

    var yes = true
    REP(n) { i =>
      val c = marked(i)
      yes = g(i).forall(p => marked(p) == (c^1))
    }
    if(!yes) {
      out.println(-1)
    } else {
      val x = marked.zipWithIndex.filter(_._1 == 0).map(_._2 + 1)
      out.println(x.length)
      out.println(x.mkString(" "))
      val y = marked.zipWithIndex.filter(_._1 == 1).map(_._2 + 1)
      out.println(y.length)
      out.println(y.mkString(" "))
    }
  }

  var g: Array[ListBuffer[Int]] = _
  var marked: Array[Int] = _

  def fill(u: Int) {
    val x = marked(u)
    g(u).foreach { f =>
      if(marked(f) == -1) {
        marked(f) = x^1
        fill(f)
      }
    }
  }
}

object A687 {
  def mark(
    edges: Array[Seq[Int]],
    color: Array[Int],
    now: Int,
    colorToAssign: Int
  ): Boolean = {
    //println(s"Coloring $now the color $colorToAssign")
    color(now) = colorToAssign

    val nextColor = 3 - colorToAssign

    edges(now).map {
      case next if (color(next) == 0) =>
        if (!mark(edges, color, next, nextColor)) return false
      case next if (color(next) != nextColor) =>
        return false
      case _ => // no op
    }

    return true
  }

  def main(args: Array[String]) = {
    val n :: m :: rest = readLine.split(' ').map { _.toInt }.toList
    val edges = Array.ofDim[Seq[Int]](n)

    (0 until n).map { case node => edges(node) = Seq[Int]()}

    (1 to m).map { case l =>
      val u :: v :: rest = readLine.split(' ').map { _.toInt}.toList
      edges(u - 1) = edges(u - 1) :+ (v - 1)
      edges(v - 1) = edges(v - 1) :+ (u - 1)
    }

    val color = Array.ofDim[Int](n)

    val result = (0 until n).map { case node =>
      if (color(node) == 0) mark(edges, color, node, 1)
    }
    .exists(_ == false)

    if (result) {
      println(-1)
    } else {
      println(color.count(_ == 1))
      println(color.filter(_ == 1).mkString(" "))
      println(color.count(_ == 2))
      println(color.filter(_ == 2).mkString(" "))
    }
  }
}


There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 ≤ i < n + m) such that positions with indexes i and i + 1 contain children of different genders (position i has a girl and position i + 1 has a boy or vice versa) must be as large as possible. Help the children and tell them how to form the line.

The single line of the input contains two integers n and m (1 ≤ n, m ≤ 100), separated by a space.

Print a line of n + m characters. Print on the i-th position of the line character "B", if the i-th position of your arrangement should have a boy and "G", if it should have a girl.  Of course, the number of characters "B" should equal n and the number of characters "G" should equal m. If there are multiple optimal solutions, print any of them.

NoteIn the first sample another possible answer is BGBGBG. In the second sample answer BBGBGB is also optimal.

import scala.io.Source

object Solution extends App {
  val in = Source.fromFile("input.txt").getLines()
  val Array(b, g) = in.next().split(' ').map(_.toInt)
  val p = new java.io.PrintWriter("output.txt")
  if (b > g)
    p.println("BG" * Math.min(b, g) + "B" * (b - g))
  else
    p.println("GB" * Math.min(b, g) + "G" * (g - b))
  p.close()
}

import java.util.Scanner
import java.io._

object A {
    def main(args: Array[String]) {
      val sc: Scanner = new Scanner(new File("input.txt"))
      val n: Int = sc.nextInt()
      val m: Int = sc.nextInt()
      var b: Boolean = true
      if (n<m)b=false
      var np: Int = 0
      var mp: Int = 0
      var s:String = ""
      for (i <- 0 until n+m) {
        if (i % 2 == 0 && b && np < n) {s=s+"B";np=np+1}
        else if (i % 2 == 0 && mp < m) {s=s+"G";mp=mp+1}
        else if (i % 2 !=0 && b && mp < m) {s=s+"G";mp=mp+1}
        else if (np < n ) {s=s+"B";np=np+1}
        else if (mp < m ) {s=s+"G";mp=mp+1}
      }
      val bw: BufferedWriter = new BufferedWriter(new FileWriter("output.txt"))
      bw.write(s)
      bw.close()
    }
}

import scala.io.Source
import java.io.PrintWriter
import java.io.File

object BoysGirls extends App {
    val it = Source.fromFile("input.txt").getLines
    val writer = new PrintWriter(new File("output.txt" ))
    val ss = it.next.split(" ")
    val iss = ss.map(_.toInt)
    val nb = iss(0)
    val ng = iss(1)

    def printc(s:String, n:Int) =
      for (i <- 0 until n) {
        writer.print(s)

      }

    // println("nb,ng = " + nb + " " + ng)
    if (nb == ng) printc("GB", ng)
	else if (nb > ng) {
		printc( "BG", ng);
		printc( "B", (nb-ng))
	} else {
		printc( "GB", nb)
		printc( "G", (ng-nb) )
	}
    writer.close()
}

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

object test2 extends App {
	val sc=new Scanner(new File("input.txt"))
	val out=new PrintWriter("output.txt")
	val m=sc.nextInt
	val n=sc.nextInt
	if(m<=n)
		out.println("GB"*m+"G"*(n-m))
	else
        out.println("BG"*n+"B"*(m-n))
    out.close
}

import java.util.Scanner
import java.io._

object A {
    def main(args: Array[String]) {
      val sc: Scanner = new Scanner(new File("input.txt"))
      val n: Int = sc.nextInt()
      val m: Int = sc.nextInt()
      var b: Boolean = true
      if (n<m)b=false
      var np: Int = 0
      var mp: Int = 0
      var s:String = ""
      for (i <- 0 until n+m) {
        if (i % 2 == 0 && b && np < n) {s+"B";np=np+1}
        else if (i % 2 == 0 && mp < m) {s+"G";mp=mp+1}
        else if (i % 2 !=0 && b && mp < m) {s+"G";mp=mp+1}
        else if (np < n ) {s+"B";np=np+1}
      }
      val bw: BufferedWriter = new BufferedWriter(new FileWriter("output.txt"))
      bw.write(s)
      bw.close()
    }
}

import java.util.Scanner
import java.io._

object A {
    def main(args: Array[String]) {
      val sc: Scanner = new Scanner(new File("input.txt"))
      val n: Int = sc.nextInt()
      val m: Int = sc.nextInt()
      var b: Boolean = true
      if (n<m)b=false
      var np: Int = 0
      var mp: Int = 0
      var s:String = ""
      for (i <- 0 until n+m) {
        if (i % 2 == 0 && b && np < n) {s=s+"B";np=np+1}
        else if (i % 2 == 0 && mp < m) {s=s+"G";mp=mp+1}
        else if (i % 2 !=0 && b && mp < m) {s=s+"G";mp=mp+1}
        else if (np < n ) {s=s+"B";np=np+1}
      }
      val bw: BufferedWriter = new BufferedWriter(new FileWriter("output.txt"))
      bw.write(s)
      bw.close()
    }
}

import java.util.Scanner
import java.io._

object A {
    def main(args: Array[String]) {
      val sc: Scanner = new Scanner(new File("input.txt"))
      val n: Int = sc.nextInt()
      val m: Int = sc.nextInt()
      var b: Boolean = true
      if (n<m)b=false
      var np: Int = 0
      var mp: Int = 0
      var s:String = ""
      for (i <- 0 until n+m) {
        if (i % 2 == 0 && b && np < n) {s+"B";np=np+1}
        else if (i % 2 == 0 && mp < m) {s+"G";mp=mp+1}
        else if (i % 2 !=0 && b && mp < m) {s+"G";mp=mp+1}
        else if (np < n ) {s+"B";np=np+1}
      }
      val bw: BufferedWriter = new BufferedWriter(new FileWriter("output.txt"))
      bw.write(s+"\n")
      bw.close()
    }
}

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

object test2 extends App {
	val sc=new Scanner(new File("input.txt"))
	val out=new PrintWriter("output.txt")
	val m=sc.nextInt
	val n=sc.nextInt
	if(m<=n)
		out.println("GB"*m+"B"*(n-m))
	else
        out.println("BG"*n+"G"*(m-n))
    out.close
}

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

object test2 extends App {
	val sc=new Scanner(new File("input.txt"))
	val out=new PrintWriter(new File("output.txt"))
	val m=sc.nextInt
	val n=sc.nextInt
	if(m<=n)
		print("BG"*m+"B"*(n-m))
	else
        print("GB"*n+"G"*(m-n))
    out.close
}

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

object test2 extends App {
	val sc=new Scanner(new File("input.txt"))
	val out=new PrintWriter("output.txt")
	val m=sc.nextInt
	val n=sc.nextInt
	if(m<=n)
		println("BG"*m+"B"*(n-m))
	else
        println("GB"*n+"G"*(m-n))
    out.close
}

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

object test2 extends App {
	val sc=new Scanner(new File("input.txt"))
	val out=new PrintWriter("output.txt")
	val m=sc.nextInt
	val n=sc.nextInt
	if(m<=n)
		out.println("BG"*m+"B"*(n-m))
	else
        out.println("GB"*n+"G"*(m-n))
    out.close
}

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

object test2 extends App {
	val sc=new Scanner(new File("input.txt"))
	val out=new PrintWriter("output.txt")
	val m=sc.nextInt
	val n=sc.nextInt
	if(m<=n)
		print("BG"*m+"B"*(n-m))
	else
        print("GB"*n+"G"*(m-n))
    out.close
}

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

object test2 extends App {
	val sc=new Scanner(new File("input.txt"))
	val out=new PrintWriter("output.txt")
	val m=sc.nextInt
	val n=sc.nextInt
	if(m<=n)
		println("BG"*m+"B"*(n-m))
	else
        println("GB"*n+"G"*(m-n))
}