Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings s1 and s2 from two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task.Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string.Given two strings s1 and s2 consist of lowercase Latin letters, find the smallest (by length) common substring p of both s1 and s2, where p is a unique substring in s1 and also in s2. See notes for formal definition of substring and uniqueness.

The first line of input contains s1 and the second line contains s2 (1 ≤ |s1|, |s2| ≤ 5000). Both strings consist of lowercase Latin letters.

Print the length of the smallest common unique substring of s1 and s2. If there are no common unique substrings of s1 and s2 print -1.

NoteImagine we have string a = a1a2a3...a|a|, where |a| is the length of string a, and ai is the ith letter of the string. We will call string alal + 1al + 2...ar (1 ≤ l ≤ r ≤ |a|) the substring [l, r] of the string a. The substring [l, r] is unique in a if and only if there is no pair l1, r1 such that l1 ≠ l and the substring [l1, r1] is equal to the substring [l, r] in a.

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 S1, S2 = ns()
    val S = S1 + "$" + S2

    val sa = suffixArray(S)
    val (lcp, rank) = longestCommonPrefix(S, sa)

    /**
      * これ以上の長さの部分文字列じゃないと重複が存在する
      */
    def limits(s: String) = {
      val sa = suffixArray(s)
      val (lcp, _) = longestCommonPrefix(s, sa)
      val res = Array.ofDim[Int](s.length + 1)
      val n = lcp.length
      REP(n) { i =>
        if (i == n - 1) {
          res(sa(i)) = lcp(i)
        } else {
          res(sa(i)) = max(lcp(i), lcp(i + 1))
        }
      }
      res
    }

    val limit1 = limits(S1)
    val limit2 = limits(S2)

    val INF = 1e6.toInt
    var ans = INF

    // debug
//    REP(sa.length) { i =>
//      System.err.println(s"$i ${S.substring(sa(i))}")
//    }

    REP(lcp.length) { i =>
      if (lcp(i) > 0) {
        val p1 = min(sa(i), sa(i - 1))
        val p2 = max(sa(i), sa(i - 1))

        // p1がS1に, p2がS2に含まれる
        if (p1 < S1.length && p2 > S1.length) {
          val p2OnS2 = p2 - S1.length - 1
          val mxLim = max(limit1(p1), limit2(p2OnS2))

          // p1, p2ともに重複制限に引っかからない最短の文字列
          val len = mxLim + 1

          // lcpがlenを作れる程に十分長い
          if (len <= lcp(i)) {
            // p1+lelnがS1を超えない
            if (p1 + len <= S1.length) {
              // debug
//              System.err.println(s"$i ${S.substring(p1, p1 + len)}")
              ans = min(ans, len)
            }
          }
        }
      }
    }

    if (ans == INF) ans = -1
    out.println(ans)
  }

  /**
    * LCPには一個手前の文字との共通の文字数が入っている
    * @return (LCP, 文字列の位置 => 辞書順の位)
    */
  def longestCommonPrefix(s: String, sa: Array[Int]): (Array[Int], Array[Int]) = {
    val n = s.length
    val lcp = Array.ofDim[Int](n + 1)
    val rank = Array.ofDim[Int](n + 1)
    REP(n + 1) { i =>
      rank(sa(i)) = i
    }
    var h = 0
    REP(n) { i =>
      val j = sa(rank(i) - 1) // sa上で１個前
      h = max(0, h - 1)
      while(i + h < n && j + h < n && s(i + h) == s(j + h)) {
        h += 1
      }
      lcp(rank(i)) = h
    }
    (lcp, rank)
  }

  /**
    * 大文字小文字が混ざってる場合はうまくいかないので注意
    * @return 辞書順 => 文字列の位置
    */
  def suffixArray(s: String): Array[Int] = {
    val n = s.length

    val sa = Array.iterate(0, n + 1)(_ + 1)
    val rank, tmp = Array.ofDim[Int](n + 1)
    REP(n) { i =>
      rank(i) = s(i)
    }
    rank(n) = -1

    def lt(len: Int)(i: Int, j: Int): Boolean = {
      if (rank(i) != rank(j)) rank(i) < rank(j)
      else {
        // 長さが足りないってことは文字がないてことなので、辞書順で先になる
        val ri = if (i + len <= n) rank(i + len) else -1
        val rj = if (j + len <= n) rank(j + len) else -1
        ri < rj
      }
    }

    var len = 1
    while(len <= n) {
      // 今のランクとダブリングをつかってlen*2までの長さで並び替える
      Sorting.quickSort[Int](sa)(Ordering.fromLessThan(lt(len)))

      // 次のlen*2の長さで並び替えたsaからrankの値を計算する
      tmp(sa(0)) = 0
      REP(n, 1) { i =>
        tmp(sa(i)) = tmp(sa(i - 1)) + (if (lt(len)(sa(i - 1), sa(i))) 1 else 0) // 前の順位のものより大きい場合は+1する
      }

      Array.copy(tmp, 0, rank, 0, n + 1)
      len *= 2
    }
    sa
  }

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

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

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

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

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

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

object Match extends App {
	val (s0, s1) = (readLine, readLine)

	def shortest (s : String) : Array[Int] = {
		val (buf, ans) = (Array.fill(s.length + 1)(0), Array.fill(s.length)(0))
		for (i <- s.length - 1 to 0 by -1) {
			for (j <- 0 until s.length) {
				if (s(i) == s(j) && i != j) {
					buf(j) = buf(j + 1) + 1
					ans(i) = math.max(ans(i), buf(j))
				} else buf(j) = 0
			}
		}
		ans
	}

	val (m0, m1) = (shortest(s0), shortest(s1))
	val (buf, inf) = (Array.fill(s1.length + 1)(0), 123456)
	var ans = inf
	for (i <- s0.length - 1 to 0 by -1) {
		for (j <- 0 until s1.length) {
			if (s0(i) == s1(j)) {
				buf(j) = buf(j + 1) + 1
				if (buf(j) > m0(i) && buf(j) > m1(j)) {
					ans = math.min(ans, 1 + math.max(m0(i), m1(j)))
				}
			} else buf(j) = 0
		}
	}

	print(if (ans < inf) ans else -1)
}

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 S1, S2 = ns()
    val S = S1 + S2

    val sa = suffixArray(S)
    val (lcp, rank) = longestCommonPrefix(S, sa)

    /**
      * これ以上の長さの部分文字列じゃないと重複が存在する
      */
    def limits(s: String) = {
      val sa = suffixArray(s)
      val (lcp, _) = longestCommonPrefix(s, sa)
      val res = Array.ofDim[Int](s.length + 1)
      val n = lcp.length
      REP(n) { i =>
        if (i == n - 1) {
          res(sa(i)) = lcp(i)
        } else {
          res(sa(i)) = max(lcp(i), lcp(i + 1))
        }
      }
      res
    }

    val limit1 = limits(S1)
    val limit2 = limits(S2)

    val INF = 1e6.toInt
    var ans = INF

    // debug
//    REP(sa.length) { i =>
//      System.err.println(s"$i ${S.substring(sa(i))}")
//    }

    REP(lcp.length) { i =>
      if (lcp(i) > 0) {
        val p1 = min(sa(i), sa(i - 1))
        val p2 = max(sa(i), sa(i - 1))

        // p1がS1に, p2がS2に含まれる
        if (p1 <= S1.length && p2 >= S1.length) {
          val p2OnS2 = p2 - S1.length
          val mxLim = max(limit1(p1), limit2(p2OnS2))
          val len = mxLim + 1
          // p1, p2ともに重複制限に引っかからない程の長い文字列
          if (len <= lcp(i)) {
            // p1+lがS1を超えない
            if (p1 + len <= S1.length) {
              // debug
//              System.err.println(s"$i ${S.substring(p1, p1 + len)}")
              ans = min(ans, len) // 引っかからない最短の文字列
            }
          }
        }
      }
    }

    if (ans == INF) ans = -1
    out.println(ans)
  }

  /**
    * LCPには一個手前の文字との共通の文字数が入っている
    * @return (LCP, 文字列の位置 => 辞書順の位)
    */
  def longestCommonPrefix(s: String, sa: Array[Int]): (Array[Int], Array[Int]) = {
    val n = s.length
    val lcp = Array.ofDim[Int](n + 1)
    val rank = Array.ofDim[Int](n + 1)
    REP(n + 1) { i =>
      rank(sa(i)) = i
    }
    var h = 0
    REP(n) { i =>
      val j = sa(rank(i) - 1) // sa上で１個前
      h = max(0, h - 1)
      while(i + h < n && j + h < n && s(i + h) == s(j + h)) {
        h += 1
      }
      lcp(rank(i)) = h
    }
    (lcp, rank)
  }

  /**
    * 大文字小文字が混ざってる場合はうまくいかないので注意
    * @return 辞書順 => 文字列の位置
    */
  def suffixArray(s: String): Array[Int] = {
    val n = s.length

    val sa = Array.iterate(0, n + 1)(_ + 1)
    val rank, tmp = Array.ofDim[Int](n + 1)
    REP(n) { i =>
      rank(i) = s(i)
    }
    rank(n) = -1

    def lt(len: Int)(i: Int, j: Int): Boolean = {
      if (rank(i) != rank(j)) rank(i) < rank(j)
      else {
        // 長さが足りないってことは文字がないてことなので、辞書順で先になる
        val ri = if (i + len <= n) rank(i + len) else -1
        val rj = if (j + len <= n) rank(j + len) else -1
        ri < rj
      }
    }

    var len = 1
    while(len <= n) {
      // 今のランクとダブリングをつかってlen*2までの長さで並び替える
      Sorting.quickSort[Int](sa)(Ordering.fromLessThan(lt(len)))

      // 次のlen*2の長さで並び替えたsaからrankの値を計算する
      tmp(sa(0)) = 0
      REP(n, 1) { i =>
        tmp(sa(i)) = tmp(sa(i - 1)) + (if (lt(len)(sa(i - 1), sa(i))) 1 else 0) // 前の順位のものより大きい場合は+1する
      }

      Array.copy(tmp, 0, rank, 0, n + 1)
      len *= 2
    }
    sa
  }

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

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

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

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

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

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

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 S1, S2 = ns()
    val S = S1 + S2

    val sa = suffixArray(S)
    val (lcp, rank) = longestCommonPrefix(S, sa)

    /**
      * これ以上の長さの部分文字列じゃないと重複が存在する
      */
    def limits(s: String) = {
      val sa = suffixArray(s)
      val (lcp, _) = longestCommonPrefix(s, sa)
      val res = Array.ofDim[Int](s.length + 1)
      val n = lcp.length
      REP(n) { i =>
        if (i == n - 1) {
          res(sa(i)) = lcp(i)
        } else {
          res(sa(i)) = max(lcp(i), lcp(i + 1))
        }
      }
      res
    }

    val limit1 = limits(S1)
    val limit2 = limits(S2)

    val INF = 1e6.toInt
    var ans = INF

//    REP(sa.length) { i =>
//      System.err.println(s"$i ${S.substring(sa(i))}")
//    }

    REP(lcp.length) { i =>
      if (lcp(i) > 0) {
        val p1 = min(sa(i), sa(i - 1))
        val p2 = max(sa(i), sa(i - 1))

        // p1がS1に, p2がS2に含まれる
        if (p1 <= S1.length && p2 >= S1.length) {
          val p2OnS2 = p2 - S1.length
          val mxLim = max(limit1(p1), limit2(p2OnS2))
          // p1, p2ともに重複制限に引っかからない程の長い文字列
          if (mxLim < lcp(i)) {
            val len = mxLim + 1
            // p1+lがS1を超えない
//            if (len <= S.length) {
              ans = min(ans, len) // 引っかからない最短の文字列
//            }
          }
        }
      }
    }

    if (ans == INF) ans = -1
    out.println(ans)
  }

  /**
    * LCPには一個手前の文字との共通の文字数が入っている
    * @return (LCP, 文字列の位置 => 辞書順の位)
    */
  def longestCommonPrefix(s: String, sa: Array[Int]): (Array[Int], Array[Int]) = {
    val n = s.length
    val lcp = Array.ofDim[Int](n + 1)
    val rank = Array.ofDim[Int](n + 1)
    REP(n + 1) { i =>
      rank(sa(i)) = i
    }
    var h = 0
    REP(n) { i =>
      val j = sa(rank(i) - 1) // sa上で１個前
      h = max(0, h - 1)
      while(i + h < n && j + h < n && s(i + h) == s(j + h)) {
        h += 1
      }
      lcp(rank(i)) = h
    }
    (lcp, rank)
  }

  /**
    * 大文字小文字が混ざってる場合はうまくいかないので注意
    * @return 辞書順 => 文字列の位置
    */
  def suffixArray(s: String): Array[Int] = {
    val n = s.length

    val sa = Array.iterate(0, n + 1)(_ + 1)
    val rank, tmp = Array.ofDim[Int](n + 1)
    REP(n) { i =>
      rank(i) = s(i)
    }
    rank(n) = -1

    def lt(len: Int)(i: Int, j: Int): Boolean = {
      if (rank(i) != rank(j)) rank(i) < rank(j)
      else {
        // 長さが足りないってことは文字がないてことなので、辞書順で先になる
        val ri = if (i + len <= n) rank(i + len) else -1
        val rj = if (j + len <= n) rank(j + len) else -1
        ri < rj
      }
    }

    var len = 1
    while(len <= n) {
      // 今のランクとダブリングをつかってlen*2までの長さで並び替える
      Sorting.quickSort[Int](sa)(Ordering.fromLessThan(lt(len)))

      // 次のlen*2の長さで並び替えたsaからrankの値を計算する
      tmp(sa(0)) = 0
      REP(n, 1) { i =>
        tmp(sa(i)) = tmp(sa(i - 1)) + (if (lt(len)(sa(i - 1), sa(i))) 1 else 0) // 前の順位のものより大きい場合は+1する
      }

      Array.copy(tmp, 0, rank, 0, n + 1)
      len *= 2
    }
    sa
  }

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

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

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

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

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

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

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 S1, S2 = ns()
    val S = S1 + S2

    val sa = suffixArray(S)
    val (lcp, rank) = longestCommonPrefix(S, sa)

    /**
      * これ以上の長さの部分文字列じゃないと重複が存在する
      */
    def limits(s: String) = {
      val sa = suffixArray(s)
      val (lcp, _) = longestCommonPrefix(s, sa)
      val res = Array.ofDim[Int](s.length + 1)
      val n = lcp.length
      REP(n) { i =>
        if (i == n - 1) {
          res(sa(i)) = lcp(i)
        } else {
          res(sa(i)) = max(lcp(i), lcp(i + 1))
        }
      }
      res
    }

    val limit1 = limits(S1)
    val limit2 = limits(S2)

    val INF = 1e6.toInt
    var ans = INF

    // debug
//    REP(sa.length) { i =>
//      System.err.println(s"$i ${S.substring(sa(i))}")
//    }

    REP(lcp.length) { i =>
      if (lcp(i) > 0) {
        val p1 = min(sa(i), sa(i - 1))
        val p2 = max(sa(i), sa(i - 1))

        // p1がS1に, p2がS2に含まれる
        if (p1 < S1.length && p2 >= S1.length) {
          val p2OnS2 = p2 - S1.length
          val mxLim = max(limit1(p1), limit2(p2OnS2))

          // p1, p2ともに重複制限に引っかからない最短の文字列
          val len = mxLim + 1

          // lcpがlenを作れる程に十分長い
          if (len <= lcp(i)) {
            // p1+lelnがS1を超えない
            if (p1 + len <= S1.length) {
              // debug
//              System.err.println(s"$i ${S.substring(p1, p1 + len)}")
              ans = min(ans, len)
            }
          }
        }
      }
    }

    if (ans == INF) ans = -1
    out.println(ans)
  }

  /**
    * LCPには一個手前の文字との共通の文字数が入っている
    * @return (LCP, 文字列の位置 => 辞書順の位)
    */
  def longestCommonPrefix(s: String, sa: Array[Int]): (Array[Int], Array[Int]) = {
    val n = s.length
    val lcp = Array.ofDim[Int](n + 1)
    val rank = Array.ofDim[Int](n + 1)
    REP(n + 1) { i =>
      rank(sa(i)) = i
    }
    var h = 0
    REP(n) { i =>
      val j = sa(rank(i) - 1) // sa上で１個前
      h = max(0, h - 1)
      while(i + h < n && j + h < n && s(i + h) == s(j + h)) {
        h += 1
      }
      lcp(rank(i)) = h
    }
    (lcp, rank)
  }

  /**
    * 大文字小文字が混ざってる場合はうまくいかないので注意
    * @return 辞書順 => 文字列の位置
    */
  def suffixArray(s: String): Array[Int] = {
    val n = s.length

    val sa = Array.iterate(0, n + 1)(_ + 1)
    val rank, tmp = Array.ofDim[Int](n + 1)
    REP(n) { i =>
      rank(i) = s(i)
    }
    rank(n) = -1

    def lt(len: Int)(i: Int, j: Int): Boolean = {
      if (rank(i) != rank(j)) rank(i) < rank(j)
      else {
        // 長さが足りないってことは文字がないてことなので、辞書順で先になる
        val ri = if (i + len <= n) rank(i + len) else -1
        val rj = if (j + len <= n) rank(j + len) else -1
        ri < rj
      }
    }

    var len = 1
    while(len <= n) {
      // 今のランクとダブリングをつかってlen*2までの長さで並び替える
      Sorting.quickSort[Int](sa)(Ordering.fromLessThan(lt(len)))

      // 次のlen*2の長さで並び替えたsaからrankの値を計算する
      tmp(sa(0)) = 0
      REP(n, 1) { i =>
        tmp(sa(i)) = tmp(sa(i - 1)) + (if (lt(len)(sa(i - 1), sa(i))) 1 else 0) // 前の順位のものより大きい場合は+1する
      }

      Array.copy(tmp, 0, rank, 0, n + 1)
      len *= 2
    }
    sa
  }

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

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

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

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

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

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

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 S1, S2 = ns()
    val S = S1 + S2

    val sa = suffixArray(S)
    val (lcp, rank) = longestCommonPrefix(S, sa)

    /**
      * これ以上の長さの部分文字列じゃないと重複が存在する
      */
    def limits(s: String) = {
      val sa = suffixArray(s)
      val (lcp, _) = longestCommonPrefix(s, sa)
      val res = Array.ofDim[Int](s.length + 1)
      val n = lcp.length
      REP(n) { i =>
        if (i == n - 1) {
          res(sa(i)) = lcp(i)
        } else {
          res(sa(i)) = max(lcp(i), lcp(i + 1))
        }
      }
      res
    }

    val limit1 = limits(S1)
    val limit2 = limits(S2)

    val INF = 1e6.toInt
    var ans = INF

//    REP(sa.length) { i =>
//      System.err.println(s"$i ${S.substring(sa(i))}")
//    }

    REP(lcp.length) { i =>
      val len = lcp(i)
      if (len > 0) {
        val p1 = min(sa(i), sa(i - 1))
        val p2 = max(sa(i), sa(i - 1))

        // [p1, p1+len]がS1に, [p2, p2+len]がS2に含まれる
        if (p1 + len <= S1.length && p2 >= S1.length) {
          val p2OnS2 = p2 - S1.length
          val mxLim = max(limit1(p1), limit2(p2OnS2))
          // p1, p2ともに重複制限に引っかからない程の長い文字列
          if (mxLim < len) {
            ans = min(ans, mxLim + 1) // 引っかからない最短の文字列
          }
        }
      }
    }

    if (ans == INF) ans = -1
    out.println(ans)
  }

  /**
    * LCPには一個手前の文字との共通の文字数が入っている
    * @return (LCP, 文字列の位置 => 辞書順の位)
    */
  def longestCommonPrefix(s: String, sa: Array[Int]): (Array[Int], Array[Int]) = {
    val n = s.length
    val lcp = Array.ofDim[Int](n + 1)
    val rank = Array.ofDim[Int](n + 1)
    REP(n + 1) { i =>
      rank(sa(i)) = i
    }
    var h = 0
    REP(n) { i =>
      val j = sa(rank(i) - 1) // sa上で１個前
      h = max(0, h - 1)
      while(i + h < n && j + h < n && s(i + h) == s(j + h)) {
        h += 1
      }
      lcp(rank(i)) = h
    }
    (lcp, rank)
  }

  /**
    * 大文字小文字が混ざってる場合はうまくいかないので注意
    * @return 辞書順 => 文字列の位置
    */
  def suffixArray(s: String): Array[Int] = {
    val n = s.length

    val sa = Array.iterate(0, n + 1)(_ + 1)
    val rank, tmp = Array.ofDim[Int](n + 1)
    REP(n) { i =>
      rank(i) = s(i)
    }
    rank(n) = -1

    def lt(len: Int)(i: Int, j: Int): Boolean = {
      if (rank(i) != rank(j)) rank(i) < rank(j)
      else {
        // 長さが足りないってことは文字がないてことなので、辞書順で先になる
        val ri = if (i + len <= n) rank(i + len) else -1
        val rj = if (j + len <= n) rank(j + len) else -1
        ri < rj
      }
    }

    var len = 1
    while(len <= n) {
      // 今のランクとダブリングをつかってlen*2までの長さで並び替える
      Sorting.quickSort[Int](sa)(Ordering.fromLessThan(lt(len)))

      // 次のlen*2の長さで並び替えたsaからrankの値を計算する
      tmp(sa(0)) = 0
      REP(n, 1) { i =>
        tmp(sa(i)) = tmp(sa(i - 1)) + (if (lt(len)(sa(i - 1), sa(i))) 1 else 0) // 前の順位のものより大きい場合は+1する
      }

      Array.copy(tmp, 0, rank, 0, n + 1)
      len *= 2
    }
    sa
  }

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

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

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

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

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

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


One day, Ahmed_Hossam went to Hemose and said "Let's solve a gym contest!". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?There is an Agent in Valorant, and he has $$$n$$$ weapons. The $$$i$$$-th weapon has a damage value $$$a_i$$$, and the Agent will face an enemy whose health value is $$$H$$$.The Agent will perform one or more moves until the enemy dies.In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to $$$0$$$. However, not everything is so easy: the Agent can't choose the same weapon for $$$2$$$ times in a row.What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 10^5)$$$. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$H$$$ $$$(2 \leq n \leq 10^3, 1 \leq H \leq 10^9)$$$ — the number of available weapons and the initial health value of the enemy. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$ — the damage values of the weapons. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print a single integer — the minimum number of times that the Agent will have to use the weapons to kill the enemy.

NoteIn the first test case, the Agent can use the second weapon, making health value of the enemy equal to $$$4-7=-3$$$. $$$-3 \le 0$$$, so the enemy is dead, and using weapon $$$1$$$ time was enough.In the second test case, the Agent can use the first weapon first, and then the second one. After this, the health of enemy will drop to $$$6-4-2 = 0$$$, meaning he would be killed after using weapons $$$2$$$ times.In the third test case, the Agent can use the weapons in order (third, first, third), decreasing the health value of enemy to $$$11 - 7 - 2 - 7 = -5$$$ after using the weapons $$$3$$$ times. Note that we can't kill the enemy by using the third weapon twice, as even though $$$11-7-7<0$$$, it's not allowed to use the same weapon twice in a row.

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

object sample 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 myType(var social: Int, var cp:  Int, var ind: Int)extends Ordered[myType] {
        override def compare(that: myType): Int = {
            if (social < that.social)
                return -1
            else if (social == that.social)
                return 0
            else
                return 1
        }
    }

    val t = readInt()
    for (tt <- 1 to t) {
        val n = readInt()
        var h = readInt()
        var tmp = 0
        var mx1 = 0
        var mx2 = 0
        for (i <- 0 until n) {
            tmp = readInt()
            if (tmp > mx2)
                mx2 = tmp
            if (mx2 > mx1){
                tmp = mx1
                mx1 = mx2
                mx2 = tmp
            }
        }
        var cnt = 2 * (h/(mx1+mx2)).toInt
        if (h %(mx1+mx2)!= 0)
            cnt += 1
        if (h%(mx1+mx2)> mx1)
            cnt += 1
        writer.println(cnt)
    }
    writer.flush()
}

object A extends App {

  def gamerHemose(an: Array[Int], h: Int): Int = {
    val Array(a1, a2, _*) = an.sorted(Ordering[Int].reverse)

    (h / (a1 + a2), h % (a1 + a2)) match {
      case (i, 0)            => 2 * i
      case (i, j) if j <= a1 => 2 * i + 1
      case (i, _)            => 2 * i + 2
    }
  }

  import InOut._

  val t = nextInt()

  (0 until t).foreach { _ =>
    val n  = nextInt()
    val h  = nextInt()
    val an = nextInts(n)

    out.println(gamerHemose(an, h))
  }

  out.flush()

  final object InOut {
    val in  = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)

    def nextInt()        = Integer.parseInt(nextToken())
    def nextInts(n: Int) = Array.fill(n)(nextInt())

    def nextLong()        = java.lang.Long.parseLong(nextToken())
    def nextLongs(n: Int) = Array.fill(n)(nextLong())

    def nextBig()        = BigInt(nextToken())
    def nextBigs(n: Int) = Array.fill(n)(nextBig())

    def nextLine() = in.readLine()

    private[this] var tokenizer: java.util.StringTokenizer = _

    def nextToken(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new java.util.StringTokenizer(in.readLine())
      tokenizer.nextToken()
    }
  }

}

object A extends App {

  def gamerHemose(an: Array[Int], h: Int): Int = {
    val Array(a1, a2, _*) = an.sorted(Ordering[Int].reverse)

    val (i, j) = (h / (a1 + a2), h % (a1 + a2))

    j match {
      case 0            => 2 * i
      case j if j <= a1 => 2 * i + 1
      case _            => 2 * i + 2
    }
  }

  import InOut._

  val t = nextInt()

  (0 until t).foreach { _ =>
    val n  = nextInt()
    val h  = nextInt()
    val an = nextInts(n)

    out.println(gamerHemose(an, h))
  }

  out.flush()

  final object InOut {
    val in  = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)

    def nextInt()        = Integer.parseInt(nextToken())
    def nextInts(n: Int) = Array.fill(n)(nextInt())

    def nextLong()        = java.lang.Long.parseLong(nextToken())
    def nextLongs(n: Int) = Array.fill(n)(nextLong())

    def nextBig()        = BigInt(nextToken())
    def nextBigs(n: Int) = Array.fill(n)(nextBig())

    def nextLine() = in.readLine()

    private[this] var tokenizer: java.util.StringTokenizer = _

    def nextToken(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new java.util.StringTokenizer(in.readLine())
      tokenizer.nextToken()
    }
  }

}

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

object sample 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 myType(var social: Int, var cp:  Int, var ind: Int)extends Ordered[myType] {
        override def compare(that: myType): Int = {
            if (social < that.social)
                return -1
            else if (social == that.social)
                return 0
            else
                return 1
        }
    }

    val t = readInt()
    for (tt <- 1 to t) {
        val n = readInt()
        var h = readInt()
        var tmp = 0
        var mx1 = 0
        var mx2 = 0
        for (i <- 0 until n) {
            tmp = readInt()
            if (tmp > mx2)
                mx2 = tmp
            if (mx2 > mx1){
                tmp = mx1
                mx1 = mx2
                mx2 = tmp
            }
        }
        var cnt = (2 * h/(mx1+mx2)).toInt
        if (h %(mx1+mx2)!= 0)
            cnt += 1
        if (h%(mx1+mx2)> mx1)
            cnt += 1
        writer.println(cnt)
    }
    writer.flush()
}

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

object sample 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 myType(var social: Int, var cp:  Int, var ind: Int)extends Ordered[myType] {
        override def compare(that: myType): Int = {
            if (social < that.social)
                return -1
            else if (social == that.social)
                return 0
            else
                return 1
        }
    }

    val t = readInt()
    for (tt <- 1 to t) {
        var n = readInt()
        var h = readInt()
        val arr = new Array[Int](n)
        for (i <- 0 until n)
            arr(i) = readInt()
        quickSort(arr)
        var cnt = 0
        while (h > 0 && n > 0) {
            cnt += 1
            h -= arr(n-1)
            n -= 1
        }
        writer.println(cnt)
    }
    writer.flush()
}

object A extends App {

  def gamerHemose(an: Array[Int], h: Int): Int = {
    val Array(a1, a2, _*) = an.sorted(Ordering[Int].reverse)
    h / (a1 + a2) * 2 + (if (h % (a1 + a2) == 0) 0 else 1)
  }

  import InOut._

  val t = nextInt()

  (0 until t).foreach { _ =>
    val n  = nextInt()
    val h  = nextInt()
    val an = nextInts(n)

    out.println(gamerHemose(an, h))
  }

  out.flush()

  final object InOut {
    val in  = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)

    def nextInt()        = Integer.parseInt(nextToken())
    def nextInts(n: Int) = Array.fill(n)(nextInt())

    def nextLong()        = java.lang.Long.parseLong(nextToken())
    def nextLongs(n: Int) = Array.fill(n)(nextLong())

    def nextBig()        = BigInt(nextToken())
    def nextBigs(n: Int) = Array.fill(n)(nextBig())

    def nextLine() = in.readLine()

    private[this] var tokenizer: java.util.StringTokenizer = _

    def nextToken(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new java.util.StringTokenizer(in.readLine())
      tokenizer.nextToken()
    }
  }

}


Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length $$$n$$$ consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements. Vasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is $$$0$$$, and removing $$$i$$$-th character increases the ambiguity by $$$a_i$$$ (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still $$$4$$$ even though you delete it from the string had).Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the statement. The second line contains one string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters — the statement written by Vasya. The third line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 998244353$$$).

Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.

NoteIn the first example, first two characters are removed so the result is ardh.In the second example, $$$5$$$-th character is removed so the result is hhzawde.In the third example there's no need to remove anything.

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val S = ns(N).map {
      case 'h' => 1
      case 'a' => 2
      case 'r' => 3
      case 'd' => 4
      case _ => -1
    }

    val A = nal(N)

    val INF = 1e17.toLong
    val dp = Array.fill[Long](5)(INF)
    dp(0) = 0

    REP(N) { i =>
      val s = S(i)
      if (s != -1) {
        if (dp(s - 1) != INF) dp(s) = min(dp(s), dp(s - 1))
        if (dp(s - 1) != INF) dp(s - 1) += A(i)
      }
    }

    val ans = map(4) { i =>
      dp(i)
    }.min

    if (dp(4) != INF) out.println(ans)
    else out.println(0)
  }

  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
  }

  def debug(as: Array[Long], delimiter: String = " "): Unit = {
    debug(as.mkString(delimiter))
  }

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

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 S = ns(N).map {
      case 'h' => 1
      case 'a' => 2
      case 'r' => 3
      case 'd' => 4
      case _ => -1
    }

    val A = nal(N)

    val INF = 1e17.toLong
    val dp = Array.fill[Long](5)(INF)
    dp(0) = 0

    REP(N) { i =>
      val s = S(i)
      if (s != -1) {
        if (dp(s - 1) != INF) dp(s) = min(dp(s), dp(s - 1))
        if (dp(s - 1) != INF) dp(s - 1) += A(i)
      }
//      debug(dp)
    }

//    debug(dp)

    val ans = map(4) { i =>
      dp(i)
    }.min

    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
  }

  def debug(as: Array[Long], delimiter: String = " "): Unit = {
    debug(as.mkString(delimiter))
  }

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

object Main {
    import java.io.File
    import java.util.Scanner
    import java.io.PrintWriter

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

    val pat = "hard"

    def main(args: Array[String]) {
        val n = in.nextInt
        in.nextLine
        val str = in.nextLine
        val a = (for (i <- 0 until n) yield in.nextInt).toArray
        val f = new Array[Long]((n + 1) * 4)
        for (i <- 1 until n + 1; j <- 0 until 4)
            f(i * 4 + j) = {
                if (str(i - 1) == pat(j)) {
                    if (j == 0 || f((i - 1) * 4 + j) + a(i - 1) < f((i - 1) * 4 + j - 1))
                        f((i - 1) * 4 + j) + a(i - 1)
                    else f((i - 1) * 4 + j - 1)
                }
                else f((i - 1) * 4 + j)
            }
        out.println(f(n * 4 + 3))
        in.close
        out.close
    }
}


You are given a matrix $$$a$$$ of size $$$n \times m$$$ consisting of integers.You can choose no more than $$$\left\lfloor\frac{m}{2}\right\rfloor$$$ elements in each row. Your task is to choose these elements in such a way that their sum is divisible by $$$k$$$ and this sum is the maximum.In other words, you can choose no more than a half (rounded down) of elements in each row, you have to find the maximum sum of these elements divisible by $$$k$$$.Note that you can choose zero elements (and the sum of such set is $$$0$$$).

The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le n, m, k \le 70$$$) — the number of rows in the matrix, the number of columns in the matrix and the value of $$$k$$$. The next $$$n$$$ lines contain $$$m$$$ elements each, where the $$$j$$$-th element of the $$$i$$$-th row is $$$a_{i, j}$$$ ($$$1 \le a_{i, j} \le 70$$$).

Print one integer — the maximum sum divisible by $$$k$$$ you can obtain.

NoteIn the first example, the optimal answer is $$$2$$$ and $$$4$$$ in the first row, $$$5$$$ and $$$2$$$ in the second row and $$$7$$$ and $$$4$$$ in the third row. The total sum is $$$2 + 4 + 5 + 2 + 7 + 4 = 24$$$.

//package codeforces.contests._1433

object ZeroRemainderSum {
  def main(args: Array[String]): Unit = {
    val Array(n, m, k) = io.StdIn.readLine.split(" ").map(_.toInt)

    val matrix = {
      val matrix = new Array[Array[Int]](n)
      (0 until n).foreach(matrix(_) = io.StdIn.readLine.split(" ").map(_.toInt))
      matrix
    }

    val dp = Array.fill(n + 1, m, m / 2 + 1, k)(Int.MinValue)

    dp(0)(0)(0)(0) = 0

    for {
      i <- 0 until n
      j <- 0 until m
      cM = ((m / 2) min (j + 1)) + 1
      c <- 0 until cM
      r <- 0 until k
    } {

      if (dp(i)(j)(c)(r) != Int.MinValue) {

        if (j < m - 1) {
          dp(i)(j + 1)(c)(r) = dp(i)(j + 1)(c)(r) max dp(i)(j)(c)(r)

          if (c + 1 < cM) {
            val rr = (r + matrix(i)(j)) % k
            dp(i)(j + 1)(c + 1)(rr) = dp(i)(j + 1)(c + 1)(rr) max {
              dp(i)(j)(c)(r) + matrix(i)(j)
            }
          }
        } else { // next row
          dp(i + 1)(0)(0)(r) = dp(i + 1)(0)(0)(r) max dp(i)(j)(c)(r)

          val rr = (r + matrix(i)(j)) % k
          if (c + 1 < cM) {
            dp(i + 1)(0)(0)(rr) = dp(i + 1)(0)(0)(rr) max {
              dp(i)(j)(c)(r) + matrix(i)(j)
            }
          }
        }
      }
    }

    println(dp(n)(0)(0)(0) max 0)

  }
}


Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string $$$s$$$ of length $$$n$$$.Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string $$$s$$$.She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string $$$s$$$ a palindrome, or tell her that it's impossible. Notice that she can only erase symbols equal to the one letter she chose.A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings 'kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq' are not.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The next $$$2 \cdot t$$$ lines contain the description of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the string. The second line of each test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and $$$-1$$$, if it is impossible.

5
8
abcaacab
6
xyzxyz
4
abba
8
rprarlap
10
khyyhhyhky

NoteIn the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well.In the second test case, it can be shown that it is impossible to choose a letter and erase some of its occurrences to get a palindrome.In the third test case, you don't have to erase any symbols because the string is already a palindrome.

object C extends App {

  def minimumNumberOfErasing(str: String): Option[Int] = {
    val size = str.size

    @annotation.tailrec
    def go(left: Int, right: Int, char: Char, count: Int = 0): Option[Int] =
      if (left >= right) Some(count)
      else
        (str(left), str(right)) match {
          case (leftChar, rightChar) if leftChar == rightChar => go(left + 1, right - 1, char, count)
          case (leftChar, _) if char == leftChar              => go(left + 1, right, char, count + 1)
          case (_, rightChar) if char == rightChar            => go(left, right - 1, char, count + 1)
          case _                                              => None
        }

    str.distinct.foldLeft[Option[Int]](None) { (countOpt, char) =>
      val candidateOpt = go(0, size - 1, char)
      candidateOpt.map { candidate => candidate min countOpt.getOrElse(candidate) }.orElse(countOpt)
    }
  }

  import InOut._

  val t = nextInt()

  (0 until t).foreach { _ =>
    val n = nextInt()
    val s = nextLine()

    minimumNumberOfErasing(s) match {
      case Some(count) => out.println(count)
      case _           => out.println(-1)
    }
  }

  out.flush()

  object InOut {
    val in  = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)

    def nextInt()        = Integer.parseInt(nextToken())
    def nextInts(n: Int) = Array.fill(n)(nextInt())

    def nextLong()        = java.lang.Long.parseLong(nextToken())
    def nextLongs(n: Int) = Array.fill(n)(nextLong())

    def nextBig()        = BigInt(nextToken())
    def nextBigs(n: Int) = Array.fill(n)(nextBig())

    def nextLine() = in.readLine()

    private[this] var tokenizer: java.util.StringTokenizer = _

    private[this] def nextToken(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new java.util.StringTokenizer(in.readLine())
      tokenizer.nextToken()
    }
  }

}

object C extends App {

  def minimumNumberOfErasing(str: String): Option[Int] = {
    val size = str.size

    @annotation.tailrec
    def go(left: Int, right: Int, countOpt: Option[Int] = Some(0), erasedOpt: Option[Char] = None): Option[Int] =
      if (left >= right) countOpt
      else {
        val char = str(left)

        str.lastIndexOf(char, right) match {
          case end if end <= left  =>
            erasedOpt match {
              case Some(erased) if char != erased => None
              case _                              => go(left + 1, right, countOpt.map(_ + 1), erasedOpt)
            }
          case end if end == right => go(left + 1, right - 1, countOpt, erasedOpt)
          case end                 =>
            @annotation.tailrec
            def inner(index: Int, countOpt: Option[Int], erasedOpt: Option[Char]): (Option[Int], Option[Char]) =
              if (index > right) (countOpt, erasedOpt)
              else
                erasedOpt match {
                  case Some(erased) if erased != str(index) => (None, None)
                  case _                                    => inner(index + 1, countOpt.map(_ + 1), Some(str(index)))
                }

            inner(end + 1, countOpt, erasedOpt) match {
              case (None, _)       => go(left + 1, right, countOpt.map(_ + 1), Some(char))
              case (count, erased) => go(left + 1, end - 1, count, erased)
            }
        }
      }

    go(0, size - 1)
  }

  import InOut._

  val t = nextInt()

  (0 until t).foreach { _ =>
    val n = nextInt()
    val s = nextLine()

    minimumNumberOfErasing(s) match {
      case Some(count) => out.println(count)
      case _           => out.println(-1)
    }
  }

  out.flush()

  object InOut {
    val in  = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)

    def nextInt()        = Integer.parseInt(nextToken())
    def nextInts(n: Int) = Array.fill(n)(nextInt())

    def nextLong()        = java.lang.Long.parseLong(nextToken())
    def nextLongs(n: Int) = Array.fill(n)(nextLong())

    def nextBig()        = BigInt(nextToken())
    def nextBigs(n: Int) = Array.fill(n)(nextBig())

    def nextLine() = in.readLine()

    private[this] var tokenizer: java.util.StringTokenizer = _

    private[this] def nextToken(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new java.util.StringTokenizer(in.readLine())
      tokenizer.nextToken()
    }
  }

}

object C extends App {

  def minimumNumberOfErasing(str: String): Option[Int] = {
    val size = str.size

    @annotation.tailrec
    def go(left: Int, right: Int, countOpt: Option[Int] = Some(0), erasedOpt: Option[Char] = None): Option[Int] =
      if (left >= right) countOpt
      else {
        val char = str(left)

        str.lastIndexOf(char, right) match {
          case end if end <= left  =>
            erasedOpt match {
              case Some(erased) if char != erased => None
              case _                              => go(left + 1, right, countOpt.map(_ + 1), erasedOpt)
            }
          case end if end == right => go(left + 1, right - 1, countOpt, erasedOpt)
          case end                 =>
            @annotation.tailrec
            def inner(index: Int, countOpt: Option[Int], erasedOpt: Option[Char]): (Option[Int], Option[Char]) =
              if (index > right) (countOpt, erasedOpt)
              else
                erasedOpt match {
                  case Some(erased) if erased != str(index) => (None, None)
                  case _                                    => inner(index + 1, countOpt.map(_ + 1), Some(str(index)))
                }

            inner(end + 1, countOpt, erasedOpt) match {
              case (None, _)       => go(left + 1, right, countOpt.map(_ + 1), Some(char))
              case (count, erased) => go(left + 1, end - 1, count, erased)
            }
        }
      }

    println()

    go(0, size - 1)
  }

  import InOut._

  val t = nextInt()

  (0 until t).foreach { _ =>
    val n = nextInt()
    val s = nextLine()

    out.println(minimumNumberOfErasing(s))
  }

  out.flush()

  object InOut {
    val in  = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)

    def nextInt()        = Integer.parseInt(nextToken())
    def nextInts(n: Int) = Array.fill(n)(nextInt())

    def nextLong()        = java.lang.Long.parseLong(nextToken())
    def nextLongs(n: Int) = Array.fill(n)(nextLong())

    def nextBig()        = BigInt(nextToken())
    def nextBigs(n: Int) = Array.fill(n)(nextBig())

    def nextLine() = in.readLine()

    private[this] var tokenizer: java.util.StringTokenizer = _

    private[this] def nextToken(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
        tokenizer = new java.util.StringTokenizer(in.readLine())
      tokenizer.nextToken()
    }
  }

}


Polycarp has a string $$$s$$$ consisting of lowercase Latin letters.He encodes it using the following algorithm.He goes through the letters of the string $$$s$$$ from left to right and for each letter Polycarp considers its number in the alphabet:  if the letter number is single-digit number (less than $$$10$$$), then just writes it out;  if the letter number is a two-digit number (greater than or equal to $$$10$$$), then it writes it out and adds the number 0 after. For example, if the string $$$s$$$ is code, then Polycarp will encode this string as follows:  'c' — is the $$$3$$$-rd letter of the alphabet. Consequently, Polycarp adds 3 to the code (the code becomes equal to 3);  'o' — is the $$$15$$$-th letter of the alphabet. Consequently, Polycarp adds 15 to the code and also 0 (the code becomes 3150);  'd' — is the $$$4$$$-th letter of the alphabet. Consequently, Polycarp adds 4 to the code (the code becomes 31504);  'e' — is the $$$5$$$-th letter of the alphabet. Therefore, Polycarp adds 5 to the code (the code becomes 315045). Thus, code of string code is 315045.You are given a string $$$t$$$ resulting from encoding the string $$$s$$$. Your task is to decode it (get the original string $$$s$$$ by $$$t$$$).

The first line of the input contains an integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of test cases in the input. The descriptions of the test cases follow. The first line of description of each test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of the given code. The second line of the description of each test case contains a string $$$t$$$ of length $$$n$$$ — the given code. It is guaranteed that there exists such a string of lowercase Latin letters, as a result of encoding which the string $$$t$$$ is obtained.

For each test case output the required string $$$s$$$ — the string that gives string $$$t$$$ as the result of encoding. It is guaranteed that such a string always exists. It can be shown that such a string is always unique.

code
aj
abacaba
ll
codeforces
aaaak
aaaaj
aaaaa
zf

NoteThe first test case is explained above.In the second test case, the answer is aj. Indeed, the number of the letter a is equal to $$$1$$$, so 1 will be appended to the code. The number of the letter j is $$$10$$$, so 100 will be appended to the code. The resulting code is 1100.There are no zeros in the third test case, which means that the numbers of all letters are less than $$$10$$$ and are encoded as one digit. The original string is abacaba.In the fourth test case, the string $$$s$$$ is equal to ll. The letter l has the number $$$12$$$ and is encoded as 120. So ll is indeed 120120.

import scala.util.Try

object _1729B extends CodeForcesApp {
  override def solve() = repeat() {
    val _ = nextInt()
    val s = nextStr().toList.map(_ - '0')
    printLine(parse(s))
  }

  val table = Seq.tabulate(27)(i => ('a' - 1 + i).toChar)

  def parse(chars: List[Int]): String = chars match {
    case Nil => ""
    case 0 :: _ => throw new IllegalStateException()
    case x :: (r1 @ (y :: 0 :: r2)) if 10*x + y < table.length =>
      Try(table(10*x + y) + parse(r2)).getOrElse(table(x) + parse(r1))
    case x :: rest => table(x) + parse(rest)
  }
}
/**************************** [Ignore Template Below] **************************************/
trait CodeForcesApp {
  import java.io._
  var in = new BufferedReader(new InputStreamReader(System.in))
  var out = new PrintWriter(System.out)

  val tokens = Iterator.continually(nextLine()).takeWhile(_ != null).flatMap(_.split(' '))

  def nextLine(): String = in.readLine()
  def nextStr(): String = tokens.next()
  def nextInt(): Int = nextStr().toInt
  def nextLong(): Long = nextStr().toLong
  def nextDouble(): Double = nextStr().toDouble
  def nextN[A](r: => A): Iterator[A] = Iterator.fill(nextInt())(r)
  def repeat[U](n: Int = nextInt())(f: => U): Unit = (1 to n).foreach(_ => f)
  def query[A](query: String)(res: => A): A = {printLine(query); out.flush(); res}

  def print(xs: Any*): Unit = out.print(xs.mkString(" "))
  def printLine(xs: Any*): Unit = if (xs.isEmpty) out.println() else xs.foreach(out.println)
  def debug(x: => Any): Unit = if (!sys.props.contains("ONLINE_JUDGE")) println(s">>> DEBUG: $x")

  def main(args: Array[String]): Unit = try solve() finally out.flush()
  def solve(): Any
}

object _1729B extends CodeForcesApp {
  override def solve() = repeat() {
    val _ = nextInt()
    val s = nextStr().toList.map(_ - '0')
    out.println(parse(s))
  }

  val table = Seq.tabulate(27)(i => ('a' - 1 + i).toChar)

  def parse(chars: List[Int]): String = chars match {
    case Nil => ""
    case 0 :: _ => illegal()
    case x :: (r1 @ (y :: 0 :: r2)) if 10*x + y < table.length =>
      try {
        table(10*x + y) + parse(r2)
      } catch {
        case _: Throwable => table(x) + parse(r1)
      }
    case x :: rest => table(x) + parse(rest)
  }
}
/**************************** [Ignore Template Below] **************************************/
trait CodeForcesApp {
  import java.io._
  var in = new BufferedReader(new InputStreamReader(System.in))
  var out = new PrintWriter(System.out)

  val tokens = Iterator.continually(nextLine()).takeWhile(_ != null).flatMap(_.split(' '))

  def nextLine(): String = in.readLine()
  def nextStr(): String = tokens.next()
  def nextInt(): Int = nextStr().toInt
  def nextLong(): Long = nextStr().toLong
  def nextDouble(): Double = nextStr().toDouble
  def nextN[A](r: => A): Iterator[A] = Iterator.fill(nextInt())(r)
  def repeat[U](n: Int = nextInt())(f: => U): Unit = (1 to n).foreach(_ => f)
  def query[A](query: String, res: => A): A = {out.println(query); out.flush(); res}

  def debug(x: => Any): Unit = if (!sys.props.contains("ONLINE_JUDGE")) println(s">>> DEBUG: $x")
  def illegal(msg: String = "can't happen") = throw new IllegalStateException(msg)

  def main(args: Array[String]): Unit = try solve() finally out.flush()
  def solve(): Any
}


You have a statistic of price changes for one product represented as an array of $$$n$$$ positive integers $$$p_0, p_1, \dots, p_{n - 1}$$$, where $$$p_0$$$ is the initial price of the product and $$$p_i$$$ is how the price was increased during the $$$i$$$-th month.Using these price changes you are asked to calculate the inflation coefficients for each month as the ratio of current price increase $$$p_i$$$ to the price at the start of this month $$$(p_0 + p_1 + \dots + p_{i - 1})$$$.Your boss said you clearly that the inflation coefficients must not exceed $$$k$$$ %, so you decided to increase some values $$$p_i$$$ in such a way, that all $$$p_i$$$ remain integers and the inflation coefficients for each month don't exceed $$$k$$$ %.You know, that the bigger changes — the more obvious cheating. That's why you need to minimize the total sum of changes.What's the minimum total sum of changes you need to make all inflation coefficients not more than $$$k$$$ %?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 100$$$; $$$1 \le k \le 100$$$) — the length of array $$$p$$$ and coefficient $$$k$$$. The second line of each test case contains $$$n$$$ integers $$$p_0, p_1, \dots, p_{n - 1}$$$ ($$$1 \le p_i \le 10^9$$$) — the array $$$p$$$.

For each test case, print the minimum total sum of changes you need to make all inflation coefficients not more than $$$k$$$ %.

NoteIn the first test case, you can, for example, increase $$$p_0$$$ by $$$50$$$ and $$$p_1$$$ by $$$49$$$ and get array $$$[20150, 50, 202, 202]$$$. Then you get the next inflation coefficients:   $$$\frac{50}{20150} \le \frac{1}{100}$$$;  $$$\frac{202}{20150 + 50} \le \frac{1}{100}$$$;  $$$\frac{202}{20200 + 202} \le \frac{1}{100}$$$; In the second test case, you don't need to modify array $$$p$$$, since the inflation coefficients are already good:   $$$\frac{1}{1} \le \frac{100}{100}$$$;  $$$\frac{1}{1 + 1} \le \frac{100}{100}$$$;

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

    for (_ <- 1 to nextInt) {
      val n, k = nextInt
      val ps = nextLongs(n)

      def can(pd: Long): Boolean = {
        var pPrev = ps(0) + pd
        var ok = true
        for (i <- 1 until n) {
          val p = ps(i)
//          println(pPrev, p, pPrev * (100 + k), (pPrev + p) * 100)
          if (pPrev * (100 + k) < (pPrev + p) * 100) {
            ok = false
          }
          pPrev += p
        }
        ok
      }

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

      val sum = binSearch(0, Long.MaxValue / 1000)

      out.println(sum)
    }

    out.flush()
  }

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


The company $$$X$$$ has $$$n$$$ employees numbered from $$$1$$$ through $$$n$$$. Each employee $$$u$$$ has a direct boss $$$p_u$$$ ($$$1 \le p_u \le n$$$), except for the employee $$$1$$$ who has no boss. It is guaranteed, that values $$$p_i$$$ form a tree. Employee $$$u$$$ is said to be in charge of employee $$$v$$$ if $$$u$$$ is the direct boss of $$$v$$$ or there is an employee $$$w$$$ such that $$$w$$$ is in charge of $$$v$$$ and $$$u$$$ is the direct boss of $$$w$$$. Also, any employee is considered to be in charge of himself.In addition, for each employee $$$u$$$ we define it's level $$$lv(u)$$$ as follow:   $$$lv(1)=0$$$  $$$lv(u)=lv(p_u)+1$$$ for $$$u \neq 1$$$ In the near future, there are $$$q$$$ possible plans for the company to operate. The $$$i$$$-th plan consists of two integers $$$l_i$$$ and $$$r_i$$$, meaning that all the employees in the range $$$[l_i, r_i]$$$, and only they, are involved in this plan. To operate the plan smoothly, there must be a project manager who is an employee in charge of all the involved employees. To be precise, if an employee $$$u$$$ is chosen as the project manager for the $$$i$$$-th plan then for every employee $$$v \in [l_i, r_i]$$$, $$$u$$$ must be in charge of $$$v$$$. Note, that $$$u$$$ is not necessary in the range $$$[l_i, r_i]$$$. Also, $$$u$$$ is always chosen in such a way that $$$lv(u)$$$ is as large as possible (the higher the level is, the lower the salary that the company has to pay the employee).Before any plan is operated, the company has JATC take a look at their plans. After a glance, he tells the company that for every plan, it's possible to reduce the number of the involved employees exactly by one without affecting the plan. Being greedy, the company asks JATC which employee they should kick out of the plan so that the level of the project manager required is as large as possible. JATC has already figured out the answer and challenges you to do the same.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 100\,000$$$, $$$1 \le q \le 100\,000$$$) — the number of employees and the number of plans, respectively. The second line contains $$$n-1$$$ integers $$$p_2, p_3, \dots, p_n$$$ ($$$1 \le p_i \le n$$$) meaning $$$p_i$$$ is the direct boss of employee $$$i$$$. It is guaranteed, that values $$$p_i$$$ form a directed tree with the root of $$$1$$$. Each of the following $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i<r_i \le n$$$) — the range of the employees, involved in the corresponding plan.

Print $$$q$$$ lines, each containing two integers — the number of the employee which should be kicked from the corresponding plan and the maximum possible level of the project manager in that case. If there are more than one way to choose that employee, print any of them.

NoteIn the example:    In the first query, we can choose whether $$$4$$$ or $$$5$$$ or $$$6$$$ and the project manager will be $$$3$$$.In the second query, if we choose any employee other than the employee $$$8$$$, the project manager will be $$$1$$$. If we choose $$$8$$$, the project manager will be $$$3$$$. Since $$$lv(3)=1 > lv(1)=0$$$, choosing $$$8$$$ is the best strategy.In the third query, no matter how we choose the employee, the project manager will always be $$$1$$$.In the fourth query, if we choose $$$9$$$ or $$$10$$$ then the project manager will be $$$3$$$. If we choose $$$11$$$ then the project manager will be $$$7$$$. Since $$$lv(7)=3>lv(3)=1$$$, we choose $$$11$$$ as the answer.

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, Q = ni()

    val from, to = Array.ofDim[Int](N - 1)
    rep(N - 1) { i =>
      from(i) = ni() - 1
      to(i) = i + 1
    }

    val g = packUGraph(N, from, to)

    val NN = N
    val K = 20

    val (depth, parent, _) = traceBfs(g)
    parent(0) = 0
    val anc = Array.ofDim[Int](K, NN)
    rep(NN) { i =>
      anc(0)(i) = parent(i)
    }
    rep(anc.length - 1, 1) { k =>
      rep(NN) { i =>
        anc(k)(i) = anc(k-1)(anc(k-1)(i))
      }
    }

    def getParent(v: Int, d: Int): Int = {
      def step(v0: Int, k: Int, d0: Int): Int = {
        if (d0 == 0) v0
        else if (d0 % 2 == 1) step(anc(k)(v0), k + 1, d0 / 2)
        else step(v0, k + 1, d0 / 2)
      }

      step(v, 0, d)
    }

    val INF = Integer.MAX_VALUE
    def lca(v: Int, u: Int): Int = {
      if (v == INF) u
      else if (u == INF) v
      else {
        def step(v0: Int, u0: Int, k: Int): Int = {
          if (k == -1) {
            anc(0)(v0) // １つ前の子を返したかったら(v0, u0)を返す
          } else if (anc(k)(v0) == anc(k)(u0)) {
            step(v0, u0, k - 1)
          } else {
            step(anc(k)(v0), anc(k)(u0), k - 1)
          }
        }

        val d = min(depth(v), depth(u))
        val v0 = getParent(v, depth(v) - d)
        val u0 = getParent(u, depth(u) - d)
        if (v0 == u0) v0 // この時点で同じだったらダブリングで調べる必要がない
        else step(v0, u0, anc.length - 1)
      }
    }

    // 範囲lca
    val t1 = new SegmentTree(N, INF)(lca)

    // (元になるノード, lca)
    case class NeighbourLca(v: Int, lca: Int)
    // 区間の個数はN-1
    val t2 = new SegmentTree(N - 1, NeighbourLca(INF, INF))({ (u, v) =>
      if (lca(u.lca, v.lca) == u.lca) u else v
    })
    rep(N) { i =>
      t1.update(i, i)
    }
    rep(N - 1) { i =>
      val lca0 = lca(i, i + 1)
      t2.update(i, NeighbourLca(i, lca0))
    }

    rep(Q) { _ =>
      val l, r = ni() - 1 // [l, r] 最低２要素ある

      // delを削除した場合の区間全体でのLCAのlevelを返す
      def tryIt(del: Int): Int = {
        val node = if (del == l) t1.query(l + 1, r + 1)
        else if (del == r) t1.query(l, r)
        else lca(t1.query(l, del), t1.query(del + 1, r + 1))
        depth(node)
      }

      val ms = t2.query(l, r) // 要素[l, r] => 区間では[l, r-1]
      val lvl1 = tryIt(ms.v) // 区間の左の要素を削除
      val lvl2 = tryIt(ms.v + 1) // 区間の右を削除
      if (lvl1 > lvl2) {
        out.println(s"${ms.v + 1} $lvl1")
      } else {
        out.println(s"${ms.v + 2} $lvl2")
      }
    }
  }

  /**
    * @param n ノード数
    */
  def packUGraph(n: Int, from: Array[Int], to: Array[Int]): Array[Array[Int]] = {
    val t = new Array[Array[Int]](n)
    val p = new Array[Int](n)
    val m = from.length
    rep(m)(i => p(from(i)) += 1)
    rep(m)(i => p(to(i)) += 1)
    rep(n)(i => t(i) = new Array(p(i)))
    rep(m) { i =>
      p(from(i)) -= 1
      t(from(i))(p(from(i))) = to(i)
      p(to(i)) -= 1
      t(to(i))(p(to(i))) = from(i)
    }
    t
  }

  /**
    * @return (depth, parent, queue)
    */
  def traceBfs(g: Array[Array[Int]], rt: Int = 0): (Array[Int], Array[Int], Array[Int]) = {
    val n = g.length
    val INF = 1e9.toInt + 10
    val q, p = Array.ofDim[Int](n)
    q(0) = rt
    p(rt) = -1
    val d = Array.fill[Int](n)(INF)
    d(rt) = 0
    var cur = 0
    var last = 1
    while (cur < last) {
      val v = q(cur)
      rep(g(v).length) { i =>
        val u = g(v)(i)
        if (d(u) == INF) {
          d(u) = d(v) + 1
          p(u) = v
          q(last) = u
          last += 1
        }
      }
      cur += 1
    }
    (d, p, q)
  }

  /**
    * @param n 個数 最大値じゃないぞ。
    * iの範囲は[0, n - 1]
    */
  class SegmentTree[A: ClassTag](n: Int, zero: A)(f: (A, A) => A) {
    private val N = {
      val a = Integer.highestOneBit(n)
      if (a == n) a else a << 1
    }
    private val dat: Array[A] = Array.fill(2 * N - 1)(zero)

    def update(i: Int, a: A): Unit = {
      assert(i < n)
      var k = N - 1 + i
      dat(k) = a
      while(k > 0) {
        k = (k - 1) / 2
        dat(k) = f(dat(2 * k + 1), dat(2 * k + 2))
      }
    }

    /**
      * [a, b)
      */
    def query(a: Int, b: Int): A = {
      assert(a <= n && b <= n && a <= b)

      def step(k: Int, l: Int, r: Int): A = {
        if (r <= a || b <= l) {
          zero
        } else if (a <= l && r <= b) {
          dat(k)
        } else {
          val vl = step(k * 2 + 1, l, (l + r) / 2)
          val vr = step(k * 2 + 2, (l + r) / 2, r)
          f(vl, vr)
        }
      }

      if (a == b) zero
      else step(0, 0, N)
    }
  }

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


Polycarp wants to buy exactly $$$n$$$ shovels. The shop sells packages with shovels. The store has $$$k$$$ types of packages: the package of the $$$i$$$-th type consists of exactly $$$i$$$ shovels ($$$1 \le i \le k$$$). The store has an infinite number of packages of each type.Polycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly $$$n$$$ shovels?For example, if $$$n=8$$$ and $$$k=7$$$, then Polycarp will buy $$$2$$$ packages of $$$4$$$ shovels.Help Polycarp find the minimum number of packages that he needs to buy, given that he:   will buy exactly $$$n$$$ shovels in total;  the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from $$$1$$$ to $$$k$$$, inclusive.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then, $$$t$$$ test cases follow, one per line. Each test case consists of two positive integers $$$n$$$ ($$$1 \le n \le 10^9$$$) and $$$k$$$ ($$$1 \le k \le 10^9$$$) — the number of shovels and the number of types of packages.

Print $$$t$$$ answers to the test cases. Each answer is a positive integer — the minimum number of packages.

NoteThe answer to the first test case was explained in the statement.In the second test case, there is only one way to buy $$$8$$$ shovels — $$$8$$$ packages of one shovel.In the third test case, you need to buy a $$$1$$$ package of $$$6$$$ shovels.

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

import scala.reflect.ClassTag

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

  private[this] val isOnlineJudge = true

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

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

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

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

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i);
      i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i);
      i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }

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

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

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

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

  import Main._
  import fScanner._

  def solve(): Unit = {
    val t = ni()
    REP(t) { _ =>
      val n = ni()
      val k = ni()

      if(k >= n) {
        out.println(1)
      } else {
        val l = math.ceil(math.sqrt(n.toDouble)).toInt
        var ans = n
        2 to l by 1 foreach { f =>
          if(n % f == 0) {
            val u = n / f
            if(u <= k) ans = math.min(ans, f)
            if(f <= k) ans = math.min(ans, u)
          }
        }
        out.println(ans)
      }
    }
  }
}

object D extends App {
  val t = scala.io.StdIn.readInt()

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

    val ans = n / math
      .sqrt(n)
      .toLong
      .to(1, -1)
      .collect {
        case x if n % x == 0 => List(x, n / x).filter(_ <= k)
      }
      .flatten
      .max

    println(ans)
  }
}

object D extends App {
  val t = scala.io.StdIn.readInt()

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

    def x(n: Long): Long =
      if (n <= k) 1
      else if (n % k == 0) n / k
      else if (n % 2 == 0) 2 * x(n / 2)
      else n

    val ans = x(n)

    println(ans)
  }
}

object D extends App {
  val t = scala.io.StdIn.readInt()

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

    val ans =
      if (n <= k) 1
      else
        k.min(math.sqrt(n).toLong)
          .until(1, -1)
          .find(n % _ == 0)
          .map(x => List(x, n / x).filter(_ <= k).max)
          .getOrElse(n)

    println(ans)
  }
}

object D extends App {
  val t = scala.io.StdIn.readInt()

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

    def x(n: Long): Long =
      if (n <= k) 1
      else if (n % 2 == 0) 2 * x(n / 2)
      else n

    val ans = x(n)

    println(ans)
  }
}

object D extends App {
  val t = scala.io.StdIn.readInt()

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

    val ans =
      if (n <= k) 1
      else k.min(math.sqrt(n).toLong).until(1, -1).find(n % _ == 0).getOrElse(n)

    println(ans)
  }
}

object D extends App {
  val t = scala.io.StdIn.readInt()

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

    val ans =
      if (n <= k) 1
      else
        k.min(math.sqrt(n).toLong).until(1, -1).find(n % _ == 0).map(n / _).getOrElse(n)

    println(ans)
  }
}