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

import collection.mutable.HashMap
import scala.collection.{SortedSet, mutable}
import scala.util.Random
  protected val EPS = 1e-11

  private def default = (
    new StreamTokenizer(new InputStreamReader(System.in)),
    new Scanner(System.in),
    new PrintWriter(System.out)
  )

  val (in, ins, out) = {
    try {
      if (sys.props.contains("xDx")) {
        (
          new StreamTokenizer(new FileReader("input.txt")),
          new Scanner(new FileReader("input.txt")),
          new PrintWriter(new FileWriter("output.txt"))
        )
      } else {
        default
      }
    } catch {
      case _: Throwable => default
    }
  }

  private def nextInt = {
    in.nextToken
    in.nval.toInt
  }

  private def nextLong = {
    in.nextToken
    in.nval.toLong
  }

  private def nextDouble = {
    in.nextToken
    in.nval
  }

  private def nextString = {
    in.nextToken
    in.sval
  }

  private def nextChar = {
    in.nextToken
    in.ttype.toChar
  }

  def main(args: Array[String]): Unit = {
    try {
      new Run().run()
    } catch {
      case e: Throwable =>
        throw new RuntimeException(e)
    } finally out.close()
  }

  class Run {
    import collection.breakOut
    def run(): Unit = {
        val n = nextInt
        val field = Array.ofDim[Int](2, n)
        val sum = Array.ofDim[Long](2, n)
        val sum123 = Array.ofDim[Long](2, n)
        val sum321 = Array.ofDim[Long](2, n)
        for(i <- 0 until 2) {
            for(j <- 0 until n) {
                val a = nextInt
                field(i)(j) = a
                sum(i)(j) = a
                if(j > 0) sum(i)(j) += sum(i)(j - 1)
            }
        }

        for(i <- 0 until 2) {
            for(j <- n - 1 to 0 by -1) {
                sum321(i)(j) = (n - 1  - j).toLong * field(i)(j)
                sum123(i)(j) = sum(i)(n - 1) - sum(i)(j)
                if(j < n - 1) {
                    sum321(i)(j) += sum321(i)(j + 1)
                    sum123(i)(j) += sum123(i)(j + 1)
                }
            }
        }

        var res = 0L
        var line = 0
        var time = 0L
        var acc = 0L
        def getSum(line: Int, i: Int) = {
            if(i < 0) 0 else sum(line)(i)
        }
        for(i <- 0 until n) {
            val ol = other(line)
            val tmp = sum123(line)(i) + time * (sum(line)(n - 1) - getSum(line, i - 1)) +
                sum321(ol)(i) + (time + n - i)*(sum(ol)(n - 1) - getSum(ol, i - 1))
            res = math.max(res, tmp + acc)
            acc += field(line)(i)*time + field(ol)(i)*(time + 1)

            time += 2
            line = ol
        }

        println(math.max(res, acc))
    }

    def other(line: Int) = (line + 1) % 2

  }

}

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val A = Array(na(N), na(N))
    var ans = 0L

    val S = Array.ofDim[Long](N + 1)
    REP_r(N) { i =>
      S(i) = S(i + 1) + A(0)(i) + A(1)(i)
    }
    val tour = Array.ofDim[Long](2, N + 1) // 0-indexed, ここからぐぐっと回った時に、係数0スタートで累積した値
    REP(2) { x =>
      REP_r(N) { i =>
        val end = A(x ^ 1)(i).toLong * ((N - i) * 2 - 1)
        tour(x)(i) = tour(x)(i + 1) + S(i + 1) + end
      }
    }

    var past = 0L
    REP(N) { i =>
      val x = i % 2
      val v0 = i * 2
      val v1 = v0 + 1
      ans = max(ans, past + tour(x)(i) + S(i) * v0)
      past += A(x)(i).toLong * v0 + A(x ^ 1)(i).toLong * v1
    }

    out.println(ans)
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val X = Array.ofDim[Long](2, N)
    rep(N) (i => X(0)(i) = ni())
    rep(N) (i => X(1)(i) = ni())

    val cum = Array.ofDim[Long](N + 1)
    rep(N) { i =>
      cum(i + 1) = cum(i) + X(0)(i) + X(1)(i)
    }

    def S(i: Int): Long = cum(N) - cum(i)

    def initOutside(i: Int, off: Int) = {
      val x = i % 2
      var res = 0L
      rep(N - i) { j =>
        res += X(x)(j + i) * (j + off)
      }
      rep(N - i) { j =>
        res += X(x ^ 1)(N - 1 - j) * (N - i + j + off)
      }
      res
    }

    val prevOutside = Array.ofDim[Long](2)
    prevOutside(0) = initOutside(0, 0)
    prevOutside(1) = initOutside(1, 2)

    // i: [0, N-1] 横の位置
    def collectOutside(i: Int): Long = {
      val x = i % 2
      if (i < 2) {
        prevOutside(x)
      } else {
        val k = (N - i + 2) * 2 - 1
        val off = (i - 2) * 2
        val res = prevOutside(x) +
          2 * S(i) -
          off * X(x)(i - 2) -
          (off + 1) * X(x)(i - 1) -
          (off + k) * X(x ^ 1)(i - 2) -
          (off + k - 1) * X(x ^ 1)(i - 1)
        prevOutside(x) = res
        res
      }
    }

    def step(i: Int) = {
      val x = i % 2
      i * 2 * X(x)(i) + (i * 2 + 1) * X(x ^ 1)(i)
    }

    var ans = 0L
    var cur = 0L
    rep(N - 1) { i =>
      ans = max(ans, collectOutside(i) + cur)
      cur += step(i)
    }
    ans = max(ans, cur + step(N - 1))

    out.println(ans)
  }

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val X = Array.ofDim[Int](2, N)
    rep(N) (i => X(0)(i) = ni())
    rep(N) (i => X(1)(i) = ni())

    val cum = Array.ofDim[Long](N + 1)
    rep(N - 1) { i =>
      cum(i + 1) = cum(i) + X(0)(i + 1) + X(1)(i + 1)
    }

    def S(i: Int): Long = cum(N - i)

    def initOutside(i: Int, off: Int) = {
      val x = i % 2
      var res = 0L
      rep(N - i) { j =>
        res += X(x)(j + i) * (j + off)
      }
      rep(N - i) { j =>
        res += X(x ^ 1)(N - 1 - j) * (N - i + j + off)
      }
      res
    }

    val prevOutside = Array.ofDim[Long](2)
    prevOutside(0) = initOutside(0, 0)
    prevOutside(1) = initOutside(1, 2)

    // i: [0, N-1] 横の位置
    def collectOutside(i: Int): Long = {
      val x = i % 2
      if (i < 2) {
        prevOutside(x)
      } else {
        val k = (N - i + 2) * 2 - 1
        val res = prevOutside(x) +
          2 * S(i) +
          2 * X(x)(i - 1) -
          (k - 2) * X(x ^ 1)(i - 2) -
          (k - 4) * X(x ^ 1)(i - 1)
        prevOutside(x) = res
        res
      }
    }

    def step(i: Int) = {
      val x = i % 2
      i * 2 * X(x)(i) + (i * 2 + 1) * X(x ^ 1)(i)
    }

    var ans = 0L
    var cur = 0L
    rep(N - 1) { i =>
      ans = max(ans, collectOutside(i) + cur)
      cur += step(i)
    }
    ans = max(ans, cur + step(N - 1))

    out.println(ans)
  }

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val A = Array(na(N), na(N))
    var ans = 0L

    val S = Array.ofDim[Long](N + 1)
    REP_r(N) { i =>
      S(i) = S(i + 1) + A(0)(i) + A(1)(i)
    }
    val tour = Array.ofDim[Long](2, N + 1) // 0-indexed, ここからぐぐっと回った時に、係数0スタートで累積した値
    REP(2) { x =>
      REP_r(N) { i =>
        val end = A(x ^ 1)(i) * ((N - i) * 2 - 1)
        tour(x)(i) = tour(x)(i + 1) + S(i + 1) + end
      }
    }

    var past = 0L
    REP(N) { i =>
      val x = i % 2
      val v0 = i * 2
      val v1 = v0 + 1
      ans = max(ans, past + tour(x)(i) + S(i) * v0)
      past += A(x)(i).toLong * v0 + A(x ^ 1)(i).toLong * v1
    }

    out.println(ans)
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val A = Array(na(N), na(N))
    var ans = 0L

    val S = Array.ofDim[Long](N + 1)
    REP_r(N) { i =>
      S(i) = S(i + 1) + A(0)(i) + A(1)(i)
    }
    val tour = Array.ofDim[Long](2, N + 1) // 0-indexed, ここからぐぐっと回った時に、係数0スタートで累積した値
    REP(2) { x =>
      REP_r(N) { i =>
        val end = A(x ^ 1)(i) * ((N - i) * 2 - 1)
        tour(x)(i) = tour(x)(i + 1) + S(i + 1) + end
      }
    }

    var past = 0L
    REP(N) { i =>
      val x = i % 2
      val v0 = i * 2
      val v1 = v0 + 1
      ans = max(ans, past + tour(x)(i) + S(i) * v0)
      past += A(x)(i) * v0 + A(x ^ 1)(i) * v1
    }

    out.println(ans)
  }

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

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N = ni()
    val X = Array.ofDim[Int](2, N)
    rep(N) (i => X(0)(i) = ni())
    rep(N) (i => X(1)(i) = ni())

    val cum = Array.ofDim[Long](N + 1)
    rep(N) { i =>
      cum(i + 1) = cum(i) + X(0)(i) + X(1)(i)
    }

    def S(i: Int): Long = cum(N) - cum(i)

    def initOutside(i: Int, off: Int) = {
      val x = i % 2
      var res = 0L
      rep(N - i) { j =>
        res += X(x)(j + i) * (j + off)
      }
      rep(N - i) { j =>
        res += X(x ^ 1)(N - 1 - j) * (N - i + j + off)
      }
      res
    }

    val prevOutside = Array.ofDim[Long](2)
    prevOutside(0) = initOutside(0, 0)
    prevOutside(1) = initOutside(1, 2)

    // i: [0, N-1] 横の位置
    def collectOutside(i: Int): Long = {
      val x = i % 2
      if (i < 2) {
        prevOutside(x)
      } else {
        val k = (N - i + 2) * 2 - 1
        val off = (i - 2) * 2
        val res = prevOutside(x) +
          2 * S(i) -
          off * X(x)(i - 2) -
          (off + 1) * X(x)(i - 1) -
          (off + k) * X(x ^ 1)(i - 2) -
          (off + k - 1) * X(x ^ 1)(i - 1)
        prevOutside(x) = res
        res
      }
    }

    def step(i: Int) = {
      val x = i % 2
      i * 2 * X(x)(i) + (i * 2 + 1) * X(x ^ 1)(i)
    }

    var ans = 0L
    var cur = 0L
    rep(N - 1) { i =>
      ans = max(ans, collectOutside(i) + cur)
      cur += step(i)
    }
    ans = max(ans, cur + step(N - 1))

    out.println(ans)
  }

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

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

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

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

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

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

import collection.mutable.HashMap
import scala.collection.{SortedSet, mutable}
import scala.util.Random
  protected val EPS = 1e-11

  private def default = (
    new StreamTokenizer(new InputStreamReader(System.in)),
    new Scanner(System.in),
    new PrintWriter(System.out)
  )

  val (in, ins, out) = {
    try {
      if (sys.props.contains("xDx")) {
        (
          new StreamTokenizer(new FileReader("input.txt")),
          new Scanner(new FileReader("input.txt")),
          new PrintWriter(new FileWriter("output.txt"))
        )
      } else {
        default
      }
    } catch {
      case _: Throwable => default
    }
  }

  private def nextInt = {
    in.nextToken
    in.nval.toInt
  }

  private def nextLong = {
    in.nextToken
    in.nval.toLong
  }

  private def nextDouble = {
    in.nextToken
    in.nval
  }

  private def nextString = {
    in.nextToken
    in.sval
  }

  private def nextChar = {
    in.nextToken
    in.ttype.toChar
  }

  def main(args: Array[String]): Unit = {
    try {
      new Run().run()
    } catch {
      case e: Throwable =>
        throw new RuntimeException(e)
    } finally out.close()
  }

  class Run {
    import collection.breakOut
    def run(): Unit = {
        val n = nextInt
        val field = Array.ofDim[Int](2, n)
        val sum = Array.ofDim[Long](2, n)
        val sum123 = Array.ofDim[Long](2, n)
        val sum321 = Array.ofDim[Long](2, n)
        for(i <- 0 until 2) {
            for(j <- 0 until n) {
                val a = nextInt
                field(i)(j) = a
                sum(i)(j) = a
                if(j > 0) sum(i)(j) += sum(i)(j - 1)
            }
        }

        for(i <- 0 until 2) {
            for(j <- n - 1 to 0 by -1) {
                sum321(i)(j) = (n - 1  - j) * field(i)(j)
                sum123(i)(j) = sum(i)(n - 1) - sum(i)(j)
                if(j < n - 1) {
                    sum321(i)(j) += sum321(i)(j + 1)
                    sum123(i)(j) += sum123(i)(j + 1)
                }
            }
        }

        var res = 0L
        var line = 0
        var time = 0L
        var acc = 0L
        def getSum(line: Int, i: Int) = {
            if(i < 0) 0 else sum(line)(i)
        }
        for(i <- 0 until n) {
            val ol = other(line)
            val tmp = sum123(line)(i) + time * (sum(line)(n - 1) - getSum(line, i - 1)) +
                sum321(ol)(i) + (time + n - i)*(sum(ol)(n - 1) - getSum(ol, i - 1))
            res = math.max(res, tmp + acc)
            acc += field(line)(i)*time + field(ol)(i)*(time + 1)

            time += 2
            line = ol
        }

        println(math.max(res, acc))
    }

    def other(line: Int) = (line + 1) % 2

  }

}

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

import collection.mutable.HashMap
import scala.collection.{SortedSet, mutable}
import scala.util.Random
  protected val EPS = 1e-11

  private def default = (
    new StreamTokenizer(new InputStreamReader(System.in)),
    new Scanner(System.in),
    new PrintWriter(System.out)
  )

  val (in, ins, out) = {
    try {
      if (sys.props.contains("xDx")) {
        (
          new StreamTokenizer(new FileReader("input.txt")),
          new Scanner(new FileReader("input.txt")),
          new PrintWriter(new FileWriter("output.txt"))
        )
      } else {
        default
      }
    } catch {
      case _: Throwable => default
    }
  }

  private def nextInt = {
    in.nextToken
    in.nval.toInt
  }

  private def nextLong = {
    in.nextToken
    in.nval.toLong
  }

  private def nextDouble = {
    in.nextToken
    in.nval
  }

  private def nextString = {
    in.nextToken
    in.sval
  }

  private def nextChar = {
    in.nextToken
    in.ttype.toChar
  }

  def main(args: Array[String]): Unit = {
    try {
      new Run().run()
    } catch {
      case e: Throwable =>
        throw new RuntimeException(e)
    } finally out.close()
  }

  class Run {
    import collection.breakOut
    def run(): Unit = {
        val n = nextInt
        val field = Array.ofDim[Int](2, n)
        val sum = Array.ofDim[Long](2, n)
        val sum123 = Array.ofDim[Long](2, n)
        val sum321 = Array.ofDim[Long](2, n)
        for(i <- 0 until 2) {
            for(j <- 0 until n) {
                val a = nextInt
                field(i)(j) = a
                sum(i)(j) = a
                if(j > 0) sum(i)(j) += sum(i)(j - 1)
            }
        }

        for(i <- 0 until 2) {
            for(j <- n - 1 to 0 by -1) {
                sum321(i)(j) = (n - 1  - j) * field(i)(j)
                sum123(i)(j) = sum(i)(n - 1) - sum(i)(j)
                if(j < n - 1) {
                    sum321(i)(j) += sum321(i)(j + 1)
                    sum123(i)(j) += sum123(i)(j + 1)
                }
            }
        }

        var res = 0L
        var line = 0
        var time = 0
        var acc = 0L
        def getSum(line: Int, i: Int) = {
            if(i < 0) 0 else sum(line)(i)
        }
        for(i <- 0 until n) {
            val ol = other(line)
            val tmp = sum123(line)(i) + time * (sum(line)(n - 1) - getSum(line, i - 1)) +
                sum321(ol)(i) + (time + n - i)*(sum(ol)(n - 1) - getSum(ol, i - 1))
            res = math.max(res, tmp + acc)
            acc += field(line)(i)*time + field(ol)(i)*(time + 1)

            time += 2
            line = ol
        }

        println(math.max(res, acc))
    }

    def other(line: Int) = (line + 1) % 2

  }

}

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

import collection.mutable.HashMap
import scala.collection.{SortedSet, mutable}
import scala.util.Random
  protected val EPS = 1e-11

  private def default = (
    new StreamTokenizer(new InputStreamReader(System.in)),
    new Scanner(System.in),
    new PrintWriter(System.out)
  )

  val (in, ins, out) = {
    try {
      if (sys.props.contains("xDx")) {
        (
          new StreamTokenizer(new FileReader("input.txt")),
          new Scanner(new FileReader("input.txt")),
          new PrintWriter(new FileWriter("output.txt"))
        )
      } else {
        default
      }
    } catch {
      case _: Throwable => default
    }
  }

  private def nextInt = {
    in.nextToken
    in.nval.toInt
  }

  private def nextLong = {
    in.nextToken
    in.nval.toLong
  }

  private def nextDouble = {
    in.nextToken
    in.nval
  }

  private def nextString = {
    in.nextToken
    in.sval
  }

  private def nextChar = {
    in.nextToken
    in.ttype.toChar
  }

  def main(args: Array[String]): Unit = {
    try {
      new Run().run()
    } catch {
      case e: Throwable =>
        throw new RuntimeException(e)
    } finally out.close()
  }

  class Run {
    import collection.breakOut
    def run(): Unit = {
        val n = nextInt
        val field = Array.ofDim[Int](2, n)
        val sum = Array.ofDim[Long](2, n)
        val sum123 = Array.ofDim[Long](2, n)
        val sum321 = Array.ofDim[Long](2, n)
        for(i <- 0 until 2) {
            for(j <- 0 until n) {
                val a = nextInt
                field(i)(j) = a
                sum(i)(j) = a
                if(j > 0) sum(i)(j) += sum(i)(j - 1)
            }
        }

        for(i <- 0 until 2) {
            for(j <- n - 1 to 0 by -1) {
                sum321(i)(j) = (n - 1  - j) * field(i)(j)
                sum123(i)(j) = sum(i)(n - 1) - sum(i)(j)
                if(j < n - 1) {
                    sum321(i)(j) += sum321(i)(j + 1)
                    sum123(i)(j) += sum123(i)(j + 1)
                }
            }
        }

        var res = 0L
        var line = 0
        var time = 0
        var acc = 0L
        def getSum(line: Int, i: Int) = {
            if(i < 0) 0 else sum(line)(i)
        }
        for(i <- 0 until n) {
            val ol = other(line)
            val tmp = sum123(line)(i) + time * (sum(line)(n - 1) - getSum(line, i - 1)) +
                sum321(ol)(i) + (time + n - i)*(sum(ol)(n - 1) - getSum(ol, i - 1))
            res = math.max(res, tmp)
            acc += field(line)(i)*time + field(ol)(i)*(time + 1)

            time += 2
            line = ol
        }

        println(math.max(res, acc))
    }

    def other(line: Int) = (line + 1) % 2

  }

}


import scala.io.StdIn._

object A {

	def main(args: Array[String]) {
		val t = readInt()
		1 to t foreach { _ =>
			val n = readInt()
			for (i <- 1 to n) println(i)
		}
	}
}

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

    for (_ <- 1 to nextInt) {
      val n = nextInt
      out.println((1 to n).mkString(" "))
    }

    out.flush()
  }

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


import java.util

import scala.collection.mutable

object B extends App {

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

  val Array(n, m) = readInts(2)
  val _xs = readInts(n + m)
  val isTaxi = readInts(n + m).map(_ == 1)

  val ts = Array.ofDim[Int](m)
  val xs = Array.ofDim[Int](n)

  var ti, xi = 0
  for {
    i <- _xs.indices
  } {
    if (isTaxi(i)) {
      ts(ti) = _xs(i)
      ti += 1
    } else {
      xs(xi) = _xs(i)
      xi += 1
    }
  }

  val as = Array.fill(m){ 0 }

  val tree = new util.TreeMap[Integer, Integer]()

  for (i <- ts.indices) tree.put(ts(i), i)

  for (x <- xs) {
    val left = tree.floorKey(x)
    val right = tree.ceilingKey(x)
    if (left == null) {
      as(tree.get(right)) += 1
    } else if (right == null) {
      as(tree.get(left)) += 1
    } else if (x - left <= right - x) {
      as(tree.get(left)) += 1
    } else {
      as(tree.get(right)) += 1
    }
  }

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

  println(as.mkString(" "))

  Console.flush
}

import java.util.Scanner

object Task2 {

  def main(args: Array[String]): Unit = {
    val scanner = new Scanner(System.in)
    val numberOfRiders = scanner.nextInt()
    val numberOfTaxiDrivers = scanner.nextInt()
    // Coordinates are always increasing and distinct
    val indexToCoordinates = new Array[Int](numberOfRiders + numberOfTaxiDrivers)
    indexToCoordinates.indices
      .foreach(i =>
        indexToCoordinates.update(i, scanner.nextInt())
      )
    val indexToIsTaxiDriver = new Array[Boolean](numberOfRiders + numberOfTaxiDrivers)
    indexToIsTaxiDriver.indices
      .foreach { i =>
        indexToIsTaxiDriver.update(i, scanner.nextInt() == 1)
      }
    val residentIndexToNearestTaxiDriverIndex = new Array[Int](numberOfRiders + numberOfTaxiDrivers)
    var currentTaxiDriverIndex = -1
    indexToCoordinates.indices
      .foreach { index =>
        if (indexToIsTaxiDriver(index)) {
          currentTaxiDriverIndex = index
        }
        residentIndexToNearestTaxiDriverIndex.update(index, currentTaxiDriverIndex)
      }
    // Now currentTaxiDriverIndex points to an existing taxi driver
    (indexToCoordinates.length - 1 to 0 by -1)
      .foreach { index =>
        if (indexToIsTaxiDriver(index)) {
          currentTaxiDriverIndex = index
        }
        val previousTaxiDriverDistance =
          if (residentIndexToNearestTaxiDriverIndex(index) == -1) {
            1000000001
          } else {
            Math.abs(indexToCoordinates(residentIndexToNearestTaxiDriverIndex(index)) - indexToCoordinates(index))
          }
        val currentTaxiDriverDistance =
          Math.abs(indexToCoordinates(currentTaxiDriverIndex) - indexToCoordinates(index))
        val nearestTaxiDriverIndex =
          if (previousTaxiDriverDistance <= currentTaxiDriverDistance) {
            residentIndexToNearestTaxiDriverIndex(index)
          } else {
            currentTaxiDriverIndex
          }
        residentIndexToNearestTaxiDriverIndex.update(index, nearestTaxiDriverIndex)
      }
    val taxiDriverIndexToNumberOfResidentWhoWouldCall = new Array[Int](numberOfRiders + numberOfTaxiDrivers)
    residentIndexToNearestTaxiDriverIndex.indices
      .foreach { index =>
        if (!indexToIsTaxiDriver(index)) {
          val driverIndex = residentIndexToNearestTaxiDriverIndex(index)
          taxiDriverIndexToNumberOfResidentWhoWouldCall.update(
            driverIndex,
            taxiDriverIndexToNumberOfResidentWhoWouldCall(driverIndex) + 1
          )
        }
      }
    val _taxiDriverIndexToNumberOfResidentWhoWouldCall = new Array[Int](numberOfTaxiDrivers)
    currentTaxiDriverIndex = 0
    taxiDriverIndexToNumberOfResidentWhoWouldCall.indices
      .foreach { index =>
        if (indexToIsTaxiDriver(index)) {
          _taxiDriverIndexToNumberOfResidentWhoWouldCall.update(currentTaxiDriverIndex, taxiDriverIndexToNumberOfResidentWhoWouldCall(index))
          currentTaxiDriverIndex += 1
        }
      }
    println(_taxiDriverIndexToNumberOfResidentWhoWouldCall.mkString(" "))
  }

}

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

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

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

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

  def solve(): Unit = {
    val N, M = ni()
    val loc = na(N + M)
    var ri, di = 0
    val R = Array.ofDim[Int](N)
    val D = Array.ofDim[Int](M)
    rep(N + M) { i =>
      if (ni() == 1) {
        D(di) = loc(i)
        di += 1
      } else {
        R(ri) = loc(i)
        ri += 1
      }
    }

    val ans = Array.ofDim[Int](M)
    rep(M) { i =>
      def d1 = D(i) - D(i - 1)
      def d2 = D(i + 1) - D(i)
      def l = D(i) - (d1 - 1) / 2
      def r = D(i) + d2 / 2 // inclusive
      val lnum = if (i == 0) 0 else lowerBound(R, l)
      val rnum = if (i == M - 1) R.length else lowerBound(R, r + 1)
      val num = rnum - lnum
      ans(i) = num
    }

    out.println(ans.mkString(" "))
  }

  // あえてコピペ
  // 要はcountLt
  def lowerBound(a: Array[Int], x: Int): Int = {
    def step(l: Int, h: Int): Int = {
      if (h - l == 1) h
      else {
        val mid = (l + h) / 2
        if (a(mid) >= x) step(l, mid)
        else step(mid, h)
      }
    }

    step(-1, a.length)
  }

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

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

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

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

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

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

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

import scala.collection.mutable

object A extends App {

  def solve(in: InputReader): Unit = {
    val n, m = in.nextInt
    val arr = Array.fill(n + m)(in.nextInt)
    val b = Array.fill(n + m)(in.nextInt)
    val ans = Array.fill(m)(0)
    var i = 0
    var k = 0
    while (b(i) != 1) {
      i += 1
      ans(k) += 1
    }
    while (i < n + m) {
      var j = i + 1
      while (j < (n + m) && b(j) != 1) j += 1
      val distL = arr(i)
      val distR = if (j == (n + m)) 1e+15 else arr(j)
      (i + 1 until j).foreach { p =>
        if (arr(p) - distL <= distR - arr(p))
          ans(k) += 1
        else
          ans(k + 1) += 1
      }
      i = j
      k += 1
    }

    println(ans.mkString(" "))
  }

  solve(new InputReader(System.in))

  private class InputReader(val in: InputStream) {

    private val reader: BufferedReader = new BufferedReader(new InputStreamReader(in))
    private var tokenizer: StringTokenizer = _

    def nextInt: Int = {
      next.toInt
    }

    def nextDouble: Double = {
      next.toDouble
    }

    def nextLong: Long = {
      next.toLong
    }

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

    def readLine: String = {
      try {
        reader.readLine()
      } catch {
        case e: IOException => throw new RuntimeException(e)
      }
    }
  }

}

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

  override def apply(io: IO): io.type = {
    val n, m = io.read[Int]
    val pos = io.read[Vector, Int](n + m)
    val isDriver = io.read[Vector, Boolean](n + m)

    val left: IndexedSeq[Int] = pos.indices.scanLeft(Int.MaxValue) {
      case (nearestDriver, i) => if (isDriver(i)) pos(i) else nearestDriver
    }.tail

    val right: IndexedSeq[Int] = pos.indices.scanRight(Int.MaxValue) {
      case (i, nearestDriver) => if (isDriver(i)) pos(i) else nearestDriver
    }.init

    val ans: mutable.Map[Int, Int] = pos.indices
      .collect({case i if isDriver(i) => pos(i) -> 0})(collection.breakOut)

    for {
      i <- pos.indices if !isDriver(i)
      taxi = if ((left(i) - pos(i)).abs <= (right(i) - pos(i)).abs) left(i) else right(i)
    } ans(taxi) += 1

    io.writeAll(ans.toSeq.sorted.map(_._2))
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

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

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

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

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

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

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

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

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }

  object Read {
    implicit def collection[C[_], A: Read](implicit b: CanBuild[A, C[A]]) : Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                                   : Read[String]       = new Read(_.next())
    implicit val int                                                      : Read[Int]          = string.map(_.toInt)
    implicit val long                                                     : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                                   : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                                   : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                               : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                                 : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]                        : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]               : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                                  : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}

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

  override def apply(io: IO): io.type = {
    val n, m = io.read[Int]
    val pos = io.read[Vector, Int](n + m)
    val isDriver = io.read[Vector, Boolean](n + m)
    val inf = 1e9.toInt

    val left: IndexedSeq[Int] = pos.indices.scanLeft(inf) {
      case (nearestDriver, i) => if (isDriver(i)) pos(i) else nearestDriver
    }.tail

    val right: IndexedSeq[Int] = pos.indices.scanRight(inf) {
      case (i, nearestDriver) => if (isDriver(i)) pos(i) else nearestDriver
    }.init

    val ans = mutable.Map.empty[Int, Int].withDefaultValue(0)

    pos.indices foreach {i =>
      if(isDriver(i)) {
        ans(pos(i)) = ans(pos(i)) max 0
      } else {
        val taxi = if ((left(i) - pos(i)).abs <= (right(i) - pos(i)).abs) left(i) else right(i)
        ans(taxi) += 1
      }
    }

    io.writeAll(ans.toSeq.sorted.map(_._2))
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

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

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

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

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

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

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

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

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }

  object Read {
    implicit def collection[C[_], A: Read](implicit b: CanBuild[A, C[A]]) : Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                                   : Read[String]       = new Read(_.next())
    implicit val int                                                      : Read[Int]          = string.map(_.toInt)
    implicit val long                                                     : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                                   : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                                   : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                               : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                                 : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]                        : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]               : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                                  : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}


object D extends App {

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

  val Array(n, k) = readInts(2)
  val cs = Array.fill(n){ readLine.toCharArray }

  val ks = Array.fill(n, n)(0)
  ks(0)(0) = k

  for (step <- 0 until 2 * n - 1) {
    var i = if (step < n) 0 else step - n + 1
    var j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      if (ks(i)(j) > 0 && cs(i)(j) != 'a') {
        cs(i)(j) = 'a'
        ks(i)(j) -= 1
      }
      if (j < n - 1) {
        ks(i)(j + 1) = Math.max(ks(i)(j + 1), ks(i)(j))
      }
      if (i < n - 1) {
        ks(i + 1)(j) = Math.max(ks(i + 1)(j), ks(i)(j))
      }
      i += 1
      j -= 1
    }
  }

  val best = Array.fill(2 * n - 1){ 'z' }
  val canReach = Array.fill(n, n){ false }

  canReach(0)(0) = true

  for (step <- 0 until 2 * n - 1) {

    var i = if (step < n) 0 else step - n + 1
    var j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      val c = cs(i)(j)
      if (canReach(i)(j) && c <= best(step)) best(step) = c
      i += 1
      j -= 1
    }

    i = if (step < n) 0 else step - n + 1
    j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      val c = cs(i)(j)
      if (canReach(i)(j) && c <= best(step)) {
        if (j < n - 1) canReach(i)(j + 1) = true
        if (i < n - 1) canReach(i + 1)(j) = true
      }
      i += 1
      j -= 1
    }
  }

  println(best.mkString)
}

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

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

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

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

  def solve(): Unit = {
    val N, K = ni()
    val grid = Array.ofDim[Array[Char]](N)
    rep(N) { i =>
      grid(i) = ns(N)
    }

    case class Coord(h: Int, w: Int)
    val INF = Int.MaxValue / 2

    def dijk_a(sh: Int, sw: Int) = {
      val d = Array.fill[Int](N, N)(INF)
      case class Visit(h: Int, w: Int, cost: Int)
      val queue = new java.util.ArrayDeque[Visit]()
      d(sh)(sw) = 0
      queue.add(Visit(sh, sw, 0))

      while(!queue.isEmpty) {
        val v = queue.poll()
        if (d(v.h)(v.w) == v.cost) {
          rep(2) { x =>
            val nh = v.h + (x ^ 1)
            val nw = v.w + x
            val c = if (grid(v.h)(v.w) == 'a') 0 else 1
            val next = v.cost + c
            if (nh < N && nw < N && K - next >= 0) {
              if (d(nh)(nw) > next) {
                d(nh)(nw) = next
                if (c == 0) {
                  queue.addFirst(Visit(nh, nw, next))
                } else {
                  queue.addLast(Visit(nh, nw, next))
                }
              }
            }
          }
        }
      }

      d
    }

    val d = dijk_a(0, 0)

    /**
      * 一番アルファベット順が小さいものだけを残す
      */
    class MinCharCoord {
      val coord = mutable.Set[Coord]()
      var char = 'z'

      def +=(a: Coord): Unit = {
        val c = grid(a.h)(a.w)
        if (c == char) {
          coord += a
        } else if (c < char) {
          char = c
          coord.clear()
          coord += a
        }
      }

      def clear(): Unit = {
        coord.clear()
        char = 'z'
      }

      def nonEmpty = coord.nonEmpty
      def head = coord.head
    }

    /**
      * aで埋めれる一番距離が大きくなる場所の次に行ける場所を返す
      * @return (aの個数, aの次の候補)
      */
    def findMaxDist(): (Int, MinCharCoord) = {
      // 右下までaで埋めれた場合
      if (d(N - 1)(N - 1) != INF && (d(N - 1)(N - 1) < K || grid(N - 1)(N - 1) == 'a')) {
        (2 * N - 1, new MinCharCoord)
      } else {
        var dist = 0
        val coord = new MinCharCoord
        rep(N) { h =>
          rep(N) { w =>
            if (d(h)(w) != INF) {
              val move = h + w

              if (dist == move) {
                coord += Coord(h, w)
              } else if (dist < move) {
                dist = move
                coord.clear()
                coord += Coord(h, w)
              }
            }
          }
        }
        (dist, coord)
      }
    }

    val (as, ini) = findMaxDist()

    val str = new mutable.StringBuilder()
    rep(as)(_ => str += 'a')

    var q = ini
    var next = new MinCharCoord

    // 深さでBFSする感じ
    while (q.nonEmpty) {
      str += q.char
      q.coord foreach { c =>
        rep(2) { x =>
          val nh = c.h + (x ^ 1)
          val nw = c.w + x
          if (nh < N && nw < N) next += Coord(nh, nw)
        }
      }
      val temp = q
      q = next
      next = temp
      next.clear()
    }

    out.println(str.toString)
  }

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N, K = ni()
    val grid = Array.ofDim[Array[Char]](N)
    rep(N) { i =>
      grid(i) = ns(N)
    }

    case class Coord(h: Int, w: Int)
    val INF = Int.MaxValue / 2

    def dijk_a(sh: Int, sw: Int) = {
      val d = Array.fill[Int](N, N)(INF)
      case class Visit(h: Int, w: Int, cost: Int)
      val queue = new java.util.ArrayDeque[Visit]()
      d(sh)(sw) = 0
      queue.add(Visit(sh, sw, 0))

      while(!queue.isEmpty) {
        val v = queue.poll()
        if (d(v.h)(v.w) == v.cost) {
          rep(2) { x =>
            val nh = v.h + (x ^ 1)
            val nw = v.w + x
            val c = if (grid(v.h)(v.w) == 'a') 0 else 1
            val next = v.cost + c
            if (nh < N && nw < N && K - next >= 0) {
              if (d(nh)(nw) > next) {
                d(nh)(nw) = next
                if (c == 0) {
                  queue.addFirst(Visit(nh, nw, next))
                } else {
                  queue.addLast(Visit(nh, nw, next))
                }
              }
            }
          }
        }
      }

      d
    }

    val d = dijk_a(0, 0)

    /**
      * 一番アルファベット順が小さいものだけを残す
      */
    class MinCharCoord {
      val coord = mutable.Set[Coord]()
      var char = 'z'

      def +=(a: Coord): Unit = {
        val c = grid(a.h)(a.w)
        if (c == char) {
          coord += a
        } else if (c < char) {
          char = c
          coord.clear()
          coord += a
        }
      }

      def clear(): Unit = {
        coord.clear()
        char = 'z'
      }

      def nonEmpty = coord.nonEmpty
      def head = coord.head
    }

    /**
      * aで埋めれる一番距離が大きくなる場所の次に行ける場所を返す
      * @return (aの個数, aの次の候補)
      */
    def findMaxDist(): (Int, MinCharCoord) = {
      // 右下までaで埋めれた場合
      if (d(N - 1)(N - 1) != INF && (d(N - 1)(N - 1) > 0 || grid(N - 1)(N - 1) == 'a')) {
        (2 * N - 1, new MinCharCoord)
      } else {
        var dist = 0
        val coord = new MinCharCoord
        rep(N) { h =>
          rep(N) { w =>
            if (d(h)(w) != INF) {
              val move = h + w

              if (dist == move) {
                coord += Coord(h, w)
              } else if (dist < move) {
                dist = move
                coord.clear()
                coord += Coord(h, w)
              }
            }
          }
        }
        (dist, coord)
      }
    }

    val (as, ini) = findMaxDist()

    val str = new mutable.StringBuilder()
    rep(as)(_ => str += 'a')

    var q = ini
    var next = new MinCharCoord

    // 深さでBFSする感じ
    while (q.nonEmpty) {
      str += q.char
      q.coord foreach { c =>
        rep(2) { x =>
          val nh = c.h + (x ^ 1)
          val nw = c.w + x
          if (nh < N && nw < N) next += Coord(nh, nw)
        }
      }
      val temp = q
      q = next
      next = temp
      next.clear()
    }

    out.println(str.toString)
  }

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N, K = ni()
    val grid = Array.ofDim[Array[Char]](N)
    rep(N) { i =>
      grid(i) = ns(N)
    }

    case class Coord(h: Int, w: Int)
    val INF = Int.MaxValue / 2

    def dijk_a(sh: Int, sw: Int) = {
      val d = Array.fill[Int](N, N)(INF)
      case class Visit(h: Int, w: Int, cost: Int)
      val queue = new java.util.ArrayDeque[Visit]()
      d(sh)(sw) = 0
      queue.add(Visit(sh, sw, 0))

      while(!queue.isEmpty) {
        val v = queue.poll()
        if (d(v.h)(v.w) == v.cost) {
          rep(2) { x =>
            val nh = v.h + (x ^ 1)
            val nw = v.w + x
            val c = if (grid(v.h)(v.w) == 'a') 0 else 1
            val next = v.cost + c
            if (nh < N && nw < N && K - next >= 0) {
              if (d(nh)(nw) > next) {
                d(nh)(nw) = next
                if (c == 0) {
                  queue.addFirst(Visit(nh, nw, next))
                } else {
                  queue.addLast(Visit(nh, nw, next))
                }
              }
            }
          }
        }
      }

      d
    }

    val d = dijk_a(0, 0)
    def findMaxDist(): (Int, ArrayBuffer[Coord]) = {
      var dist = 0
      val coord = ArrayBuffer[Coord]()
      rep(N) { h =>
        rep(N) { w =>
          if (d(h)(w) != INF) {
            if (dist == h + w) {
              coord += Coord(h, w)
            } else if (dist < h + w) {
              dist = h + w
              coord.clear()
              coord += Coord(h, w)
            }
          }
        }
      }
      (dist, coord)
    }

    def filterMinAlpha(as: mutable.Set[Coord]): mutable.Set[Coord] = {
      as.groupBy(c => grid(c.h)(c.w)).toSeq.minBy(_._1)._2
    }

    val (as, ini) = findMaxDist()

    val str = new mutable.StringBuilder()
    var q = mutable.Set(ini: _*)
    rep(as)(_ => str += 'a')

    // 深さでBFSする感じ
    while (q.nonEmpty) {
      val next = mutable.Set[Coord]()
      val filtered = filterMinAlpha(q)
      val a = filtered.head
      str += grid(a.h)(a.w)
      filtered foreach { c =>
        rep(2) { x =>
          val nh = c.h + (x ^ 1)
          val nw = c.w + x
          if (nh < N && nw < N) next += Coord(nh, nw)
        }
      }
      q = next
    }

    out.println(str.toString)
  }

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

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

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

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

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

object D extends App {

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

  val Array(n, k) = readInts(2)
  val cs = Array.fill(n){ readLine.toCharArray }

  val best = Array.fill(2 * n - 1){ 'z' }

  best(0) = cs(0)(0)
  val ks = Array.fill(n, n)(0)
  ks(0)(0) = k

  for (step <- 0 until 2 * n - 2) {
    var i = if (step < n) 0 else step - n + 1
    var j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      if (ks(i)(j) > 0) best(step) = 'a'
      val c = if (ks(i)(j) > 0) 'a' else cs(i)(j)
      if (c <= best(step)) {
        val kk = if (cs(i)(j) == 'a') ks(i)(j) else ks(i)(j) - 1
        if (j < n - 1) {
          if (cs(i)(j + 1) <= best(step + 1)) {
            best(step + 1) = cs(i)(j + 1)
          }
          ks(i)(j + 1) = Math.max(ks(i)(j + 1), kk)
        }
        if (i < n - 1) {
          if (cs(i + 1)(j) <= best(step + 1)) {
            best(step + 1) = cs(i + 1)(j)
          }
          ks(i + 1)(j) = Math.max(ks(i + 1)(j), kk)
        }
      }
      i += 1
      j -= 1
    }
  }

  if (ks(n - 1)(n - 1) > 0) best(2 * n - 2) = 'a'

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

  println(best.mkString)

  Console.flush
}

import scala.collection.mutable

object D extends App {

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

  val Array(n, k0) = readInts(2)
  val cs = Array.fill(n){ readLine.toCharArray }
  var k = k0

  if (cs(0)(0) != 'a' && k > 0) {
    cs(0)(0) = 'a'
    k -= 1
  }

  val best = Array.fill(2 * n - 1){ 'z' }

  best(0) = cs(0)(0)
  val ks = Array.fill(n, n)(0)
  ks(0)(0) = k

  for (step <- 0 until 2 * n - 2) {
    var i = if (step < n) 0 else step - n + 1
    var j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      if (ks(i)(j) > 0) best(step) = 'a'
      val c = if (ks(i)(j) > 0) 'a' else cs(i)(j)
      if (c <= best(step)) {
        val kk = if (cs(i)(j) == 'a') ks(i)(j) else ks(i)(j) - 1
        if (j < n - 1 && cs(i)(j + 1) <= best(step + 1)) {
          best(step + 1) = cs(i)(j + 1)
          ks(i)(j + 1) = Math.max(ks(i)(j + 1), kk)
        }
        if (i < n - 1 && cs(i + 1)(j) <= best(step + 1)) {
          best(step + 1) = cs(i + 1)(j)
          ks(i + 1)(j) = Math.max(ks(i + 1)(j), kk)
        }
      }
      i += 1
      j -= 1
    }
  }

  if (ks(n - 1)(n - 1) > 0) best(2 * n - 2) = 'a'

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

  println(best.mkString)

  Console.flush
}

object D extends App {

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

  val Array(n, k) = readInts(2)
  val cs = Array.fill(n){ readLine.toCharArray }

  val best = Array.fill(2 * n - 1){ 'z' }
  val canReach = Array.fill(n, n){ false }

  best(0) = cs(0)(0)
  val ks = Array.fill(n, n)(0)
  ks(0)(0) = k
  canReach(0)(0) = true

  for (step <- 0 until 2 * n - 2) {
    var i = if (step < n) 0 else step - n + 1
    var j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      if (ks(i)(j) > 0) best(step) = 'a'
      else if (cs(i)(j) < best(step)) best(step) = cs(i)(j)
      i += 1
      j -= 1
    }
    i = if (step < n) 0 else step - n + 1
    j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      val c = if (ks(i)(j) > 0) 'a' else cs(i)(j)
      if (canReach(i)(j) && c <= best(step)) {
        val kk = if (cs(i)(j) == 'a') ks(i)(j) else ks(i)(j) - 1
        if (j < n - 1) {
          if (cs(i)(j + 1) <= best(step + 1)) {
            best(step + 1) = cs(i)(j + 1)
            canReach(i)(j + 1) = true
          }
          ks(i)(j + 1) = Math.max(ks(i)(j + 1), kk)
        }
        if (i < n - 1) {
          if (cs(i + 1)(j) <= best(step + 1)) {
            best(step + 1) = cs(i + 1)(j)
            canReach(i + 1)(j) = true
          }
          ks(i + 1)(j) = Math.max(ks(i + 1)(j), kk)
        }
      }
      i += 1
      j -= 1
    }
  }

  if (ks(n - 1)(n - 1) > 0) best(2 * n - 2) = 'a'

  println(best.mkString)
}

object D extends App {

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

  val Array(n, k) = readInts(2)
  val cs = Array.fill(n){ readLine.toCharArray }

  val best = Array.fill(2 * n - 1){ 'z' }
  val canReach = Array.fill(n, n){ false }

  best(0) = cs(0)(0)
  val ks = Array.fill(n, n)(0)
  ks(0)(0) = k
  canReach(0)(0) = true

  for (step <- 0 until 2 * n - 2) {
    var i = if (step < n) 0 else step - n + 1
    var j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      if (ks(i)(j) > 0) best(step) = 'a'
      i += 1
      j -= 1
    }
    i = if (step < n) 0 else step - n + 1
    j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      val c = if (ks(i)(j) > 0) 'a' else cs(i)(j)
      if (canReach(i)(j) && c <= best(step)) {
        val kk = if (cs(i)(j) == 'a') ks(i)(j) else ks(i)(j) - 1
        if (j < n - 1) {
          if (cs(i)(j + 1) <= best(step + 1)) {
            best(step + 1) = cs(i)(j + 1)
            canReach(i)(j + 1) = true
          }
          ks(i)(j + 1) = Math.max(ks(i)(j + 1), kk)
        }
        if (i < n - 1) {
          if (cs(i + 1)(j) <= best(step + 1)) {
            best(step + 1) = cs(i + 1)(j)
            canReach(i + 1)(j) = true
          }
          ks(i + 1)(j) = Math.max(ks(i + 1)(j), kk)
        }
      }
      i += 1
      j -= 1
    }
  }

  if (ks(n - 1)(n - 1) > 0) best(2 * n - 2) = 'a'

  println(best.mkString)
}

object D extends App {

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

  val Array(n, k) = readInts(2)
  val cs = Array.fill(n){ readLine.toCharArray }

  val best = Array.fill(2 * n - 1){ 'z' }
  val canReach = Array.fill(n, n){ false }

  best(0) = cs(0)(0)
  val ks = Array.fill(n, n)(0)
  ks(0)(0) = k
  canReach(0)(0) = true

  for (step <- 0 until 2 * n - 2) {
    var i = if (step < n) 0 else step - n + 1
    var j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      if (canReach(i)(j)) {
        if (ks(i)(j) > 0) best(step) = 'a'
        else if (cs(i)(j) < best(step)) best(step) = cs(i)(j)
      }
      i += 1
      j -= 1
    }
    i = if (step < n) 0 else step - n + 1
    j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      val c = if (ks(i)(j) > 0) 'a' else cs(i)(j)
      if (canReach(i)(j) && c <= best(step)) {
        val kk = if (cs(i)(j) == 'a') ks(i)(j) else ks(i)(j) - 1
        if (j < n - 1) {
          if (cs(i)(j + 1) <= best(step + 1)) {
            best(step + 1) = cs(i)(j + 1)
            canReach(i)(j + 1) = true
          }
          ks(i)(j + 1) = Math.max(ks(i)(j + 1), kk)
        }
        if (i < n - 1) {
          if (cs(i + 1)(j) <= best(step + 1)) {
            best(step + 1) = cs(i + 1)(j)
            canReach(i + 1)(j) = true
          }
          ks(i + 1)(j) = Math.max(ks(i + 1)(j), kk)
        }
      }
      i += 1
      j -= 1
    }
  }

  if (ks(n - 1)(n - 1) > 0) best(2 * n - 2) = 'a'

  println(best.mkString)
}

object D extends App {

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

  val Array(n, k) = readInts(2)
  val cs = Array.fill(n){ readLine.toCharArray }

  val best = Array.fill(2 * n - 1){ 'z' }

  best(0) = cs(0)(0)
  val ks = Array.fill(n, n)(0)
  ks(0)(0) = k

  for (step <- 0 until 2 * n - 2) {
    var i = if (step < n) 0 else step - n + 1
    var j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      if (ks(i)(j) > 0) best(step) = 'a'
      i += 1
      j -= 1
    }
    i = if (step < n) 0 else step - n + 1
    j = if (step < n) step else n - 1
    while (j >= 0 && i < n) {
      val c = if (ks(i)(j) > 0) 'a' else cs(i)(j)
      if (c <= best(step)) {
        val kk = if (cs(i)(j) == 'a') ks(i)(j) else ks(i)(j) - 1
        if (j < n - 1) {
          if (cs(i)(j + 1) <= best(step + 1)) {
            best(step + 1) = cs(i)(j + 1)
          }
          ks(i)(j + 1) = Math.max(ks(i)(j + 1), kk)
        }
        if (i < n - 1) {
          if (cs(i + 1)(j) <= best(step + 1)) {
            best(step + 1) = cs(i + 1)(j)
          }
          ks(i + 1)(j) = Math.max(ks(i + 1)(j), kk)
        }
      }
      i += 1
      j -= 1
    }
  }

  if (ks(n - 1)(n - 1) > 0) best(2 * n - 2) = 'a'

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

  println(best.mkString)

  Console.flush
}


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

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

    val ad = an.distinct.toList
    val d  = ad.length

    if (d > k) println(-1)
    else {
      val sub                    = ad ::: (1 to k).toList.diff(ad).take(k - d)
      lazy val subs: Stream[Int] = sub.toStream #::: subs

      val ans = subs.take(k * n).toList

      println(ans.length)
      println(ans.mkString(" "))
    }
  }
}

object Main {

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

  import scala.collection.mutable
  import scala.collection.mutable.ListBuffer
  def main(args: Array[String]): Unit = {
    solve(System.in,System.out)
  }
  def solve(inputStream: InputStream, out: PrintStream): Unit = {
    val in = new InputReader(inputStream)
    val numCases = in.nextInt()
    (1 to numCases).foreach{ _ =>
      val (n,k) = (in.nextInt(),in.nextInt())

      val a = (1 to n).map(_ => in.nextInt()).distinct.toArray
      if(a.length > k){
        out.println(-1)
      }else{
        out.println(10000)
        var i = 0
        var e = 0
        val b = a ++ (Array.fill(k-a.length)(1))
        val sb = new mutable.StringBuilder()
        while(e<10000){
          sb.append(b(i))
          if(e < 10000-1)
            sb.append(' ')
          i += 1
          if(i >= k)
            i = 0
          e += 1
        }
        out.println(sb.toString())

      }

    }
  }
  class InputReader(stream: InputStream){
    val reader: BufferedReader = new BufferedReader(new InputStreamReader(stream),32768)
    var tokenizer: StringTokenizer = _
    def next(): String = {
      while(tokenizer == null || !tokenizer.hasMoreTokens){
        try{
          tokenizer = new StringTokenizer(reader.readLine())
        }catch {
          case e: IOException => throw new RuntimeException(e)
        }
      }
      tokenizer.nextToken()
    }
    def nextInt(): Int =  next().toInt
    def nextLong(): Long =  next().toLong
  }
}

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

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

    val ad = an.distinct.toList

    val ans =
      if (ad.length > k) List()
      else {
        val sub = ad ::: (1 to n).toList.diff(ad).take(k - ad.length)

        lazy val subs: Stream[Int] = sub.toStream #::: subs

        subs.take(k * an.length).toList
      }

    if (ans.isEmpty) println(-1)
    else {
      println(ans.length)
      println(ans.mkString(" "))
    }
  }
}

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

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

    lazy val ak: Stream[Int] = (1 to k).toStream #::: ak

    val am =
      if (an.max > k) List()
      else ak.take(k * an.length).toList

    if (am.isEmpty) println(-1)
    else {
      println(am.length)
      println(am.mkString(" "))
    }
  }
}

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

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

    val amin = an.min
    val amax = an.max

    val t = amax - amin + 1

    val ans =
      if (t > k) List()
      else {
        val r = n.min(amax + k - t)
        val l = r - k + 1

        if (l <= 0) List()
        else {
          lazy val ak: Stream[Int] = (l to r).toStream #::: ak

          ak.take(k * an.length).toList
        }
      }

    if (ans.isEmpty) println(-1)
    else {
      println(ans.length)
      println(ans.mkString(" "))
    }
  }
}

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

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

    val amin = an.min
    val amax = an.max

    lazy val ak: Stream[Int] = (amin to (amin + k - 1)).toStream #::: ak

    val ans =
      if (amax - amin + 1 > k) List()
      else ak.take(k * an.length).toList

    if (ans.isEmpty) println(-1)
    else {
      println(ans.length)
      println(ans.mkString(" "))
    }
  }
}

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

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

    val ad = an.distinct.toList
    val d  = ad.length

    if (d > k) println(-1)
    else {
      val sub                    = ad ::: ad.diff(1 to n).take(k - d)
      lazy val subs: Stream[Int] = sub.toStream #::: subs

      val ans = subs.take(k * n).toList

      println(ans.length)
      println(ans.mkString(" "))
    }

  }
}

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

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

    val ad = an.distinct.toList
    val d  = ad.length

    if (d > k) println(-1)
    else {
      val sub                    = ad ::: (1 to k).toList.diff(ad)
      lazy val subs: Stream[Int] = sub.toStream #::: subs

      val ans = subs.take(k * n).toList

      println(ans.length)
      println(ans.mkString(" "))
    }
  }
}


import java.io.InputStreamReader

/**
 * Created by pva701 on 4/7/15.
 */
object HelloWorld {
    val MOD = 1000000000 + 7
    def pow(x:Int, y:Int):Int=if (y == 0) 1 else (1L * x * pow(x, y - 1) % MOD).toInt
    def main(args:Array[String]):Unit= {
        val n = readInt()
        val s = readLine()
        val AL = List('A', 'C', 'G', 'T')
        val cnt = new Array[Int](256)
        s.foreach(c=>cnt(c) += 1)
        val cnChar = cnt(AL.reduceLeft((a, b)=>if (cnt(a) > cnt(b)) a else b))
        val num = AL.foldLeft(0)((a, b)=>if (cnChar == cnt(b)) a + 1 else a)
        println(pow(num, n))
    }
}

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

object C {

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

  val MOD = 1e9 + 7

  def solve: Int = {
    val n = nextInt
    val ch = next.toCharArray
    val cnt = new Array[Int](4)
    for (i <- 0 until n) {
      ch(i) match {
        case 'A' => cnt(0) += 1
        case 'C' => cnt(1) += 1
        case 'G' => cnt(2) += 1
        case 'T' => cnt(3) += 1
        case _ =>
      }
    }
    val max = cnt.toList.max
    var k = 0
    for (i <- 0 until 4) {
      if (cnt(i) == max) {
        k += 1
      }
    }
    out.println(BigInt.int2bigInt(k).pow(n).%(BigInt.int2bigInt(MOD.toInt)))
    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 A extends App {
  val n = readLine.toInt
  val counts = readLine.groupBy(identity).map(_._2.size)
  val maxCount = counts.max
  val withMaxCount = counts.count(_ == maxCount)
  println(BigInt(withMaxCount).pow(n).mod(1000000007))
}

import scala.collection._

object A extends App {

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

  val Array(n) = readInts(1)
  val s = readLine.map {
    case 'A' => 0
    case 'C' => 1
    case 'G' => 2
    case 'T' => 3
  }

  val cntA = s.count(_ == 0)
  val cntC = s.count(_ == 1)
  val cntG = s.count(_ == 2)
  val cntT = s.count(_ == 3)
  val cnt = (if (cntA > 0) 1 else 0) + (if (cntC > 0) 1 else 0) + (if (cntG > 0) 1 else 0) +(if (cntT > 0) 1 else 0)

  println(BigInt(cnt).pow(n).mod(1000000007))
}

object A extends App {

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

  val Array(n) = readInts(1)
  val s = readLine.map {
    case 'A' => 0
    case 'C' => 1
    case 'G' => 2
    case 'T' => 3
  }

  val cntA = s.count(_ == 0)
  val cntC = s.count(_ == 1)
  val cntG = s.count(_ == 2)
  val cntT = s.count(_ == 3)
  val cnt = (if (cntA > 0) 1 else 0) max (if (cntC > 0) 1 else 0) max (if (cntG > 0) 1 else 0) max (if (cntT > 0) 1 else 0)

  println(BigInt(cnt).pow(n).mod(1000000007))
}


import scala.annotation.tailrec
import scala.io.StdIn._
object Main extends App {

  for (_ ← 0 until readInt) {
    val Array(a, b) = readLine.trim.split(' ').map(_.toLong)
    println(
      (1 to digit(b)).map(d ⇒ power(10, d) - 1).count(_ <= b) * a
    )
  }
  def power(base: Long, exp: Int): Long = {
    exp match {
      case 0 ⇒ 1L
      case 1 ⇒ base
      case _ ⇒ power(base, exp & 1) * power(base * base, exp >> 1)
    }
  }
  def digit(value: Long): Int = {
    value match {
      case 0 ⇒ 0
      case _ ⇒ 1 + digit(value / 10)
    }
  }
}

object Main {
    def main(args: Array[String]) = {
      val t = readInt
      (0 until t).foreach((x) => f1)
    }
    def f1(): Unit = {
        val Array(a, b) = readLine.split(" ").map(_.toLong)
        println(f2(a, b))
    }

    def f2(a: Long, b: Long): Long = {
        a * log10(b + 1)
    }
    def log10(x: Long): Long = {
        if (x < 10) 0 else log10(x / 10) + 1
    }
  }

  // ab + a + b = a * 10^(log(b)) + b => b + 1 = 10^log(b) (for b)

import java.io.BufferedInputStream
import java.util.Scanner

object Main extends App {
  val inputReader = new Scanner(new BufferedInputStream(System.in))
  val T = inputReader.nextInt()
  for (_ <- 0 until T) {
    val A = inputReader.nextLong()
    val B = inputReader.nextDouble()
    val log10B = Math.log10(B + 1.001).toLong
    println(A * log10B)
  }
}

object Main {
    def main(args: Array[String]) = {
      val t = readInt
      (0 until t).foreach((x) => f1)
    }
    def f1(): Unit = {
        val Array(a, b) = readLine.split(" ").map(_.toInt)
        println(f2(a, b))
    }

    def f2(a: Long, b: Long): Long = {
        a * log10(b)
    }
    def log10(x: Long): Long = {
        if (x < 10) 0 else log10(x / 10) + 1
    }
  }

  // ab + a = a * 10^(log(b)) => b + 1 = 10^log(b) (for b)

object Main {
    def main(args: Array[String]) = {
      val t = readInt
      (0 until t).foreach((x) => f1)
    }
    def f1(): Unit = {
        val Array(a, b) = readLine.split(" ").map(_.toLong)
        println(f2(a, b))
    }

    def f2(a: Long, b: Long): Long = {
        a * log10(b)
    }
    def log10(x: Long): Long = {
        if (x < 10) 0 else log10(x / 10) + 1
    }
  }

  // ab + a + b = a * 10^(log(b)) + b => b + 1 = 10^log(b) (for b)


import scala.collection.mutable.Stack
object Main {
  def main(args: Array[String]):Unit = {
    val str = readLine()
    val stack = Stack[Char]()
    for (char <- str)
      if (stack.nonEmpty && stack.top == char)
        stack.pop()
      else
        stack.push(char);
    for (char <- stack.reverse)
      print(char)
    println()
  }
}

import scala.collection.mutable.ArrayBuffer

object Flask {

  def plugin(s: String): String = {
    var data:ArrayBuffer[Char] = new ArrayBuffer()
    var pos = 0
    var data_pos = 0
    data += '0'

    while (pos < s.size) {
      if (s(pos) == data(data_pos)) { data_pos -= 1 }
      else {
        data_pos += 1;
        if (data_pos == data.size)
          data += s(pos)
        else
          data(data_pos) = s(pos)
      }
      pos += 1
    }

    List.range(1, data_pos + 1).map(i => data(i)).mkString("")
  }

  def main(args: Array[String]): Unit = {
    println(plugin(readLine))
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val str = in.next()
  var stack = List.empty[Char]
  str.foreach { ch =>
    if (stack.isEmpty || stack.head != ch)
      stack ::= ch
    else if (stack.nonEmpty) {
      stack = stack.tail
    }
  }
  println(stack.reverse.mkString)
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.Random

object P081A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val input = sc.nextLine.toList

  def solve(): String = {

    @tailrec
    def loop(precedences: List[Char], remaining: List[Char]): List[Char] = {
      (precedences, remaining) match {
        case (xs, Nil)          => xs.reverse
        case (Nil, y :: ys)     => loop(y :: Nil, ys)
        case (x :: xs, y :: ys) => if (x == y) loop(xs, ys)
                                   else loop(y :: x :: xs, ys)
      }
    }

    loop(Nil, input).mkString
  }

  out.println(solve)
  out.close
}

object Main {
  def dedup(s: String): String =  {
    val stack = scala.collection.mutable.Stack[Char]()
    s.foreach{ c =>
      if (stack.headOption == Some(c)) stack.pop
      else stack.push(c)
    }
    stack.mkString.reverse
  }

  def main(args: Array[String]) = {
    var s = readLine()
    println(dedup(s))
  }
}

import scala.io.StdIn.readLine

object PlugIn {

  def processString(s: List[Char]) : List[Char] = {
    def process(c : List[Char], acc : List[Char]) : List[Char] = c match {
      case h1 :: h2 :: rest => if (h1 == h2)
                                    process(acc.head :: rest,acc.tail)
                          else process(h2 :: rest, h1 :: acc)
      case h1 :: rest => process(rest, h1:: acc)
      case Nil => acc.reverse.tail
    }
    val r = process(s, List('.'))
    //if (r == s.toList) r else processString(r)
    r

  }

  def main(args: Array[String]) {
    val s = readLine()
    println(processString(s.toList).mkString)
  }

}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.Random

object P081A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val input = sc.nextLine.toList

  def solve(): String = {

    @tailrec
    def innerLoop(acc: List[Char], cs: List[Char]): List[Char] = cs match {
        case x0 :: x1 :: xs if (x0 == x1) => innerLoop(acc, xs.dropWhile(_ == x0))
        case x :: xs                    => innerLoop(x :: acc, xs)
        case Nil => acc.reverse
      }

    @tailrec
    def outerLoop(acc: List[Char]): List[Char] = {
      val shortened = innerLoop(Nil, acc)
      if (acc == shortened) acc
      else outerLoop(shortened)
    }

    outerLoop(input).mkString
  }

  out.println(solve)
  out.close
}


/**
 * @author pvasilyev
 * @since 04 May 2016
 */
object A551 extends App {

  val n: Int = scala.io.StdIn.readInt()
  val a: Array[Int] = scala.io.StdIn.readLine().split(" ").map(_.toInt)

  val withIndex = a.zipWithIndex
  val sorted = withIndex.sortBy(_._1)(Ordering[Int].reverse)

  val result: Array[(Int, Int)] = Array.ofDim(n)
  for (i <- 0 until n) {
    if (i == 0) {
      result(0) = (1, sorted(0)._2)
    } else if (sorted(i)._1 < sorted(i - 1)._1) {
      result(i) = (i+1, sorted(i)._2)
    } else {
      result(i) = (result(i-1)._1, sorted(i)._2)
    }
  }
  println(result.sortBy(_._2).map(_._1).mkString(" "))

}

import java.util.Scanner

object CF551B extends App {
  var in = new Scanner(System.in)
  val n = in.nextInt()
  val a = (1 to n).map(i => in.nextInt())
  val b = a.map(i => a.count(j => j > i) + 1)
  println(b.mkString(" "))
}

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer
import java.io.InputStream
import java.io.PrintWriter
import scala.util.Sorting
object CF551A {

  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
  val reader=new BufferedReader(new InputStreamReader(stream), 32768);

  def next():String = {
      while (!st.hasMoreTokens()){
        val currentLine = reader.readLine
            st=new StringTokenizer(currentLine)
      }
      return st.nextToken
  }

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

  def nextLong():Long = {
      return next.toLong
  }
  def nextDouble:Double = {
          return next.toDouble
      }
  }

  def main(args: Array[String]): Unit = {
    val in = new InputReader(System.in)
    val n = in.nextInt()
    val a = Array.fill(n) { in.nextInt()}
    val b = a.map { x => a.filter { y => y > x }.size + 1}
    println(b.mkString(" "))
  }
}

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer
import java.io.InputStream
import java.io.PrintWriter
import scala.util.Sorting
object CF551A {

  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
  val reader=new BufferedReader(new InputStreamReader(stream), 32768);

  def next():String = {
      while (!st.hasMoreTokens()){
        val currentLine = reader.readLine
            st=new StringTokenizer(currentLine)
      }
      return st.nextToken
  }

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

  def nextLong():Long = {
      return next.toLong
  }
  def nextDouble:Double = {
          return next.toDouble
      }
  }

  def main(args: Array[String]): Unit = {
    val in = new InputReader(System.in)
    val out = new PrintWriter(System.out)
    val n = in.nextInt()
    var a: Array[Integer] = new Array[Integer](n)
    for(i <- 0 until n) {
      a(i) = in.nextInt()
    }
    val origin = a.clone()
    Sorting.quickSort(a)
    a = a.reverse

/*    for(i <- 0 until n) {
      out.print(a(i) + " ")
    }
    out.println

    for(i <- 0 until n) {
      out.print(origin(i) + " ")
    }
    out.println
*/
    for(i <- 0 to n-1) {
      var index = n-1
      for(j <- n-1 to 0 by -1) {
        if ( origin(i)==a(j)) {
          index = j+1
        }
      }
      out.print(index)
      if ( i == n-1)
        out.println()
      else out.print(" ")
    }

    out.flush
    out.close
  }
}

import java.util.StringTokenizer

object _551A 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)
  val b = a.map(i => a.filter(j => j > i).size + 1)
  println(b.mkString(" "))
}

object A551 {
  @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 input = readInts(n).zipWithIndex.sortBy(_._1).reverse
    val ranks = new Array[Int](n)
    ranks(input(0)._2) = 1
    var curr = 1
    for(i <- 1 until n) {
      if(input(i)._1 == input(i-1)._1) {
        ranks(input(i)._2) = curr
      } else {
        ranks(input(i)._2) = i+1
        curr = i+1
      }
    }
    println(ranks.mkString(" "))
  }
}

import scala.language.postfixOps
import java.io._

object CF_551A {
    def bin_search(arr: Array[Int], value: Int, b: Int, e: Int) : Int = {
        val mid : Int = (b+e)/2;
        if( b+1 >= e )
            b
        else if( arr(mid) <= value )
            bin_search(arr, value, mid, e)
        else
            bin_search(arr, value, b, mid)
    }

    def main(args: Array[String]) {
        val stdin = new BufferedReader(new InputStreamReader(System.in));
        val n = stdin.readLine.toInt;
        val list : Array[Int] = stdin.readLine.split(" ").toArray.map(_.toInt);
        val sorted : Array[Int] = list.sortBy( x => x );
        for( i <- 0 until list.length )
            print((list.length - bin_search(sorted, list(i), 0, list.length)).toString + " ");
        println("")
    }
}

object _551A extends CodeForcesApp {
  import scala.collection.mutable

  override type Input = List[Int]
  override type Output = List[Int]

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

  override def solve(input: Input) = {
    input map {i => input.count(_ > i) + 1}
  }

  override def format(result: Output) = result mkString " "
}

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

object A{
  //import scala.collection.mutable.PriorityQueue
  //import scala.collection.mutable.ArrayBuffer
  import util.control.Breaks._
  import java.io.{PrintWriter,InputStream,BufferedReader,InputStreamReader}
  import java.util.{Locale, Scanner,StringTokenizer}
  class InputReader(val stream:InputStream ) {
    var st:StringTokenizer=new StringTokenizer("")
    val reader=new BufferedReader(new InputStreamReader(stream), 32768);

    def next():String = {
        while (!st.hasMoreTokens()){
          val currentLine = reader.readLine
          st=new StringTokenizer(currentLine)
        }
        return st.nextToken
    }

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

    def nextLong():Long = {
    	return next.toLong
    }
    def nextDouble:Double = {
      return next.toDouble
    }
  }

  def main(args: Array[String]){

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

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    //code starts here
    val n=nextInt
    val a=Array.fill(n)(0)
    val r=Array.fill(n)(0)
    for(i<-0 until n){a(i)=nextInt}
    for(i<-0 until n){
      for(j<-0 until n){
        if(a(i)<a(j)){
          r(i)+=1
        }
      }
    }
    for(i<-0 until n){
      out.print(r(i)+1)
      out.print(" ")
    }
    out.println()
    //code ends here
    out.flush
    out.close
  }
}