Vasya has an array $$$a_1, a_2, \dots, a_n$$$.You don't know this array, but he told you $$$m$$$ facts about this array. The $$$i$$$-th fact is a triple of numbers $$$t_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$0 \le t_i \le 1, 1 \le l_i < r_i \le n$$$) and it means:  if $$$t_i=1$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}$$$ is sorted in non-decreasing order;  if $$$t_i=0$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}$$$ is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter. For example if $$$a = [2, 1, 1, 3, 2]$$$ then he could give you three facts: $$$t_1=1, l_1=2, r_1=4$$$ (the subarray $$$[a_2, a_3, a_4] = [1, 1, 3]$$$ is sorted), $$$t_2=0, l_2=4, r_2=5$$$ (the subarray $$$[a_4, a_5] = [3, 2]$$$ is not sorted), and $$$t_3=0, l_3=3, r_3=5$$$ (the subarray $$$[a_3, a_5] = [1, 3, 2]$$$ is not sorted).You don't know the array $$$a$$$. Find any array which satisfies all the given facts.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 1000, 1 \le m \le 1000$$$). Each of the next $$$m$$$ lines contains three integers $$$t_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$0 \le t_i \le 1, 1 \le l_i < r_i \le n$$$). If $$$t_i = 1$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}$$$ is sorted. Otherwise (if $$$t_i = 0$$$) subbarray $$$a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}$$$ is not sorted.

If there is no array that satisfies these facts in only line print NO (in any letter case). If there is a solution, print YES (in any letter case). In second line print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$, satisfying all the given facts. If there are multiple satisfying arrays you can print any of them.

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

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

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

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

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

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

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

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

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

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

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

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

  case class Range(l: Int, r: Int) {
    def contains(rg: Range): Boolean = l <= rg.l && rg.r <= r
  }

  def solve(): Unit = {
    import java.util.{Collections, Comparator}
    val N, M = ni()
    val sorted, unsorted, mergedSorted = new java.util.ArrayList[Range]
    REP(M) { _ =>
      val t, l, r = ni()
      if (t == 1) {
        sorted.add(Range(l, r))
      } else {
        unsorted.add(Range(l, r))
      }
    }

    Collections.sort(sorted, new Comparator[Range] {
      override def compare(o1: Range, o2: Range): Int = {
        Integer.compare(o1.l, o2.l)
      }
    })

    var l = 0
    var r = 0
    REP(sorted.size()) { i =>
      val rg = sorted.get(i)
      if (rg.l <= r) {
        r = max(r, rg.r)
      } else {
        if (l != 0) mergedSorted.add(Range(l, r))
        l = rg.l
        r = rg.r
      }
    }
    if (l != 0) mergedSorted.add(Range(l, r))
    debug(mergedSorted.toArray.mkString(" "))

    val ans = Array.ofDim[Int](N)
    var p = 0
    REP_r(N) { i =>
      p += 1
      ans(i) = p
    }
    REP_r(mergedSorted.size) { i =>
      val rg = mergedSorted.get(i)
      var num = ans(rg.r - 1)
      TO(rg.l, rg.r) { j =>
        ans(j - 1) = num
        num += 1
      }
    }

    def test: Boolean = {
      REP(sorted.size) { i =>
        val rg = sorted.get(i)
        var found = false
        TO(rg.l, rg.r - 1) { j =>
          if (j > rg.l - 1 && ans(j) < ans(j-1)) found = true
        }
        debug(s"found:$found")
        if (found) return false
      }
      REP(unsorted.size) { i =>
        val rg = unsorted.get(i)
        var found = false
        TO(rg.l, rg.r - 1) { j =>
          if (j > rg.l - 1 && ans(j) < ans(j-1)) found = true
        }
        if (!found) return false
      }
      true
    }

    debug(ans)
    if (!test) out.println("NO")
    else {
      out.println("YES")
      out.println(ans.mkString(" "))
    }

  }
}

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

object C_1187_editorial extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val A         = Array.fill[Int](n + 1)(1)
  val notSorted = mutable.ArrayBuffer[(Int, Int)]()
  val sorted    = mutable.ArrayBuffer[(Int, Int)]()

  1 to m foreach { _ =>
    val Array(t, l, r) = readLine().split(" ").map(_.toInt)

    if (t == 1) {
      sorted += ((l, r))
      (l until r).foreach { i =>
        A(i) = 0
      }
    } else {
      notSorted += ((l, r))
      (l until r).foreach { i =>
        if (A(i) == 1) A(i) = -1
      }
    }
  }

  def isSorted: Boolean = {
    sorted.foreach {
      case (l, r) =>
        (l + 1 to r).foreach { i =>
          if (A(i - 1) > A(i)) {
            return false
          }
        }
    }
    true
  }

  def isNotSorted: Boolean = {
    notSorted.foreach {
      case (l, r) =>
        val allSorted = (l + 1 to r).forall { i =>
          A(i - 1) <= A(i)
        }
        if (allSorted) return false
    }
    true
  }

  var delta = A(1)
  A(1) = 1000000000 / 2

  (2 to n).foreach { i =>
    if (A(i) == 1) A(i) = -1
    val newDelta = A(i)
    A(i) = A(i - 1) + delta
    delta = newDelta
  }

//  println("YES")
//  println(A.tail.mkString(" "))

  if (isSorted && isNotSorted) {
    println("YES")
    println(A.tail.mkString(" "))
  } else {
    println("NO")
  }

}

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

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

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

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

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

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

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

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

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

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

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

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

  case class Range(l: Int, r: Int) {
    def contains(rg: Range): Boolean = l <= rg.l && rg.r <= r
  }

  def solve(): Unit = {
    import java.util.{Collections, Comparator}
    val N, M = ni()
    val sorted, unsorted, mergedSorted = new java.util.ArrayList[Range]
    REP(M) { _ =>
      val t, l, r = ni()
      if (t == 1) {
        sorted.add(Range(l, r))
      } else {
        unsorted.add(Range(l, r))
      }
    }

    Collections.sort(sorted, new Comparator[Range] {
      override def compare(o1: Range, o2: Range): Int = {
        Integer.compare(o1.l, o2.l)
      }
    })

    var l = 0
    var r = 0
    REP(sorted.size()) { i =>
      val rg = sorted.get(i)
      if (rg.l <= r) {
        r = rg.r
      } else {
        if (l != 0) mergedSorted.add(Range(l, r))
        l = rg.l
        r = rg.r
      }
    }
    if (l != 0) mergedSorted.add(Range(l, r))
    debug(mergedSorted.toArray.mkString(" "))

    def test: Boolean = {
      REP(unsorted.size()) { i =>
        REP(mergedSorted.size()) { j =>
          if (mergedSorted.get(j).contains(unsorted.get(i))) return false
        }
      }
      true
    }

    if (!test) out.println("NO")
    else {
      out.println("YES")
      val ans = Array.ofDim[Int](N)
      var p = 0
      REP_r(N) { i =>
        p += 1
        ans(i) = p
      }
      REP_r(mergedSorted.size) { i =>
        val rg = mergedSorted.get(i)
        TO(rg.l, rg.r) { j =>
          ans(j - 1) = ans(rg.r - 1)
        }
      }
      out.println(ans.mkString(" "))
    }

  }
}

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

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

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

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

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

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

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

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

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

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

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

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

  case class Range(l: Int, r: Int) {
    def contains(rg: Range): Boolean = l <= rg.l && rg.r <= r
  }

  def solve(): Unit = {
    import java.util.{Collections, Comparator}
    val N, M = ni()
    val sorted, unsorted, mergedSorted = new java.util.ArrayList[Range]
    REP(M) { _ =>
      val t, l, r = ni()
      if (t == 1) {
        sorted.add(Range(l, r))
      } else {
        unsorted.add(Range(l, r))
      }
    }

    Collections.sort(sorted, new Comparator[Range] {
      override def compare(o1: Range, o2: Range): Int = {
        Integer.compare(o1.l, o2.l)
      }
    })

    var l = 0
    var r = 0
    REP(sorted.size()) { i =>
      val rg = sorted.get(i)
      if (rg.l <= r) {
        r = rg.r
      } else {
        if (l != 0) mergedSorted.add(Range(l, r))
        l = rg.l
        r = rg.r
      }
    }
    if (l != 0) mergedSorted.add(Range(l, r))
    debug(mergedSorted.toArray.mkString(" "))

    val ans = Array.ofDim[Int](N)
    var p = 0
    REP_r(N) { i =>
      p += 1
      ans(i) = p
    }
    REP_r(mergedSorted.size) { i =>
      val rg = mergedSorted.get(i)
      var num = ans(rg.r - 1)
      TO(rg.l, rg.r) { j =>
        ans(j - 1) = num
        num += 1
      }
    }

    def test: Boolean = {
      REP(sorted.size) { i =>
        val rg = sorted.get(i)
        var found = false
        TO(rg.l, rg.r - 1) { j =>
          if (j > rg.l - 1 && ans(j) < ans(j-1)) found = true
        }
        debug(s"found:$found")
        if (found) return false
      }
      REP(unsorted.size) { i =>
        val rg = unsorted.get(i)
        var found = false
        TO(rg.l, rg.r - 1) { j =>
          if (j > rg.l - 1 && ans(j) < ans(j-1)) found = true
        }
        if (!found) return false
      }
      true
    }

    debug(ans)
    if (!test) out.println("NO")
    else {
      out.println("YES")
      out.println(ans.mkString(" "))
    }

  }
}

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

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

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

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

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

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

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

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

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

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

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

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

  case class Range(l: Int, r: Int) {
    def contains(rg: Range): Boolean = l <= rg.l && rg.r <= r
  }

  def solve(): Unit = {
    import java.util.{Collections, Comparator}
    val N, M = ni()
    val sorted, unsorted, mergedSorted = new java.util.ArrayList[Range]
    REP(M) { _ =>
      val t, l, r = ni()
      if (t == 1) {
        sorted.add(Range(l, r))
      } else {
        unsorted.add(Range(l, r))
      }
    }

    Collections.sort(sorted, new Comparator[Range] {
      override def compare(o1: Range, o2: Range): Int = {
        Integer.compare(o1.l, o2.l)
      }
    })

    var l = 0
    var r = 0
    REP(sorted.size()) { i =>
      val rg = sorted.get(i)
      if (rg.l <= r) {
        r = rg.r
      } else {
        if (l != 0) mergedSorted.add(Range(l, r))
        l = rg.l
        r = rg.r
      }
    }
    if (l != 0) mergedSorted.add(Range(l, r))
    debug(mergedSorted.toArray.mkString(" "))

    def test: Boolean = {
      REP(unsorted.size()) { i =>
        REP(mergedSorted.size()) { j =>
          if (mergedSorted.get(j).contains(unsorted.get(i))) return false
        }
      }
      true
    }

    if (!test) out.println("NO")
    else {
      out.println("YES")
      val ans = Array.ofDim[Int](N)
      var p = 0
      REP_r(mergedSorted.size) { i =>
        p += 1
        val rg = mergedSorted.get(i)
        TO(rg.l, rg.r) { j =>
          ans(j - 1) = p
        }
      }
      REP_r(N) { i =>
        if (ans(i) == 0) {
          p += 1
          ans(i) = p
        }
      }
      out.println(ans.mkString(" "))
    }

  }
}

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

object C_1187 extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  var sorted   = mutable.ArrayBuffer[(Int, Int)]()
  var unsorted = mutable.ArrayBuffer[(Int, Int)]().sortBy(_._1)

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

  1 to m foreach { _ =>
    val Array(t, l, r) = readLine().split(" ").map(_.toInt)
    if (t == 1) sorted.append((l, r))
    else unsorted.append((l, r))
  }

  var counter    = 1
  var lastSorted = counter - 1

  sorted.foreach {
    case (l, r) =>
      l to r foreach { i =>
        answer(i - 1) = counter
        lastSorted = counter
        counter += 1
      }
  }

  (0 until n).reverse foreach { i =>
    if (answer(i) == 0) {
      answer(i) = counter
      counter += 1
    }
  }

  unsorted.foreach {
    case (l, r) => {
      l to r foreach { i =>
        answer(i - 1) = counter
        counter += 1
      }

      if (answer(l - 1) < answer(r - 1)) {
        val tmp = answer(l - 1)
        answer(l - 1) = answer(r - 1)
        answer(r - 1) = tmp
      } else {
        // cannot fix or it's fixed
      }
    }
  }

  val isSorted = {
    sorted.forall {
      case (l, r) =>
        l until r forall { i =>
          answer(i - 1) <= answer(i)
        }
    }
  }

  if (isSorted) {
    println("YES")
    println(answer.mkString(" "))
  } else {
    println("NO")
  }
}

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

object C_1187 extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  var sorted   = mutable.ArrayBuffer[(Int, Int)]()
  var unsorted = mutable.ArrayBuffer[(Int, Int)]().sortBy(_._1)

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

  1 to m foreach { _ =>
    val Array(t, l, r) = readLine().split(" ").map(_.toInt)
    if (t == 1) sorted.append((l, r))
    else unsorted.append((l, r))
  }

  var counter    = 1
  var lastSorted = counter - 1

  sorted.foreach {
    case (l, r) =>
      l to r foreach { i =>
        answer(i - 1) = counter
        lastSorted = counter
        counter += 1
      }
  }

  (0 until n).reverse foreach { i =>
    if (answer(i) == 0) {
      answer(i) = counter
      counter += 1
    }
  }

  unsorted.foreach {
    case (l, r) => {
      l to r foreach { i =>
        answer(i - 1) = counter
        counter += 1
      }

      if (answer(l - 1) < answer(r - 1)) {
        val tmp = answer(l - 1)
        answer(l - 1) = answer(r - 1)
        answer(r - 1) = tmp
      } else {
        // cannot fix or it's fixed
      }
    }
  }

  val isSorted = {
    sorted.forall {
      case (l, r) =>
        l + 1 until r forall { i =>
          answer(i - 1) <= answer(i)
        }
    }
  }

  if (isSorted) {
    println("YES")
    println(answer.mkString(" "))
  } else {
    println("NO")
  }
}

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

object C_1187_editorial extends App {
  val Array(n, m) = readLine().split(" ").map(_.toInt)

  val A         = Array.fill[Int](n + 1)(1)
  val notSorted = mutable.ArrayBuffer[(Int, Int)]()
  val sorted    = mutable.ArrayBuffer[(Int, Int)]()

  1 to m foreach { _ =>
    val Array(t, l, r) = readLine().split(" ").map(_.toInt)

    if (t == 1) {
      sorted += ((l, r))
      (l until r).foreach { i =>
        A(i) = 0
      }
    } else {
      notSorted += ((l, r))
      (l until r).foreach { i =>
        if (A(i) == 1) A(i) = -1
      }
    }
  }

  def isSorted: Boolean = {
    sorted.foreach {
      case (l, r) =>
        (l + 1 to r).foreach { i =>
          if (A(i - 1) > A(i)) {
            return false
          }
        }
    }
    true
  }

  def isNotSorted: Boolean = {
    notSorted.foreach {
      case (l, r) =>
        val allSorted = (l + 1 to r).forall { i =>
          A(i - 1) <= A(i)
        }
        if (allSorted) return false
    }
    true
  }

  var delta = A(1)
  A(1) = 0

  (2 to n).foreach { i =>
    if (A(i) == 1) A(i) = -1
    val newDelta = A(i)
    A(i) = A(i - 1) + delta
    delta = newDelta
  }

//  println("YES")
//  println(A.tail.mkString(" "))

  if (isSorted && isNotSorted) {
    println("YES")
    println(A.tail.mkString(" "))
  } else {
    println("NO")
  }

}


Let's call a sequence of integers $$$x_1, x_2, \dots, x_k$$$ MEX-correct if for all $$$i$$$ ($$$1 \le i \le k$$$) $$$|x_i - \operatorname{MEX}(x_1, x_2, \dots, x_i)| \le 1$$$ holds. Where $$$\operatorname{MEX}(x_1, \dots, x_k)$$$ is the minimum non-negative integer that doesn't belong to the set $$$x_1, \dots, x_k$$$. For example, $$$\operatorname{MEX}(1, 0, 1, 3) = 2$$$ and $$$\operatorname{MEX}(2, 1, 5) = 0$$$.You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. Calculate the number of non-empty MEX-correct subsequences of a given array. The number of subsequences can be very large, so print it modulo $$$998244353$$$. Note: a subsequence of an array $$$a$$$ is a sequence $$$[a_{i_1}, a_{i_2}, \dots, a_{i_m}]$$$ meeting the constraints $$$1 \le i_1 < i_2 < \dots < i_m \le n$$$. If two different ways to choose the sequence of indices $$$[i_1, i_2, \dots, i_m]$$$ yield the same subsequence, the resulting subsequence should be counted twice (i. e. two subsequences are different if their sequences of indices $$$[i_1, i_2, \dots, i_m]$$$ are not the same).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$5 \cdot 10^5$$$.

For each test case, print a single integer — the number of non-empty MEX-correct subsequences of a given array, taken modulo $$$998244353$$$.

NoteIn the first example, the valid subsequences are $$$[0]$$$, $$$[1]$$$, $$$[0,1]$$$ and $$$[0,2]$$$.In the second example, the valid subsequences are $$$[0]$$$ and $$$[1]$$$.In the third example, any non-empty subsequence is valid.

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

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

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

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

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

    val t = readInt()

    for (tt <- 1 to t) {
        val n = readInt()
        val d0 = new Array[Long](n+3)
        val d1 = new Array[Long](n+3)
        var ans = 0L
        for (i <- 1 to n) {
            val a = readInt()
            if (a > 0)
                d1(a-1) = (d1(a-1)*2L+d0(a-1))%rem
            d0(a+1) = (d0(a+1)*2L+d0(a))%rem
            d1(a+1) = (d1(a+1)*2L)%rem
            if (a == 0)
                d0(1) = (d0(1)+1L) % rem
            if (a == 1)
                d1(0) = (d1(0)+1L) % rem
        }
        for (i <- 0 to n+1) {
            ans = (ans + d0(i) + d1(i)) % rem
        }
        writer.println(ans)
        /*
        for (i <- 0 to n+1)
            writer.print(d0(i) + " ")
        writer.println()
        for (i <- 0 to n+1)
            writer.print(d1(i) + " ")
        writer.println()

         */

        def check(kk: Long): Boolean = {
            return false
        }
        def bs(st:Long, en:Long): Long = {
            if (en - st <= 1L) {
                if (check(st))
                    return st
                else
                    return en
            }
            val mid = (st + en) / 2L
            if (check(mid))
                return bs(st, mid)
            else
                return bs(mid, en)
        }
    }
    writer.flush()
}

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

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

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

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

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

    val t = readInt()

    for (tt <- 1 to t) {
        val n = readInt()
        val d0 = new Array[Long](n+3)
        val d1 = new Array[Long](n+3)
        var ans = 0L
        for (i <- 1 to n) {
            val a = readInt()
            ans += d0(a) + d0(a+1) + d1(a+1)
            if (a > 0)
                ans += d0(a-1) + d1(a-1)
            if (a == 0 || a == 1)
                ans += 1
            ans %= rem
            if (a > 0)
                d1(a-1) = (d1(a-1)*2L+d0(a-1))%rem
            d0(a+1) = (d0(a+1)*2L+d0(a))%rem
            d0(a+2) = (d0(a+2)+d1(a))%rem
            d1(a+1) = (d1(a+1)*2L)%rem
            if (a == 0)
                d0(1) = (d0(1)+1L) % rem
            if (a == 1)
                d1(0) = (d1(0)+1L) % rem
        }
        writer.println(ans)
        /*
        for (i <- 0 to n+1)
            writer.print(d0(i) + " ")
        writer.println()
        for (i <- 0 to n+1)
            writer.print(d1(i) + " ")
        writer.println()

         */

        def check(kk: Long): Boolean = {
            return false
        }
        def bs(st:Long, en:Long): Long = {
            if (en - st <= 1L) {
                if (check(st))
                    return st
                else
                    return en
            }
            val mid = (st + en) / 2L
            if (check(mid))
                return bs(st, mid)
            else
                return bs(mid, en)
        }
    }
    writer.flush()
}

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

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

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

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

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

    val t = readInt()

    for (tt <- 1 to t) {
        val n = readInt()
        val d0 = new Array[Long](n+3)
        val d1 = new Array[Long](n+3)
        var ans = 0L
        for (i <- 1 to n) {
            val a = readInt()
            ans += d0(a) + d0(a+1) + d1(a+1)
            if (a > 0)
                ans += d0(a-1) + d1(a-1)
            if (a == 0 || a == 1)
                ans += 1
            ans %= rem
            if (a > 0)
                d1(a-1) = (d1(a-1)*2L+d0(a-1))%rem
            d0(a+1) = (d0(a+1)*2L+d0(a))%rem
            d0(a+2) = (d0(a+2)+d1(a))%rem
            d1(a+1) = (d1(a+1)*2L)%rem
            if (a == 0)
                d0(1) = (d0(1)+1L) % rem
            if (a == 1)
                d1(0) = (d1(0)+1L) % rem
        }
        writer.println(ans)
        for (i <- 0 to n+1)
            writer.print(d0(i) + " ")
        writer.println()
        for (i <- 0 to n+1)
            writer.print(d1(i) + " ")
        writer.println()

        def check(kk: Long): Boolean = {
            return false
        }
        def bs(st:Long, en:Long): Long = {
            if (en - st <= 1L) {
                if (check(st))
                    return st
                else
                    return en
            }
            val mid = (st + en) / 2L
            if (check(mid))
                return bs(st, mid)
            else
                return bs(mid, en)
        }
    }
    writer.flush()
}

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

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

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

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

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

    val t = readInt()

    for (tt <- 1 to t) {
        val n = readInt()
        val d0 = new Array[Long](n+2)
        val d1 = new Array[Long](n+2)
        var ans = 0L
        for (i <- 1 to n) {
            val a = readInt()
            ans += d0(a) + d0(a+1) + d1(a+1)
            if (a > 0)
                ans += d0(a-1) + d1(a-1)
            if (a == 0 || a == 1)
                ans += 1
            ans %= rem
            if (a > 0)
                d1(a-1) = (d1(a-1)*2L+d0(a-1))%rem
            d0(a+1) = (d0(a+1)*2L+d0(a))%rem
            d1(a+1) = (d1(a+1)*2L+d1(a))%rem
            if (a == 0)
                d0(1) = (d0(1)+1L) % rem
            if (a == 1)
                d1(0) = (d1(0)+1L) % rem
        }
        writer.println(ans)
        //for (i <- 0 to n+1)
        //    writer.print(d0(i) + " ")
        //writer.println()
        //for (i <- 0 to n+1)
        //    writer.print(d1(i) + " ")
        //writer.println()

        def check(kk: Long): Boolean = {
            return false
        }
        def bs(st:Long, en:Long): Long = {
            if (en - st <= 1L) {
                if (check(st))
                    return st
                else
                    return en
            }
            val mid = (st + en) / 2L
            if (check(mid))
                return bs(st, mid)
            else
                return bs(mid, en)
        }
    }
    writer.flush()
}


Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than k. Distance between two vertices of the tree is the minimum possible number of edges of the path between them.As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.

The first line of the input contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k. Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree.

Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).

NoteIn the first sample, Ostap has to paint both vertices black.In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}.

import scala.collection.mutable

object C extends App {
  val MOD = 1000000007
  def readInts = scala.io.StdIn.readLine().split(" ").map(_.toInt)

  val Array(n, k) = readInts

  def mergePattern(p1: Map[Int, Long], p2: Map[Int, Long]): Map[Int, Long] = {
    val ans = mutable.HashMap.empty[Int, Long]
    for {
      (a1, b1) <- p1
      (a2, b2) <- p2
    } {
      val (a, b) =
        if (a1 + a2 > 0) (Math.max(a1, a2), b1 * b2 % MOD)
        else (Math.min(a1, a2), b1 * b2 % MOD)
      ans.put(a, (ans.getOrElse(a, 0L) + b) % MOD)
    }
    ans.toMap
  }

  val emptyLiftedPattern = Map(0 -> 1L, k+1 -> 1L)

  case class Tree(children: Seq[Tree]) {
    val pattern: Map[Int, Long] = {
      val liftedPattern = children.map(_.pattern).foldLeft(emptyLiftedPattern)(mergePattern)
      liftedPattern.flatMap {
        case (a, b) if a >= -k => Option(a - 1 -> b)
        case _ => None
      }
    }
  }

  val q = (2 to n).flatMap { i =>
    val Array(u, v) = readInts
    Seq(u->v, v->u)
  }

  def tree(x: Int, parent: Int): Tree = Tree(
    for {
      (a, b) <- q
      if a == x
      if b != parent
    } yield tree(b, a)
  )

  val t = tree(1, 0)
  val ans = t.pattern.filter(_._1 >= 0).values.sum % MOD
  println(ans)
}

import scala.collection.mutable

object C extends App {
  val MOD = 1000000007
  def readInts = scala.io.StdIn.readLine().split(" ").map(_.toInt)

  val Array(n, k) = readInts

  def mergePattern(p1: Map[Int, Long], p2: Map[Int, Long]): Map[Int, Long] = {
    val ans = mutable.HashMap.empty[Int, Long]
    for {
      (a1, b1) <- p1
      (a2, b2) <- p2
    } {
      val (a, b) =
        if (a1 + a2 > 0) (Math.max(a1, a2), b1 * b2 % MOD)
        else (Math.min(a1, a2), b1 * b2 % MOD)
      ans.put(a, ans.getOrElse(a, 0L) + b)
    }
    ans.toMap
  }

  val emptyLiftedPattern = Map(0 -> 1L, k+1 -> 1L)

  case class Tree(children: Seq[Tree]) {
    val pattern: Map[Int, Long] = {
      val liftedPattern = children.map(_.pattern).foldLeft(emptyLiftedPattern)(mergePattern)
      liftedPattern.flatMap {
        case (a, b) if a >= -k => Option(a - 1 -> b)
        case _ => None
      }
    }
  }

  val q = (2 to n).flatMap { i =>
    val Array(u, v) = readInts
    Seq(u->v, v->u)
  }

  def tree(x: Int, parent: Int): Tree = Tree(
    for {
      (a, b) <- q
      if a == x
      if b != parent
    } yield tree(b, a)
  )

  val t = tree(1, 0)
  val ans = t.pattern.filter(_._1 >= 0).values.sum % MOD
  println(ans)
}


Vova has won $$$n$$$ trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.

The first line contains one integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of trophies. The second line contains $$$n$$$ characters, each of them is either G or S. If the $$$i$$$-th character is G, then the $$$i$$$-th trophy is a golden one, otherwise it's a silver trophy.

Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.

NoteIn the first example Vova has to swap trophies with indices $$$4$$$ and $$$10$$$. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is $$$7$$$. In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is $$$4$$$. In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than $$$0$$$.

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val G = ns(N).map(a => if (a == 'G') 1 else 0)
    val S = G.sum
    val cum = cumSum(G)
    var mx = 0
    rep(N) { i =>
      if (G(i) == 0) mx = 0
      else mx += 1
    }

    var ans = mx
    var r = 0
    rep(N) { l =>
      if (r < l) r = l
      while (r + 1 < N && cum(r + 2) - cum(l) >= (r + 1 - l + 1) - 1) r += 1
      val v = cum(r + 1) - cum(l) + 1
      if (v <= S) ans = max(ans, cum(r + 1) - cum(l) + 1)
    }
    out.println(ans)
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val C = ns(N).map(c => if (c == 'G') 1 else 0)
    val L, R = Array.ofDim[Int](N)
    val S = C.sum

    L(0) = C(0)
    rep(N - 1, 1) { i =>
      L(i) = C(i) match {
        case 0 => 0
        case 1 => L(i - 1) + 1
      }
    }

    R(N - 1) = C(N - 1)
    rep_r(N - 1) { i =>
      R(i) = C(i) match {
        case 0 => 0
        case 1 => R(i + 1) + 1
      }
    }

    var ans = L.max
    rep(N - 2, 1) { i =>
      if (C(i - 1) == 1 && C(i) == 0 && C(i + 1) == 1 && S > L(i - 1) + R(i + 1)) {
        ans = max(ans, L(i - 1) + R(i + 1) + 1)
      }
    }
    out.println(ans)
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val C = ns(N).map(c => if (c == 'G') 1 else 0)
    val L, R = Array.ofDim[Int](N)
    val S = C.sum

    L(0) = C(0)
    rep(N - 1, 1) { i =>
      L(i) = C(i) match {
        case 0 => 0
        case 1 => L(i - 1) + 1
      }
    }

    R(N - 1) = C(N - 1)
    rep_r(N - 1) { i =>
      R(i) = C(i) match {
        case 0 => 0
        case 1 => R(i + 1) + 1
      }
    }

    val mx = L.max
    var ans = if (mx == S) mx else mx + 1
    rep(N - 2, 1) { i =>
      if (C(i) == 0 && S >= L(i - 1) + R(i + 1) + 1) {
        ans = max(ans, L(i - 1) + R(i + 1) + 1)
      }
    }
    out.println(ans)
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val C = ns(N).map(c => if (c == 'G') 1 else 0)
    val L, R = Array.ofDim[Int](N)
    val S = C.sum

    L(0) = C(0)
    rep(N - 1, 1) { i =>
      L(i) = C(i) match {
        case 0 => 0
        case 1 => L(i - 1) + 1
      }
    }

    R(N - 1) = C(N - 1)
    rep_r(N - 1) { i =>
      R(i) = C(i) match {
        case 0 => 0
        case 1 => R(i + 1) + 1
      }
    }

    val mx = L.max
    var ans = if (mx == S) mx else mx + 1
    rep(N - 2, 1) { i =>
      if (C(i - 1) == 1 && C(i) == 0 && C(i + 1) == 1 && S > L(i - 1) + R(i + 1)) {
        ans = max(ans, L(i - 1) + R(i + 1) + 1)
      }
    }
    out.println(ans)
  }

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

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

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

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

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

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


Sasha grew up and went to first grade. To celebrate this event her mother bought her a multiplication table $$$M$$$ with $$$n$$$ rows and $$$n$$$ columns such that $$$M_{ij}=a_i \cdot a_j$$$ where $$$a_1, \dots, a_n$$$ is some sequence of positive integers.Of course, the girl decided to take it to school with her. But while she was having lunch, hooligan Grisha erased numbers on the main diagonal and threw away the array $$$a_1, \dots, a_n$$$. Help Sasha restore the array!

The first line contains a single integer $$$n$$$ ($$$3 \leqslant n \leqslant 10^3$$$), the size of the table.  The next $$$n$$$ lines contain $$$n$$$ integers each. The $$$j$$$-th number of the $$$i$$$-th line contains the number $$$M_{ij}$$$ ($$$1 \leq M_{ij} \leq 10^9$$$). The table has zeroes on the main diagonal, that is, $$$M_{ii}=0$$$.

In a single line print $$$n$$$ integers, the original array $$$a_1, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$). It is guaranteed that an answer exists. If there are multiple answers, print any.

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

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

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val n = ni()
    val m = nm(n, n)
    val g = gcd(m(0).drop(1))
    val d = divisors(g)
    debug(s"g:$g")
    debug(d)
    val a = Array.ofDim[Int](n)
    def showResult(): Unit = {
      out.println(a.mkString(" "))
    }

    REP(d.length) { i =>
      a(0) = d(i)
      REP(n - 1, 1) { j =>
        a(j) = m(0)(j) / a(0)
      }
      var ok = true
      REP(n - 2, 1) { j =>
        ok &&= m(j)(j + 1) == a(j).toLong * a(j + 1)
      }
      if (ok) {
        showResult()
        return
      }
    }
  }

  /**
    * O(√x)
    */
  def divisors(x: Int): Array[Int] = {
    val pre = ArrayBuffer[Int]()
    val post = ArrayBuffer[Int]()
    REP(math.sqrt(x).toInt, 1) { d =>
      if (x % d == 0) {
        pre += d
        if (d * d != x) post += x / d
      }
    }

    val res = Array.ofDim[Int](pre.length + post.length)
    REP(pre.length)(i => res(i) = pre(i))
    val preLen = pre.length
    val postLen = post.length
    REP(postLen)(i => res(i + preLen) = post(postLen - 1 - i))
    res
  }

  def gcd(a: Int, b: Int): Int = {
    if (b == 0) a
    else gcd(b, a % b)
  }

  def gcd(as: Array[Int]): Int = {
    var res = as(0)
    REP(as.length - 1, 1) { i =>
      res = gcd(res, as(i))
    }
    res
  }
}


You came to the exhibition and one exhibit has drawn your attention. It consists of $$$n$$$ stacks of blocks, where the $$$i$$$-th stack consists of $$$a_i$$$ blocks resting on the surface.The height of the exhibit is equal to $$$m$$$. Consequently, the number of blocks in each stack is less than or equal to $$$m$$$.There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks. Find the maximum number of blocks you can remove such that the views for both the cameras would not change.Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100\,000$$$, $$$1 \le m \le 10^9$$$) — the number of stacks and the height of the exhibit. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le m$$$) — the number of blocks in each stack from left to right.

Print exactly one integer — the maximum number of blocks that can be removed.

NoteThe following pictures illustrate the first example and its possible solution.Blue cells indicate removed blocks. There are $$$10$$$ blue cells, so the answer is $$$10$$$.

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

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

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

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

  def solve(): Unit = {
    val N, M = ni()
    val A = na(N)
    radixSort(A)
    var p = 0
    rep(N - 1) { i =>
      if (A(i) > p) p += 1
    }
    val remain = N - 1 + max(1, A(N - 1) - p) // [1, N - 1]に１こずつ配置
    val ans = sumL(A) - remain
    out.println(ans)
  }

  /**
    * 正の値のみのときだけ
    */
  def radixSort(f: Array[Int]): Array[Int] = radixSort(f, f.length)
  def radixSort(as: Array[Int], n: Int): Array[Int] = {
    val n = as.length
    val xs = Array(as, new Array[Int](n))
    val b = Array.ofDim[Int](65537)

    def step(k: Int, x: Int): Unit = {
      rep(n){ i => b(1 + (xs(x)(i) >>> k & 0xffff)) += 1 }
      rep(65536, 1){ i => b(i) += b(i - 1) }
      rep(n){ i =>
        val j = xs(x)(i) >>> k & 0xffff
        xs(x ^ 1)(b(j)) = xs(x)(i)
        b(j) += 1
      }
    }

    step(0, 0)
    java.util.Arrays.fill(b, 0)
    step(16, 1)

    as
  }

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

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

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

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

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

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

object CF_523_2_B extends App {
  def solve(as: Seq[Int]): Long = loop(as.filter(_>0).sortWith(_>_))

  def loop(as: Seq[Int], removed: Long = 0):Long =  as match {
    // last column, keep them all and return result
    case Seq(last) => removed
    case c1 +: c2 +: _ =>
      // remove all blocks in c1 below height of c2
      if(c1 > c2) loop(as.tail, removed + c2)
      // if c2 same height or more, remove all but 1 of c1 reduce c2 to 1 below c1 if c1 is > 1
      else  {
        val newc2 = math.max(1, c1 - 1)
        val toRemove = math.max(0, c1 - 1) + (c2 - newc2)
        loop(as.tail.updated(0, newc2), removed + toRemove)
      }
  }
  val io = new IO(System.in)
  io.int(); io.int()
  println(solve(io.intSeq()))
}

/// boilerplate utility methods:
import java.util.Scanner

class IO(sc: Scanner) {
  def this(i: java.io.InputStream) = this(new Scanner(i))
  def this(s: String) = this(new Scanner(s))
  // methods are side-effecting because they advance Scanner's pointer
  // extra nextLine is required to advance past the newline on the ints line
  def collect[T](f: String => T) = {sc.nextLine();sc.nextLine().split(" ").toVector.map(f)}
  def intSeq() = collect(_.toInt)
  def doubleSeq() = collect(_.toDouble)
  def int() = sc.nextInt()
  def double() = sc.nextDouble()
}

object MainB extends App {
  val (n, m) = {
    val l = scala.io.StdIn.readLine().split(" ")
    (math.BigInt(l(0).toInt), math.BigInt(l(1).toInt))
  }
  val ns = scala.io.StdIn.readLine().split(" ").map(i => math.BigInt(i.toInt)).sorted
  var d = 0
  for (i <- ns.indices) if (ns(i) > d) d += 1
  println(ns.sum - n - ns.max + d)
}

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

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

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

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

  def solve(): Unit = {
    val N, M = ni()
    val A = na(N)
    if (N == 1) {
      out.println(0)
    } else {
      radixSort(A)

      val remain = N - 1 + max(1, A(N - 1) - A(N - 2))
      val ans = sumL(A) - remain
      out.println(ans)
    }
  }

  /**
    * 正の値のみのときだけ
    */
  def radixSort(f: Array[Int]): Array[Int] = radixSort(f, f.length)
  def radixSort(as: Array[Int], n: Int): Array[Int] = {
    val n = as.length
    val xs = Array(as, new Array[Int](n))
    val b = Array.ofDim[Int](65537)

    def step(k: Int, x: Int): Unit = {
      rep(n){ i => b(1 + (xs(x)(i) >>> k & 0xffff)) += 1 }
      rep(65536, 1){ i => b(i) += b(i - 1) }
      rep(n){ i =>
        val j = xs(x)(i) >>> k & 0xffff
        xs(x ^ 1)(b(j)) = xs(x)(i)
        b(j) += 1
      }
    }

    step(0, 0)
    java.util.Arrays.fill(b, 0)
    step(16, 1)

    as
  }

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

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

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

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

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

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

object CF_523_2_B extends App {
  def solve(as: Seq[Int]): Int = loop(as.filter(_>0).sortWith(_>_))

  def loop(as: Seq[Int], removed: Int = 0):Int =  as match {
    // last column, keep them all and return result
    case Seq(last) => removed
    case c1 +: c2 +: _ =>
      // remove all blocks in c1 below height of c2
      if(c1 > c2) loop(as.tail, removed + c2)
      // if c2 same height or more, remove all but 1 of c1 reduce c2 to 1 below c1 if c1 is > 1
      else  {
        val newc2 = math.max(1, c1 - 1)
        val toRemove = math.max(0, c1 - 1) + (c2 - newc2)
        loop(as.tail.updated(0, newc2), removed + toRemove)
      }
  }
  val io = new IO(System.in)
  io.int(); io.int()
  println(solve(io.intSeq()))
}

/// boilerplate utility methods:
import java.util.Scanner

class IO(sc: Scanner) {
  def this(i: java.io.InputStream) = this(new Scanner(i))
  def this(s: String) = this(new Scanner(s))
  // methods are side-effecting because they advance Scanner's pointer
  // extra nextLine is required to advance past the newline on the ints line
  def collect[T](f: String => T) = {sc.nextLine();sc.nextLine().split(" ").toVector.map(f)}
  def intSeq() = collect(_.toInt)
  def doubleSeq() = collect(_.toDouble)
  def int() = sc.nextInt()
  def double() = sc.nextDouble()
}

object CF_523_2_B extends App {
  def solve(as: Seq[Int]): Int = loop(as.filter(_>0).sortWith(_>_))

  def loop(as: Seq[Int], removed: Int = 0):Int =  as match {
    // last column, keep them all and return result
    case Seq(last) => removed
    case c1 +: c2 +: _ =>
      // remove all blocks in c1 below height of c2
      if(c1 > c2) loop(as.tail, removed + c2)
      // if c2 same height or more, remove all but 1 of c1 reduce c2 to 1 below c1 if c1 is > 1
      else  {
        val newc2 = math.max(1, c1 - 1)
        val toRemove = math.max(0, c1 - 1) + (c2 - newc2)
        loop(as.tail.updated(0, newc2), removed + toRemove)
      }
  }
  val io = new IO(System.in)
  io.int(); io.int()
  solve(io.intSeq())
}

/// boilerplate utility methods:
import java.util.Scanner

class IO(sc: Scanner) {
  def this(i: java.io.InputStream) = this(new Scanner(i))
  def this(s: String) = this(new Scanner(s))
  // methods are side-effecting because they advance Scanner's pointer
  // extra nextLine is required to advance past the newline on the ints line
  def collect[T](f: String => T) = {sc.nextLine();sc.nextLine().split(" ").toVector.map(f)}
  def intSeq() = collect(_.toInt)
  def doubleSeq() = collect(_.toDouble)
  def int() = sc.nextInt()
  def double() = sc.nextDouble()
}

object MainB extends App {
  val (n, m) = {
    val l = scala.io.StdIn.readLine().split(" ")
    (math.BigInt(l(0).toInt), math.BigInt(l(1).toInt))
  }
  val ns = scala.io.StdIn.readLine().split(" ").map(_.toInt).sorted
  var d = 0
  for (i <- ns.indices) if (ns(i) > d) d += 1
  println(ns.sum - n - ns.max + d)
}

object MainB extends App {
  val (n, m) = {
    val l = scala.io.StdIn.readLine().split(" ")
    (l(0).toInt, l(1).toInt)
  }
  val ns = scala.io.StdIn.readLine().split(" ").map(_.toInt).sorted
  var d = 0
  for (i <- ns.indices) if (ns(i) > d) d += 1
  println(ns.sum - n - ns.max + d)
}


A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from  - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist.

The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0.

If the required point exists, output its coordinates, otherwise output -1.

import java.util.Scanner

object CStraight extends App {

  val scanner = new Scanner(System.in)

  val a = scanner.nextLong()
  val b = scanner.nextLong()
  val c = -scanner.nextLong()

  val res = gcd(a, b)
  if (c % res._1 == 0) {
    val x0 = res._2 * (c / res._1)
    val y0 = res._3 * (c / res._1)
    println(x0 + " " + y0)
  } else {
    println("-1")
  }

  def gcd(a: Long, b: Long): (Long, Long, Long) = {
    if (a == 0) (b, 0, 1)
    else {
      val (g, x1, y1) = gcd(b % a, a)
      val x = y1 - (b / a) * x1
      val y = x1
      (g, x, y)
    }
  }

}

object P7C {
    def eGcd(a : Long, b : Long) : (Long, Long, Long) = () match {
        case _ if a < 0L => eGcd(-a, b) match {case (p, q, d) => (-p, q, d)}
        case _ if b < 0L => eGcd(a, -b) match {case (p, q, d) => (p, -q, d)}
        case _ if a < b  => eGcd(b, a) match {case (p, q, d) => (q, p, d)}
        case _ if b == 0L => (1, 0, a)
        case _ => eGcd(b, a % b) match {
            case (p, q, d) => (q, p - a / b * q, d)
        }
    }

    def main(args : Array[String]) {
        val Array(a, b, c) = readLine split ' ' map {_.toInt}
        val (p, q, d) = eGcd(a, b)
        if (c % d == 0) {
            val m = -c / d
            println(p * m + " " + q * m)
        } else {
            println("-1")
        }
    }
}

import java.util.Scanner

object CStraight extends App {

  val scanner = new Scanner(System.in)

  val a = scanner.nextInt()
  val b = scanner.nextInt()
  val c = -scanner.nextInt()

  val res = gcd(a, b)
  if (c % res._1 == 0) {
    val x0 = res._2 * (c / res._1)
    val y0 = res._3 * (c / res._1)
    println(x0 + " " + y0)
  } else {
    println("-1")
  }

  def gcd(a: Int, b: Int): (Int, Int, Int) = {
    if (a == 0) (b, 0, 1)
    else {
      val (g, x1, y1) = gcd(b % a, a)
      val x = y1 - (b / a) * x1
      val y = x1
      (g, x, y)
    }
  }

}


Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them — those with biggest ti.Your task is to handle queries of two types:  "1 id" — Friend id becomes online. It's guaranteed that he wasn't online before.  "2 id" — Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line. Are you able to help Limak and answer all queries of the second type?

The first line contains three integers n, k and q (1 ≤ n, q ≤ 150 000, 1 ≤ k ≤ min(6, n)) — the number of friends, the maximum number of displayed online friends and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 109) where ti describes how good is Limak's relation with the i-th friend. The i-th of the following q lines contains two integers typei and idi (1 ≤ typei ≤ 2, 1 ≤ idi ≤ n) — the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed. It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.

For each query of the second type print one line with the answer — "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.

NoteIn the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:  "1 3" — Friend 3 becomes online.  "2 4" — We should check if friend 4 is displayed. He isn't even online and thus we print "NO".  "2 3" — We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".  "1 1" — Friend 1 becomes online. The system now displays both friend 1 and friend 3.  "1 2" — Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed  "2 1" — Print "NO".  "2 2" — Print "YES".  "2 3" — Print "YES".

object B {

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

    //System.setIn(new FileInputStream("src/b.in"))
    //System.setOut(new PrintStream(new FileOutputStream("src/b.out")))

    var Array(n, k, q) = readLine().split(" ").map(_.toInt)
    var friends: Array[Int] = readLine().split(" ").map(_.toInt)
    var screen: Set[Int] = Set.empty[Int]

    for(i <- 0 until q) {
      var Array(tp, id) = readLine().split(" ").map(_.toInt)
      if(tp == 1) {
        if(screen.size < k || friends(id - 1) > screen.min) {
          screen += friends(id - 1)
          if(screen.size > k) {
            screen -= screen.min
          }
        }
      }
      else {
        if(screen.contains(friends(id - 1))) {
          println("YES")
        }
        else {
          println("NO")
        }
      }
    }

  }

}

import scala.collection._

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, k, q) = readInts(3)
  val ts = readInts(n)

  val sb = new StringBuilder

  val active = new mutable.TreeSet[Int]()(Ordering.by(ts))

  for (_ <- 0 until q) {
    readInts(2) match {
      case Array(1, id) =>
        active += (id - 1)
        while (active.size > k) {
          val min = active.head
          active.remove(min)
        }
      case Array(2, id) =>
        sb.append(if (active.contains(id - 1)) "YES\n" else "NO\n")
    }
  }

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

  println(sb.result())

  Console.flush
}

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

object _658B extends CodeForcesApp {
  override def apply(io: IO) = {
    val n, k, q = io[Int]
    val t = io[IndexedSeq, Int](n)
    val pq = mutable.PriorityQueue.empty[Int](Ordering.by(id => -t(id - 1)))
    val ans = Vector.empty[Boolean]
    io.separator = "\n"
    repeat(q) {
      val _ = io[(Int, Int)] match {
        case (1, id) =>
          pq += id
          if (pq.size > k) pq.dequeue()
        case (2, id) =>
          io += pq.exists(_ == id)
      }
    }
    io
  }
}
/****************************[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 sum[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
  }
  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 PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_._1).mapValues(_._2)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_._1).mapValues(_.map(_._2).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 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 SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains 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 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 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] = Dict.empty[K, V] withDefaultValue default
  }
  def memoize[I] = new {
    def apply[O](f: I => O): I => O = new mutable.HashMap[I, O]() {
      override def apply(key: I) = getOrElseUpdate(key, f(key))
    }
  }
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None
  def repeat(n: Int)(f: => Unit): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  val mod: Int = (1e9 + 7).toInt
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

class IO(in: BufferedReader, out: BufferedWriter) extends PrintWriter(out, true) with Iterator[String] {
  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

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

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

  def till(delim: String): String = tokenizer().get.nextToken(delim)
  def tillEndOfLine(): String = till("\n\r")

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

  var separator = " "
  private[this] var isNewLine = true

  def +=[A](x: A)(implicit write: IO.Write[A]): this.type = {
    write(this)(x)
    this
  }
  def appendNewLine(): this.type = {
    println()
    this
  }

  override def print(obj: String) = {
    if (isNewLine) isNewLine = false else super.print(separator)
    super.print(obj)
  }
  override def println() = if (!isNewLine) {
    super.println()
    isNewLine = true
  }
  override def close() = {
    flush()
    in.close()
    super.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[C, A](r[Int]))
    implicit val string                                       : Read[String]       = new Read(_.next())
    implicit val char                                         : Read[Char]         = string.map(_.toTraversable.onlyElement)
    implicit val boolean                                      : Read[Boolean]      = string.map(_.toBoolean)
    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[A], r[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]            : Read[(A, B, C)]    = new Read(r => (r[A], r[B], r[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]   : Read[(A, B, C, D)] = new Read(r => (r[A], r[B], r[C], r[D]))
  }
  trait Write[-A] {
    def apply(out: PrintWriter)(x: A): Unit
  }
  trait DefaultWrite {
    implicit def any[A]: Write[A] = Write(_.toString)
  }
  object Write extends DefaultWrite {
    def apply[A](f: A => String) = new Write[A] {
      override def apply(out: PrintWriter)(x: A) = out.print(f(x))
    }
    implicit val boolean: Write[Boolean] = Write(b => if (b) "YES" else "NO")
    implicit def iterable[A](implicit write: Write[A]): Write[Traversable[A]] = new Write[Traversable[A]] {
      override def apply(out: PrintWriter)(xs: Traversable[A]) = {
        xs foreach write(out)
        out.println()
      }
    }
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, k, q) = in.next().split(' ').map(_.toInt)
  val t = in.next().split(' ').map(_.toInt)
  val window = Array.ofDim[Int](k)
  (1 to q).foreach { _ =>
    val Array(ty, id) = in.next().split(' ').map(_.toInt)
    if (ty == 1) {
      if (!window.contains(id)) {
        val min = window.indices.find(i => window(i) == 0).getOrElse(
          window.indices.minBy(i => t(window(i) - 1)))
        if (window(min) == 0 || t(window(min) - 1) < t(id - 1))
          window(min) = id
      }
    } else {
      if (window.contains(id))
        println("YES")
      else
        println("NO")
    }
  }
}