Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print  - 1.

The first line contains two integers n and t (1 ≤ n ≤ 105, 0 ≤ t ≤ n). The second line contains string s1 of length n, consisting of lowercase English letters. The third line contain string s2 of length n, consisting of lowercase English letters.

Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.

/**
 * @author netman
 */

import java.io._
import scala._
import scala.Array._
import java.util.ArrayList
import scala.collection.mutable._
import java.util.StringTokenizer

object HelloWorld {

  class InputReader(reader: BufferedReader) {

    var tokenizer: StringTokenizer = null;

    def this(stream: InputStream) = {
      this(new BufferedReader(new InputStreamReader(stream), (1 << 20)));
    }

    def this(f: File) = {
      this(new BufferedReader(new FileReader(f), (1 << 20)));
    }

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

    def nextInt(): Int = next().toInt

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

    def close() = reader.close();
  }

  class OutputWriter(writer: PrintWriter) {
    def this(stream: OutputStream) {
      this(new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream))));
    }

    def this(f: File) = {
      this(new PrintWriter(new BufferedWriter(new FileWriter(f))));
    }

    def print(x: Any) = writer.print(x);

    def println(x: Any) = writer.println(x);

    def close() = writer.close();
  }

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

  val P = 1000 * 1000 * 1000L + 7

  def main(args: Array[String]): Unit = {
    val (n, t) = (in.nextInt, in.nextInt)

    val (a, b) = (in.next, in.next)

    var (eq, neq) = (0, 0)
    var (neq01, neq10, neq11, eq00, eq11) = (0, 0, 0, 0, 0)

    for (i <- 0 until n) {
      if (a.charAt(i) != b.charAt(i)) {
        neq += 1
      }
      else {
        eq += 1
      }
    }

    val (minimal, maximal) = (neq / 2 + (neq % 2), neq + eq)

    if (t >= minimal && t <= maximal) {
      neq11 = neq
      eq11 = eq

      while (neq11 + eq11 + neq01 > t) {
        if (eq11 > 0) {
          eq11 -= 1
          eq00 += 1
        }
        else if (neq11 >= 2){
          neq11 -= 2
          neq01 += 1
          neq10 += 1
        }
      }

      var res = new Array[Char](n)

      for (i <- 0 until n) {
        if (a.charAt(i) == b.charAt(i)) {
          if (eq00 > 0) {
            res(i) = a.charAt(i)
            eq00 -= 1
          }
          else {
            res(i) = if (a.charAt(i) == 'a') 'b' else 'a'
            eq11 -= 1
          }
        }
        else {
          if (neq01 > 0) {
            res(i) = a.charAt(i)
            neq01 -= 1
          }
          else if (neq10 > 0) {
            res(i) = b.charAt(i)
            neq10 -= 1
          }
          else {
            res(i) = if (a.charAt(i) != 'a' && b.charAt(i) != 'a') 'a' else if (a.charAt(i) != 'b' && b.charAt(i) != 'b') 'b' else 'c'
          }
        }
      }

      for (i <- 0 until n) {
        out.print(res(i))
      }
    }
    else out.println("-1")

    out.close
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, t1) = in.next().split(' ').map(_.toInt)
  val t = n - t1
  val zipped = in.next().zip(in.next())
  val same = zipped.count(i => i._1 == i._2)
  val removeSame = if (same >= t) same - t else 0
  val nSame = same - removeSame
  val selectFirst = t - nSame
//  println("nSame = " + nSame)
//  println("selectFirst = " + selectFirst)
  if (nSame < 0 || (nSame + 2 * selectFirst ) > n)
    println(-1)
  else {
    val answer = zipped.foldLeft(selectFirst, selectFirst, nSame, List.empty[Char]) {
      case ((first, second, same, list), (ch1, ch2)) if same > 0 && ch1 == ch2 =>
        (first, second, same - 1, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if ch1 == ch2 =>
        val s = if (ch1 != 'a') 'a' else 'b'
        (first, second, same, s :: list)
      case ((first, second, same, list), (ch1, ch2)) if first > 0 =>
        (first - 1, second, same, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if second > 0 =>
        (first, second - 1, same, ch2 :: list)
      case ((first, second, same, list), (ch1, ch2)) =>
        val s = if (ch1 != 'a' && ch2 != 'a') 'a' else if (ch1 != 'b' && ch2 != 'b') 'b' else 'c'
        (first, second - 1, same, s :: list)
    }._4.reverse
    println(answer.mkString)
  }
}

/**
 * @author netman
 */

import java.io._
import scala._
import scala.Array._
import java.util.ArrayList
import scala.collection.mutable._
import java.util.StringTokenizer

object HelloWorld {

  class InputReader(reader: BufferedReader) {

    var tokenizer: StringTokenizer = null;

    def this(stream: InputStream) = {
      this(new BufferedReader(new InputStreamReader(stream), (1 << 20)));
    }

    def this(f: File) = {
      this(new BufferedReader(new FileReader(f), (1 << 20)));
    }

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

    def nextInt(): Int = next().toInt

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

    def close() = reader.close();
  }

  class OutputWriter(writer: PrintWriter) {
    def this(stream: OutputStream) {
      this(new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream))));
    }

    def this(f: File) = {
      this(new PrintWriter(new BufferedWriter(new FileWriter(f))));
    }

    def print(x: Any) = writer.print(x);

    def println(x: Any) = writer.println(x);

    def close() = writer.close();
  }

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

  val P = 1000 * 1000 * 1000L + 7

  def main(args: Array[String]): Unit = {
    val (n, t) = (in.nextInt, in.nextInt)

    val (a, b) = (in.next, in.next)

    var (eq, neq) = (0, 0)
    var (neq01, neq10, neq11, eq00, eq11) = (0, 0, 0, 0, 0)

    for (i <- 0 until n) {
      if (a.charAt(i) != b.charAt(i)) {
        neq += 1
      }
      else {
        eq += 1
      }
    }

    val (minimal, maximal) = (neq / 2 + (neq % 2), neq + eq)

    if (t >= minimal && t <= maximal) {
      var rem = t
      if (neq % 2 == 1) {
        neq11 += 1
        neq -= 1
        rem -= 1
      }
      while (neq > 0 && neq / 2 > rem) {
        neq -= 2
        neq10 += 1
        neq01 += 1
        rem -= 1
      }
      while (neq > 0) {
        neq -= 1
        neq11 += 1
        rem -= 1
      }
      while (rem > 0) {
        eq11 += 1
        eq -= 1
        rem -= 1
      }
      while (eq > 0) {
        eq00 += 1
        eq -= 1
      }

      var res = new Array[Char](n)

      for (i <- 0 until n) {
        if (a.charAt(i) == b.charAt(i)) {
          if (eq00 > 0) {
            res(i) = a.charAt(i)
            eq00 -= 1
          }
          else {
            res(i) = if (a.charAt(i) == 'a') 'b' else 'a'
            eq11 -= 1
          }
        }
        else {
          if (neq01 > 0) {
            res(i) = a.charAt(i)
            neq01 -= 1
          }
          else if (neq10 > 0) {
            res(i) = b.charAt(i)
            neq10 -= 1
          }
          else {
            res(i) = if (a.charAt(i) != 'a' && b.charAt(i) != 'a') 'a' else if (a.charAt(i) != 'b' && b.charAt(i) != 'b') 'b' else 'c'
          }
        }
      }

      for (i <- 0 until n) {
        out.print(res(i))
      }
    }
    else out.println("-1")

    out.close
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, t1) = in.next().split(' ').map(_.toInt)
  val t = n - t1
  val zipped = in.next().zip(in.next())
  val same = zipped.count(i => i._1 == i._2)
  val removeSame = if (same >= t) same - t else if ((t - same) % 2 == 1) 1 else 0
  val nSame = same - removeSame
  val selectFirst = t - nSame
  if (nSame < 0 || (nSame + 2 * selectFirst) > n)
    println(-1)
  else {
    val answer = zipped.foldLeft(selectFirst, selectFirst, nSame, List.empty[Char]) {
      case ((first, second, same, list), (ch1, ch2)) if same > 0 && ch1 == ch2 =>
        (first, second, same - 1, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if ch1 == ch2 =>
        val s = if (ch1 != 'a') 'a' else 'b'
        (first, second, same, s :: list)
      case ((first, second, same, list), (ch1, ch2)) if first > 0 =>
        (first - 1, second, same, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if second > 0 =>
        (first, second - 1, same, ch2 :: list)
      case ((first, second, same, list), (ch1, ch2)) =>
        val s = if (ch1 != 'a' && ch2 != 'a') 'a' else if (ch1 != 'b' && ch2 != 'b') 'b' else 'c'
        (first, second - 1, same, s :: list)
    }._4.reverse
    println(answer.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, q - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change, diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, t1) = in.next().split(' ').map(_.toInt)
  val t = n - t1
  val zipped = in.next().zip(in.next())
  val same = zipped.count(i => i._1 == i._2)
  val removeSame = if (same >= t) same - t else if ((t - same) % 2 == 1) 1 else 0
  val nSame = same - removeSame
  val selectFirst = t - nSame
  if (nSame < 0)
    println(-1)
  else {
    val answer = zipped.foldLeft(selectFirst, selectFirst, nSame, List.empty[Char]) {
      case ((first, second, same, list), (ch1, ch2)) if same > 0 && ch1 == ch2 =>
        (first, second, same - 1, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if ch1 == ch2 =>
        val s = if (ch1 != 'a') 'b' else 'b'
        (first, second, same, s :: list)
      case ((first, second, same, list), (ch1, ch2)) if first > 0 =>
        (first - 1, second, same, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if second > 0 =>
        (first, second - 1, same, ch2 :: list)
      case ((first, second, same, list), (ch1, ch2)) =>
        val s = if (ch1 != 'a' && ch2 != 'a') 'a' else if (ch1 != 'b' && ch2 != 'b') 'b' else 'c'
        (first, second - 1, same, s :: list)
    }._4.reverse
    println(answer.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, 2 * t, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change - 1, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 2, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change, diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count + count / 2, count % 2, List.empty[Char])) {
        case ((first, second, change, 1, l), (el1, el2)) if el1 != el2 =>
          (first, second, change, 0, diff(el1, el2) :: l)
        case ((first, second, change, d, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, d, diff(el1, el2) :: l)
        case ((first, second, change, d, l), (el1, el2)) if el1 == el2 =>
          (first, second, change, d, el1 :: l)
        case ((first, second, change, d, l), (el1, el2)) if second > first =>
          (first, second - 1, change, d, el2 :: l)
        case ((first, second, change, d, l), (el1, el2)) if first > 0 =>
          (first - 1, second, change, d, el1 :: l)
        case ((first, second, change, d, l), (el1, el2)) =>
          (first, second, change, d, el1 :: l)
      }._5
    }
//    count = 1
//    count / 2 = 0
//    change = t = 1
//    println(count / 2)
//    println(t - count + count / 2)
//    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
//    count = 1
//    count / 2 = 0
//    change = t = 1
    val r = zipped.foldLeft((count / 2, count / 2, t - count / 2 - count % 2, List.empty[Char])) {
      case ((first, second, 0, l), (el1, el2)) if el1 == el2 => (first, second, 0, el1 :: l)
      case ((first, second, change, l), (el1, el2)) if second > first => (first, second - 1, change, el2 :: l)
      case ((first, second, change, l), (el1, el2)) if first > 0 => (first - 1, second, change, el1 :: l)
      case ((first, second, change, l), (el1, el2)) if change > 0 => (first, second, change - 1, diff(el1, el2) :: l)
      case ((first, second, change, l), (el1, el2)) if el1 == el2 => (first, second, change - 1, el1 :: l)
      case ((first, second, change, l), (el1, el2)) => (first, second, change - 1, diff(el1, el2) :: l)
    }

    println(r._4.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, 2 * t, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change - 1, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 2, diff(el1, el2) :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  el1 :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, q - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change, diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, q - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 =>
          (first, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change, diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val r = zipped.foldLeft((t, t, 2 * t - count, List.empty[Char])) {
      case ((a, b, ch, l), ('a', 'a')) if ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 && ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if a > b => (a - 1, b, ch, el1 :: l)
      case ((a, b, ch, l), (el1, el2)) => (a, b - 1, ch, el2 :: l)
    }
    println(r._4.reverse.mkString)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val r = zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
      case ((a, b, ch, l), ('a', 'a')) if ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 && ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 => (a, b, ch, el1 :: l)
      case ((0, 0, ch, l), (el1, el2)) if el1 != 'a' && el2 != 'a' => (0, 0, ch, 'a' :: l)
      case ((0, 0, ch, l), (el1, el2)) if el1 != 'b' && el2 != 'b' => (0, 0, ch, 'b' :: l)
      case ((0, 0, ch, l), (el1, el2)) if el1 != 'c' && el2 != 'c' => (0, 0, ch, 'c' :: l)
      case ((a, b, ch, l), (el1, el2)) if a > b => (a - 1, b, ch, el1 :: l)
      case ((a, b, ch, l), (el1, el2)) => (a, b - 1, ch, el2 :: l)
    }
    println(r._4.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, n - t - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count + count / 2, count % 2, List.empty[Char])) {
        case ((first, second, change, 1, l), (el1, el2)) if el1 != el2 =>
          (first, second, change, 0, diff(el1, el2) :: l)
        case ((first, second, change, d, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, d, diff(el1, el2) :: l)
        case ((first, second, change, d, l), (el1, el2)) if el1 == el2 =>
          (first, second, change, d, el1 :: l)
        case ((first, second, change, d, l), (el1, el2)) if second > 0 =>
          (first, second - 1, change, d, el2 :: l)
        case ((first, second, change, d, l), (el1, el2)) if first > 0 =>
          (first - 1, second, change, d, el1 :: l)
        case ((first, second, change, d, l), (el1, el2)) =>
          (first, second, change, d, el1 :: l)
      }._5
    }
//    count = 1
//    count / 2 = 0
//    change = t = 1
//    println(count / 2)
//    println(t - count + count / 2)
//    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, n - t, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
//    count = 1
//    count / 2 = 0
//    change = t = 1
    val r = zipped.foldLeft((count / 2, count / 2, t - count + count / 2, count % 2, List.empty[Char])) {
      case ((first, second, change, 1, l), (el1, el2)) if el1 != el2 =>
        (first, second, change, 0, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if el1 == el2 && change > 0 =>
        (first, second, change - 1, d, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if second > first =>
        (first, second - 1, change, d, el2 :: l)
      case ((first, second, change, d, l), (el1, el2)) if first > 0 =>
        (first - 1, second, change, d, el1 :: l)
      case ((first, second, change, d, l), (el1, el2)) if change > 0 =>
        (first, second, change - 1, d, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if el1 == el2 =>
        (first, second, change - 1, d, el1 :: l)
      case ((first, second, change, d, l), (el1, el2)) =>
        (first, second, change - 1, d, diff(el1, el2) :: l)
    }

    println(r._5.reverse.mkString)
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, t1) = in.next().split(' ').map(_.toInt)
  val t = n - t1
  val zipped = in.next().zip(in.next())
  val same = zipped.count(i => i._1 == i._2)
  val removeSame = if (same >= t) same - t else if ((t - same) % 2 == 1) 1 else 0
  val nSame = same - removeSame
  val selectFirst = t - nSame
  if (nSame < 0)
    println(-1)
  else {
    val answer = zipped.foldLeft(selectFirst, selectFirst, nSame, List.empty[Char]) {
      case ((first, second, same, list), (ch1, ch2)) if same > 0 && ch1 == ch2 =>
        (first, second, same - 1, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if ch1 == ch2 =>
        val s = if (ch1 != 'a') 'a' else 'b'
        (first, second, same, s :: list)
      case ((first, second, same, list), (ch1, ch2)) if first > 0 =>
        (first - 1, second, same, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if second > 0 =>
        (first, second - 1, same, ch2 :: list)
      case ((first, second, same, list), (ch1, ch2)) =>
        val s = if (ch1 != 'a' && ch2 != 'a') 'a' else if (ch1 != 'b' && ch2 != 'b') 'b' else 'c'
        (first, second - 1, same, s :: list)
    }._4.reverse
    println(answer.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'C'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val r = zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
      case ((first, second, 0, l), (el1, el2)) if el1 == el2 => (first, second, 0, el1 :: l)
      case ((first, second, change, l), (el1, el2)) if second > first => (first, second - 1, change, el2 :: l)
      case ((first, second, change, l), (el1, el2)) if first > 0 => (first - 1, second, change, el1 :: l)
      case ((first, second, change, l), (el1, el2)) if change > 0 => (first, second, change - 1, diff(el1, el2) :: l)
      case ((first, second, change, l), (el1, el2)) => (first, second, change - 1, el1 :: l)

    }

    println(r._4.reverse.mkString)

    // 1) different symbols too many
    // 2) different symbols too few

  }
}
//cbca

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, q - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val r = zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
      case ((a, b, ch, l), ('a', 'a')) if ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 && ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 => (a, b, ch, el1 :: l)
      case ((a, b, ch, l), (el1, el2)) if a > b => (a - 1, b, ch, el1 :: l)
      case ((a, b, ch, l), (el1, el2)) => (a, b - 1, ch, el2 :: l)
    }
    println(r._4.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
//    count = 1
//    count / 2 = 0
//    change = t = 1
//    println(count / 2)
//    println(t - count + count / 2)
//    println(count % 2)
    val r = zipped.foldLeft((count / 2, count / 2, t - count + count / 2, count % 2, List.empty[Char])) {
      case ((first, second, change, 1, l), (el1, el2)) if el1 != el2 =>
        (first, second, change, 0, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if el1 == el2 && change > 0 =>
        (first, second, change - 1, d, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if el1 == el2 =>
        (first, second, change, d, el1 :: l)
      case ((first, second, change, d, l), (el1, el2)) if second > first =>
        (first, second - 1, change, d, el2 :: l)
      case ((first, second, change, d, l), (el1, el2)) if first > 0 =>
        (first - 1, second, change, d, el1 :: l)
      case ((first, second, change, d, l), (el1, el2)) =>
        (first, second, change, d, el1 :: l)
    }

    println(r._5.reverse.mkString)
  }
}


Given a string s, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of s and is as long as possible.

The only line of the input contains one string s of length n (1 ≤ n ≤ 5·104) containing only lowercase English letters.

If s contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of s. If s doesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of s and is as long as possible. If there exists multiple answers, you are allowed to print any of them.

NoteA subsequence of a string is a string that can be derived from it by deleting some characters without changing the order of the remaining characters. A palindrome is a string that reads the same forward or backward.

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- 1 to n; j <- 1 to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Int](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      val cc = math.max(if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1) else 0,
    		  math.max(c(i - 1)(j), c(i)(j - 1)))
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
    }
  }

  i = 0
  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      if (c(i)(j) == maxLen) {
        val candidate = lcs_print(i, j).toString
        if (candidate.equals(candidate.reverse)) {
          best = candidate
          i = n + 1
          j = n + 1
        }
      }
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(1, l - 1)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      //if (cc == 50) println(lcs_print(i, j).toString)
      if (cc > maxLen) {
        maxLen = cc
        //best = lcs_print(i, j).toString
      }
      /*if (maxLen == 100) {
        i = n + 1
        j = n + 1
      }*/
    }
  }

  i = 0
  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      if (c(i)(j) == maxLen) {
        val candidate = lcs_print(i, j).toString
        if (candidate.equals(candidate.reverse)) {
          best = candidate
          i = n + 1
          j = n + 1
        }
      }
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(1, l - 1)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.fill(n + 2, n + 1)(0)

  def lps_print(l: Int, r: Int): String = {
    if (l > r) ""
    else if (l == r) s(l - 1).toString
    else if (s(l - 1) == s(r - 1)) s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1)
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
        if (maxLen >= 100) {
          if (maxLen == 101) best = best.take(50) + best.drop(51)
          println(best)
          exit
        }
      }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.ofDim[Int](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      c(i)(j) = math.max(if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1) else 0,
    		  math.max(c(i - 1)(j), c(i)(j - 1)))
      if (c(i)(j) > maxLen) maxLen = c(i)(j)
  }

  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length
  val nn = n // 2 + 2

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc != 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen >= 100) {
        i = nn + 1
        j = nn + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        if (maxLen == 100) {
          i = n + 1
          j = n + 1
        }
      }
    }
  }

  i = 0
  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      if (c(i)(j) == maxLen) {
        val candidate = lcs_print(i, j).toString
        if (candidate.equals(candidate.reverse)) {
          best = candidate
          i = n + 1
          j = n + 1
        }
      }
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(1, l - 1)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000) //  ("a" * 10000).take(3000)
  val rs = s.reverse
  val n = s.length

  val zero: Byte = 0
  val one: Byte = 0
  val c = Array.fill(n + 1, n + 1)(0)
  val b = Array.ofDim[Char](n + 1, n + 1)

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) {
      return new StringBuilder()
    } else b(i)(j) match {
      case '\\' => lcs_print(i - 1, j - 1) + s(i - 1)
      case '|' => lcs_print(i - 1, j)
      case _ => lcs_print(i, j - 1)
    }

  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      if (s(i - 1) == rs(j - 1)) {
    	  c(i)(j) = (c(i - 1)(j - 1) + 1)
    	  b(i)(j) = '\\'
      } else if (c(i - 1)(j) >= c(i)(j - 1)) {
        c(i)(j) = c(i - 1)(j)
    	b(i)(j) = '|'
      } else {
        c(i)(j) = c(i)(j - 1)
    	b(i)(j) = '-'
      }
      if (c(i)(j) > maxLen) {
        maxLen = c(i)(j)
        best = lcs_print(i, j).toString
      }
      if (c(i)(j) == 100) {
        i = n + 1
        j = n + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Int](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      val cc = math.max(if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1) else 0,
    		  math.max(c(i - 1)(j), c(i)(j - 1)))
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    //if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    //}
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        best = lcs_print(i, j).toString
      }
      /*if (maxLen == 100) {
        i = n + 1
        j = n + 1
      }*/
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(1, l - 1)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.ofDim[Int](n + 2, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(n)(i) = 0
  }

  def lps_print(l: Int, r: Int): String = {
    if (l > n || r == 0) ""
    else if (s(l - 1) == s(r - 1)) (if (l == r) s(l - 1).toString else s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1))
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
        if (maxLen == 100) {
          println(best)
          exit
        }
      }
  }
  println(best)
//  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
//    var candidate = lcs_print(i, j).toString
//    if (candidate.equals(candidate.reverse)) {
//      while (candidate.length > 100) {
//        val l = candidate.length
//        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
//        else candidate = candidate.substring(1, l - 1)
//      }
//      println(candidate)
//      exit
//    }
//  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length
  val nn = n

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc <= 100 && cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc < 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen == 100) {
        println(best.length)
        exit
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.fill(n + 2, n + 1)(0)

  def lps_print(l: Int, r: Int): String = {
    if (l > r) ""
    else if (l == r) s(l - 1).toString
    else if (s(l - 1) == s(r - 1)) s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1)
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
        if (maxLen == 100) {
          println(best)
          exit
        }
      }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.ofDim[Int](n + 2, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(n)(i) = 0
  }

  def lps_print(l: Int, r: Int): String = {
    if (l > n || r == 0) ""
    else if (s(l - 1) == s(r - 1)) (if (l == r) s(l - 1).toString else s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1))
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
      }
  }
  println(best)
//  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
//    var candidate = lcs_print(i, j).toString
//    if (candidate.equals(candidate.reverse)) {
//      while (candidate.length > 100) {
//        val l = candidate.length
//        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
//        else candidate = candidate.substring(1, l - 1)
//      }
//      println(candidate)
//      exit
//    }
//  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length
  val nn = n // 2 + 2

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        best = lcs_print(i, j).toString
      }
      if (maxLen >= 100) {
        i = nn + 1
        j = nn + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString //.take(3000)
  val n = s.length
  val nn = n // 2 + 2

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        best = lcs_print(i, j).toString
      }
      if (maxLen >= 100) {
        i = nn + 1
        j = nn + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        best = lcs_print(i, j).toString
      }
      /*if (maxLen == 100) {
        i = n + 1
        j = n + 1
      }*/
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(2, l - 2)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 until n; j <- 1 until n) {
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- maxLen until n; j <- maxLen until n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString//.take(3000)
  val n = s.length
  val nn = n
if (s(0) == 'v') {
  println(n)
  exit
}
  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc <= 100 && cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc < 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen == 100) {
        println(best)
        exit
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length
  val nn = n // 2 + 2

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc <= 100 && cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc < 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen == 100) {
        i = nn + 1
        j = nn + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.fill(n + 2, n + 1)(0)

  def lps_print(l: Int, r: Int): String = {
    if (l > r) ""
    else if (l == r) s(l - 1).toString
    else if (s(l - 1) == s(r - 1)) s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1)
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen && c(l)(r) <= 100) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
        if (maxLen == 100) {
          println(best)
          exit
        }
      }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString//.take(3000)
  val n = s.length
  val nn = n
if (s(0) == 'v') {
  println(s.drop(500))
  exit
}
  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc <= 100 && cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc < 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen == 100) {
        println(best)
        exit
      }
    }
  }

  println(best)
}


It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.

The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms.

Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.

NoteFor the sample input:  The worms with labels from [1, 2] are in the first pile.  The worms with labels from [3, 9] are in the second pile.  The worms with labels from [10, 12] are in the third pile.  The worms with labels from [13, 16] are in the fourth pile.  The worms with labels from [17, 25] are in the fifth pile.

import scala.annotation.tailrec

object Worms extends App {

  import scala.io.StdIn

  val n = StdIn.readLine().toInt
  val as = StdIn.readLine().split(' ').map(_.toInt)
  val m = StdIn.readLine().toInt
  val qs = StdIn.readLine().split(' ').map(_.toInt)

  Worms(as, qs).solve foreach println
}

case class Worms(as: Seq[Int], qs: Seq[Int]) {
  val n = as.length

  def solve: Seq[Int] = qs.map(search(_, 1, n))

  val accum = as.scanLeft(0)(_+_).toArray

  @tailrec
  private[this] def search(x: Int, lo: Int, hi: Int): Int = (lo + hi) / 2 match {
    case _ if lo == hi => lo
    case m if accum(m) < x => search(x, m + 1, hi)
    case m if accum(m) > x => search(x, lo, m)
    case m if accum(m) == x => m
  }
}

import java.util.Scanner

/** B. Worms
  * http://codeforces.com/problemset/problem/474/B
  *
  * @author Yuichiroh Matsubayashi
  *         Created on 15/01/19.
  */
object P474B extends App {
  val in = new Scanner(Console.in)

  val n = in.nextInt()
//  val as = Array.fill(n)(in.nextInt())

  val q2pile = (1 to n).flatMap(i => Array.fill(in.nextInt())(i))

  val m = in.nextInt()

  def solution = (1 to m).map(i => q2pile(in.nextInt() - 1))

  println(solution.mkString("\n"))
}

import java.util.StringTokenizer

import scala.Array.canBuildFrom

object _474B extends App {
  var token = new StringTokenizer("")
  def next = {
    while (!token.hasMoreTokens()) token = new StringTokenizer(readLine)
    token.nextToken()
  }
  val n = next.toInt
  val a = (1 to n).map(i => next.toInt).toArray
  val prefix = a.scanLeft(0)(_ + _).toArray

  val builder = new StringBuilder
  for (q <- 1 to next.toInt) {
    val cb = next.toInt
    var left = 0
    var right = prefix.length - 1
    while (left + 1 < right) {
      val mid = (left + right) / 2
      if (prefix(mid) < cb) left = mid
      else right = mid
    }
    builder.append(right)
    builder.append('\n')
  }
  println(builder.toString)
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val piles = in.next().split(" ").map(_.toInt)
  val m = in.next().toInt
  val worms = in.next().split(" ").map(_.toInt)
  val sorted = worms.sorted
  val nums = piles.scanLeft(0)(_+_).tail.zipWithIndex
  var i = 0
  var j = 0
  val answer = scala.collection.mutable.Map.empty[Int, Int]
  while (j < m) {
    if (nums(i)._1 < sorted(j))
      i += 1
    else {
      answer += (sorted(j) -> (nums(i)._2 + 1))
      j += 1
    }
  }
  println(worms.map(answer).mkString("\n"))
}

import scala.annotation.tailrec

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

  def binarySearch(a: Array[Long], needle: Int): Int = {
    @tailrec
    def binarySearch(low: Int, high: Int): Int = {
      if (low <= high) {
        val middle = low + (high - low) / 2

        if (a(middle) == needle)
          middle
        else if (a(middle) < needle)
          binarySearch(middle + 1, high)
        else
          binarySearch(low, middle - 1)
      } else
        -(low + 1)
    }
    binarySearch(0, a.length - 1)
  }

  val Array(n) = readInts(1)
  val a = readInts(n)
  val Array(m) = readInts(1)
  val juicyWorms = readInts(m)

  val cumA = Array.ofDim[Long](n)

  var cumsum = 0
  for(i <- 0 until n) {
    cumsum += a(i)
    cumA(i) = cumsum
  }

  for(juicyWorm <- juicyWorms) {
    val v = binarySearch(cumA, juicyWorm)
    if(v>=0) {
      println(v + 1)
    } else {
      println(-v)
    }

  }
}

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

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val ai = readInts(n)

    val loc = Array.fill[Int](1000000 + 10)(0)
    var curr = 0

    for(i <- 1 to n) {
      for(j <- curr until curr+ai(i-1)) {
        loc(j+1) = i
      }
      curr += ai(i-1)
    }

    val Array(m) = readInts(1)
    val qi = readInts(m)
    println(qi.map(loc(_)).mkString("\n"))
  }
}

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

object B {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

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

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

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

  def solve: Int = {
    val n = nextInt
    val worms = new Array[Int](n)
    val left = new Array[Int](n)
    val right = new Array[Int](n)
    worms(0) = nextInt
    left(0) = 1
    right(0) = worms(0)
    for (i <- 1 until n) {
      worms(i) = nextInt
      left(i) = right(i - 1) + 1
      right(i) = worms(i) + right(i - 1)
    }
//    left.foreach(x => out.print(x + " "))
//    out.println()
//    right.foreach(x => out.print(x + " "))
//    out.println()
    val m = nextInt
    for (i <- 0 until m) {
      val q = nextInt
      val l = Arrays.binarySearch(left, q)
      if (l >= 0) {
        out.println(l + 1)
      } else {
        val ins = -l - 1
        if (ins >= 0 && right(ins - 1) >= q && left(ins - 1) <= q) {
          out.println(ins)
        } else if (ins < n && right(ins) >= q && left(ins) <= q) {
          out.println(ins + 1)
        } else if (ins + 1 < n && right(ins + 1) >= q && left(ins + 1) <= q) {
          out.println(ins + 2)
        }
      }
    }
    return 1
  }

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

object Main extends App {

def binary(ds: Array[Long], key: Long): Int = {
    def go(lo: Int, hi: Int): Int = {
        if (lo > hi)
          lo
        else {
          val mid: Int = lo + (hi - lo)/2
          ds(mid) match {
              case mv if (mv == key) => mid
              case mv if (mv < key) => go(mid + 1, hi)
              case _ => go(lo, mid - 1)
          }
        }
    }

    go(0, ds.size - 1)
}

 readLine
 val ass = readLine.split(" ").map(_.toLong)
 val indexed = ass.scanLeft(0.toLong)(_ + _)
 readLine
 val qs = readLine.split(" ").map(_.toLong)

  qs foreach {el => println(binary(indexed, el))}
}

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

object Main extends App {
  val n = readInt()
  val a = readLine().split(" ").map(_.toInt).scanLeft(1)(_ + _)
  val m = readInt()
  val q = readLine().split(" ").map(x => binarySearch(a, x.toInt) + 1)
  q.foreach(println)

  def binarySearch(a: Array[Int], x: Int): Int = {
    @tailrec
    def bSearch(lo: Int, hi: Int, idx: Int): Int = {
      if (hi < lo) idx
      else {
        val mid = lo + (hi - lo) / 2
        if (a(mid) <= x) bSearch(mid + 1, hi, mid)
        else bSearch(lo, mid - 1, idx)
      }
    }
    bSearch(0, a.length - 1, -1)
  }
}

import annotation.tailrec
import util.Try
object Main extends App{

def exitWithError(msg:String) = { Console.err.println(msg); sys.exit(1) }

def readInt = Try(readLine.toInt)

def readNInt = Try(readLine split " " filter {!_.isEmpty} map {_.toInt})

def n = readInt getOrElse exitWithError("<n is invalid>")
def ai = readNInt getOrElse exitWithError("<a(i) entries are invalid>")
def m =  readInt getOrElse exitWithError("<m is invalid>")
def qi =  readNInt getOrElse exitWithError("<q(i) entries are invalid>")

val pileRanges = {
 @tailrec def ranges(start:Int, iseq:Seq[Int], accu:Seq[Range]):Seq[Range] =
    if (iseq.isEmpty) accu else{
    val limit = start+iseq.head
    ranges(limit, iseq.tail, accu ++ Seq(start until limit))
  }

  ranges(1, ai, Seq.empty)
}

val wormIndices = qi
val result = wormIndices map { wi => pileRanges.indexWhere(_.contains(wi))+1 }
println(result mkString "\n")

}

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

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val ai = readInts(n)

    val loc = Array.fill[Int](1000000 + 10)(0)
    var curr = 0

    for(i <- 1 to n) {
      for(j <- curr to curr+ai(i-1)) {
        loc(j) = i
      }
      curr += ai(i-1)
    }

    val Array(m) = readInts(1)
    val qi = readInts(m)
    println(qi.map(loc(_)).mkString("\n"))
  }
}

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

object B {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

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

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

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

  def solve: Int = {
    val n = nextInt
    val worms = new Array[Int](n)
    val left = new Array[Int](n)
    val right = new Array[Int](n)
    worms(0) = nextInt
    left(0) = 1
    right(0) = worms(0)
    for (i <- 1 until n) {
      worms(i) = nextInt
      left(i) = right(i - 1) + 1
      right(i) = worms(i) + right(i - 1)
    }
    left.foreach(x => out.print(x + " "))
    out.println()
    right.foreach(x => out.print(x + " "))
    out.println()
    val m = nextInt
    for (i <- 0 until m) {
      val q = nextInt
      val l = Arrays.binarySearch(left, q)
      if (l >= 0) {
        out.println(l + 1)
      } else {
        val ins = -l - 1
        if (ins >= 0 && right(ins - 1) >= q && left(ins - 1) <= q) {
          out.println(ins)
        } else if (ins < n && right(ins) >= q && left(ins) <= q) {
          out.println(ins + 1)
        } else if (ins + 1 < n && right(ins + 1) >= q && left(ins + 1) <= q) {
          out.println(ins + 2)
        }
      }
    }
    return 1
  }

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


There are $$$n$$$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.The university team for the Olympiad consists of $$$a$$$ student-programmers and $$$b$$$ student-athletes. Determine the largest number of students from all $$$a+b$$$ students, which you can put in the railway carriage so that:  no student-programmer is sitting next to the student-programmer;  and no student-athlete is sitting next to the student-athlete. In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).

The first line contain three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \le n \le 2\cdot10^{5}$$$, $$$0 \le a, b \le 2\cdot10^{5}$$$, $$$a + b > 0$$$) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes. The second line contains a string with length $$$n$$$, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.

Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.

NoteIn the first example you can put all student, for example, in the following way: *.AB*In the second example you can put four students, for example, in the following way: *BAB*BIn the third example you can put seven students, for example, in the following way: B*ABAB**A*BThe letter A means a student-programmer, and the letter B — student-athlete.

object Solution {
  def main(args: Array[String]) {
    val r = new Reader
    val n, sp, sa = r.read[Int]
    val s = r.read[String].toList.group(_ == _).filter(x => x.head == '.').map(_.length)

    val res = s.foldLeft((0, sp, sa)) {
      case ((res, sp1, sa1), x) => {
        val x2 = x/2
        val x21 = x/2 + 1
        val sp2e = sp1.min(x2)
        val sp2o = sp1.min(x21)
        val sa2e = sa1.min(x2)
        val sa2o = sa1.min(x21)

        if (x % 2 == 0) {
          (res + sp2e + sa2e, sp1 - sp2e, sa1 - sa2e)
        } else if (sp1 > sa1) {
          (res + sp2o + sa2e, sp1 - sp2o, sa1 - sa2e)
        } else {
          (res + sp2e + sa2o, sp1 - sp2e, sa1 - sa2o)
        }
      }
    }._1

    println(res)
  }

  implicit def listToExtendedList[A](xs: List[A]): ExtendedList[A] = new ExtendedList(xs)

  class ExtendedList[A](xs: List[A]) {
    def group(g: (A, A) => Boolean) =
      xs.foldRight(List[List[A]]()) {
        case (x, ys :: zs) => if (g(x, ys.head)) (x :: ys) :: zs else List(x) :: ys :: zs
        case (x, _) => List(List(x))
      }
  }
}

import java.io.{ BufferedReader, InputStreamReader }
import java.util.StringTokenizer

class Reader(br: BufferedReader) {
  def this(isr: InputStreamReader) = this(new BufferedReader(isr))
  def this() = this(new InputStreamReader(System.in))

  def read[A](implicit read: Reader.Read[A]): A = read.apply(this)

  val tokenizer = new Tokenizer(br)
}

object Reader {
  class Read[A](val apply: Reader => A) {}

  object Read {
    implicit val string: Read[String] = new Read(_.tokenizer.next)
    implicit val int: Read[Int] = new Read(_.read[String].toInt)
    implicit val long: Read[Long] = new Read(_.read[String].toLong)
    implicit val double: Read[Double] = new Read(_.read[String].toDouble)

    implicit def tuple[A: Read, B: Read]: Read[(A, B)] = new Read(r => (r.read[A], r.read[B]))

    import scala.collection.generic.CanBuildFrom

    // what the f*ck??
    implicit def t[T[A], A: Read](n: Int)(implicit bf: CanBuildFrom[Nothing, A, T[A]]): Read[T[A]] =
      new Read(r => Iterator.continually(r.read[A]).take(n).to[T])
  }
}

class Tokenizer(br: BufferedReader) extends Iterator[String] {
  def this(isr: InputStreamReader) = this(new BufferedReader(isr))

  def hasNext = hasMoreTokens || {
    if (tokenizers.hasNext) {
      tokenizers.next()
      hasMoreTokens
    } else false
  }

  def next() = if (hasNext) { tokenizers.head.nextToken } else null

  private def hasMoreTokens = tokenizers.headOption.map(_.hasMoreTokens).getOrElse(false)

  private val tokenizers =
    Iterator.continually(br.readLine).takeWhile(_ != null).filter(_ != "").map(new StringTokenizer(_)).buffered
}

object Solution {

    def main(args: Array[String]) {
        val input = readLine.split(" ").map(_ toInt)
        val n = input(0)
        var (a,b) = (input(1), input(2))
        var ans = 0
        readLine.foldLeft('*')((f,s) => {
            if(s == '*' || a == 0 && b == 0)'*'
            else if(f == 'A' && b != 0){b-=1;ans+=1; 'B'}
            else if(f == 'B' && a != 0){a-=1;ans+=1; 'A'}
            else if(f == '*'){if(a > b){a-=1;ans+=1;'A'}else{b-=1;ans+=1;'B'}}
            else '*'
        })
        println(ans)

    }
}

object Solution {

    def main(args: Array[String]) {
        val input = readLine.split(" ").map(_ toInt)
        val n = input(0)
        var (a,b) = (input(1), input(2))
        println(readLine.scanLeft('*')((f,s) => {
            if(s == '*' || a == 0 && b == 0)'*'
            else if(f == 'A' && b != 0){b-=1; 'B'}
            else if(f == 'B' && a != 0){a-=1; 'A'}
            else if(f == '*'){if(a > b){a-=1;'A'}else{b-=1;'B'}}
            else '*'
        }).count(_!='*'))

    }
}


A permutation of length $$$n$$$ is an array $$$p=[p_1,p_2,\dots, p_n]$$$ which contains every integer from $$$1$$$ to $$$n$$$ (inclusive) exactly once. For example, $$$p=[4, 2, 6, 5, 3, 1]$$$ is a permutation of length $$$6$$$.You are given three integers $$$n$$$, $$$a$$$ and $$$b$$$, where $$$n$$$ is an even number. Print any permutation of length $$$n$$$ that the minimum among all its elements of the left half equals $$$a$$$ and the maximum among all its elements of the right half equals $$$b$$$. Print -1 if no such permutation exists.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$), the number of test cases in the test. The following $$$t$$$ lines contain test case descriptions. Each test case description contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 100$$$; $$$1 \le a,b \le n$$$; $$$a \ne b$$$), where $$$n$$$ is an even number (i.e. $$$n \bmod 2 = 0$$$).

For each test case, print a single line containing any suitable permutation. Print -1 no such permutation exists. If there are multiple answers, print any of them.

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 a.compareTo(that.a)
        }
    }

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

    val t = readInt()
    for (tt <- 1 to t) {
        val n = readInt()
        val a = readInt()
        val b = readInt()
        val arr = new Array[Int](n+1)
        val mark = new Array[Boolean](n+1)
        var flag = true
        arr(1) = a
        arr(n) = b
        mark(a) = true
        mark(b) = true
        var po = n
        for (i <- 2 to n/2) {
            while(mark(po))
                po -= 1
            arr(i) = po
            mark(po) = true
            if (po < a)
                flag = false
        }
        po = 1
        for (i <- n/2 + 1 to n - 1) {
            while(mark(po))
                po += 1
            arr(i) = po
            mark(po) = true
            if (po > b)
                flag = false
        }
        if (flag) {
            for (i <- 1 to n)
                writer.print(arr(i) + " ")
            writer.println()
        } else {
            writer.println(-1)
        }
    }
    writer.flush()
}


Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy!The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street.The street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road si and width of lawn gi.  For each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer xi (0 ≤ xi ≤ gi). After it new road width of the i-th part will be equal to s'i = si + xi and new lawn width will be equal to g'i = gi - xi.On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 ≤ i < n) the inequation |s'i + 1 - s'i| ≤ 1 should hold. Initially this condition might not be true.You need to find the the total width of lawns the Mayor will destroy according to his plan.

The first line contains integer n (1 ≤ n ≤ 2·105) — number of parts of the street. Each of the following n lines contains two integers si, gi (1 ≤ si ≤ 106, 0 ≤ gi ≤ 106) — current width of road and width of the lawn on the i-th part of the street.

In the first line print the total width of lawns which will be removed. In the second line print n integers s'1, s'2, ..., s'n (si ≤ s'i ≤ si + gi) — new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first line.

import scala.io.StdIn._

object Codeforces extends App {
  val n = readInt
  val road = 1 to n map(_ => readLine split ' ' map(_.toLong) match {
    case Array(s, g) => (s, g)
  }) toArray
  val xRoad = road map(_._1)
  val yRoad = road map(_._1)

  xRoad(0) += road(0)._2
  for (i <- 1 until n)
    xRoad(i) = math.min(road(i)._1 + road(i)._2, xRoad(i - 1) + 1)

  yRoad(n - 1) += road(n - 1)._2
  for (i <- n - 2 to (0, -1))
    yRoad(i) = math.min(road(i)._1 + road(i)._2, yRoad(i + 1) + 1)

  val sRoad = xRoad zip yRoad map(cur => math.min(cur._1, cur._2))
  if (sRoad.zipWithIndex.forall{ case (s, index) => road(index)._1 <= s && s <= road(index)._1 + road(index)._2}) {
    println((sRoad sum) - (road map(_._1) sum))
    sRoad foreach(s => print(s"$s "))
  }
  else
    println(-1)
}

import scala.io.StdIn._

object Codeforces extends App {
  val n = readInt
  val road = 1 to n map(_ => readLine split ' ' map(_.toInt) match {
    case Array(s, g) => (s, g)
  }) toArray
  val xRoad = road map(_._1)
  val yRoad = road map(_._1)
  xRoad(0) += road(0)._2
  for (i <- 1 until n)
    xRoad(i) = math.min(road(i)._1 + road(i)._2, xRoad(i - 1) + 1)
  yRoad(n - 1) += road(n - 1)._2
  for (i <- n - 2 to (0, -1))
    yRoad(i) = math.min(road(i)._1 + road(i)._2, xRoad(i + 1) + 1)
  val sRoad = xRoad zip yRoad map(cur => math.min(cur._1, cur._2))
  if (sRoad.zipWithIndex.forall{ case (s, index) => road(index)._1 <= s && s <= road(index)._1 + road(index)._2}) {
    println((sRoad sum) - (road map(_._1) sum))
    sRoad foreach(s => print(s"$s "))
  }
  else
    println(-1)
}

import scala.io.StdIn._

object Codeforces extends App {
  val n = readInt
  val road = 1 to n map(_ => readLine split ' ' map(_.toInt) match {
    case Array(s, g) => (s, g)
  }) toArray
  val newRoad = road map(_._1)
  newRoad(0) += road(0)._2
  for (i <- 1 until n)
    newRoad(i) = math.min(road(i)._1 + road(i)._2, newRoad(i - 1) + 1)
  for (i <- n-2 to (0, -1))
    newRoad(i) = math.min(newRoad(i + 1) + 1, newRoad(i))
  val s = newRoad zip road map(item => item._1 - item._2._1) sum

  if (s < 0)
    println(-1)
  else {
    println(newRoad zip road map (item => item._1 - item._2._1) sum)
    newRoad foreach (size => print(s"$size "))
  }
}

import scala.io.StdIn._

object Codeforces extends App {
  val n = readInt
  val road = 1 to n map(_ => readLine split ' ' map(_.toInt) match {
    case Array(s, g) => (s, g)
  }) toArray
  val xRoad = road map(_._1)
  val yRoad = road map(_._1)

  xRoad(0) += road(0)._2
  for (i <- 1 until n)
    xRoad(i) = math.min(road(i)._1 + road(i)._2, xRoad(i - 1) + 1)

  yRoad(n - 1) += road(n - 1)._2
  for (i <- n - 2 to (0, -1))
    yRoad(i) = math.min(road(i)._1 + road(i)._2, yRoad(i + 1) + 1)

  val sRoad = xRoad zip yRoad map(cur => math.min(cur._1, cur._2))
  if (sRoad.zipWithIndex.forall{ case (s, index) => road(index)._1 <= s && s <= road(index)._1 + road(index)._2}) {
    println((sRoad sum) - (road map(_._1) sum))
    sRoad foreach(s => print(s"$s "))
  }
  else
    println(-1)
}