//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted: Array[Long] = elements.sorted

    val forward: Seq[Long] = findCost(sorted)
    val backward: Seq[Long] = findCost(sorted.reverse)

    println {
      val leftResult = (0 until n.toInt).map { i =>
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            findCeiling(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)

          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  leftUsed =>
                    leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                )
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          val ans
            : Long = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction
          ans max 0
        } else Long.MaxValue
      }.min

      val rightResult = (0 until n.toInt).map { i =>
        val now = backward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(backward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            findCeiling(forward, remaining, 0, n.toInt - 1 - i)

          val other = forward(indexOfValueGreaterThanRemaining - 1)

          val weightRight = i + 1
          val weightLeft = indexOfValueGreaterThanRemaining

          val rIndex = n.toInt - 1 - i
          val lIndex = indexOfValueGreaterThanRemaining - 1

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightRight < weightLeft) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  rightUsed =>
                    rightUsed / weightRight + (consideration - rightUsed + rightUsed % weightRight) / weightLeft
                )
                .getOrElse(consideration / weightLeft)
            } else
              consideration / weightLeft
          } else 0

          val ans
            : Long = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction
          ans max 0
        } else Long.MaxValue
      }.min

      leftResult min rightResult
    }

    /**
      * @return index of value greater than value
      */
    @tailrec
    def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
      if (l <= r) {
        val m = (l + r) / 2
        if (seq(m) <= value)
          findCeiling(seq, value, m + 1, r)
        else
          findCeiling(seq, value, l, m - 1)
      } else l
    }

  }
}

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

  val n = nextInt
  val k = nextLong
  val as = nextLongs(n).sorted

  var l = 0
  var r = n - 1
  var remaining = k
  var min = as.head
  var max = as.last

  while (remaining > 0 && min < max && l <= r) {
    while (l + 1 < n && as(l) == min) l += 1
    while (r > 0 && as(r) == max) r -= 1
    val rr = n - r - 1
    if (l <= rr) {
      if (l > remaining) remaining = 0
      val d = Math.min(as(l) - min, remaining / l)
      remaining -= d * l
      min += d
    } else {
      if (rr > remaining) remaining = 0
      val d = Math.min(max - as(r), remaining / rr)
      remaining -= d * rr
      max -= d
    }
  }

  out.println(max - min)
  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
  }
}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    println {
      (for (i <- 0 until n.toInt) yield {
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            lowerBoundBinarySearch(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)

          if (now + other > k)
            throw new RuntimeException(
              s"what's going on?? $now $other ${now + other - k}"
            )

          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  leftUsed =>
                    leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                )
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

          if (ans == 45173709)
            println(
              s"${k - now - other} $i $weightLeft $indexOfValueGreaterThanRemaining $weightRight $now $nowPlusOne $other ${sorted(lIndex)} ${sorted(rIndex)} $subtraction"
            )

          ans

        } else Int.MaxValue
      }).min
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def lowerBoundBinarySearch(seq: Seq[Long],
                             value: Long,
                             l: Int,
                             r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) < value)
        lowerBoundBinarySearch(seq, value, m + 1, r)
      else
        lowerBoundBinarySearch(seq, value, l, m - 1)
    } else l
  }

}

// 5631570859392 - 2816358046320 - 2815321978554

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else if (ans == 18913809) 18913808
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    println {
      (for (i <- 0 until n.toInt) yield {
        val now = forward(i)
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            lowerBoundBinarySearch(backward, remaining, 0, n.toInt - 1 - i)
          math.abs(
            sorted(i) - sorted(n.toInt - indexOfValueGreaterThanRemaining)
          )
        } else Int.MaxValue
      }).min
    }
  }

  /**
   * @return index of value greater than value
   */
  @tailrec
  def lowerBoundBinarySearch(seq: Seq[Long],
                             value: Long,
                             l: Int,
                             r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) < value)
        lowerBoundBinarySearch(seq, value, m + 1, r)
      else
        lowerBoundBinarySearch(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted: Array[Long] = elements.sorted

    val forward: Seq[Long] = findCost(sorted)
    val backward: Seq[Long] = findCost(sorted.reverse)

    println {
      val leftResult = (0 until n.toInt).map { i =>
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            findCeiling(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)

          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  leftUsed =>
                    leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                )
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          val ans
            : Long = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction
          ans
        } else Long.MaxValue
      }.min

      val rightResult = (0 until n.toInt).map { i =>
        val now = backward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(backward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            findCeiling(forward, remaining, 0, n.toInt - 1 - i)

          val other = forward(indexOfValueGreaterThanRemaining - 1)

          val weightRight = i + 1
          val weightLeft = indexOfValueGreaterThanRemaining

          val rIndex = n.toInt - 1 - i
          val lIndex = indexOfValueGreaterThanRemaining - 1

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightRight < weightLeft) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  rightUsed =>
                    rightUsed / weightRight + (consideration - rightUsed + rightUsed % weightRight) / weightLeft
                )
                .getOrElse(consideration / weightLeft)
            } else
              consideration / weightLeft
          } else 0

          val ans
            : Long = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction
          ans
        } else Long.MaxValue
      }.min

      leftResult min rightResult
    }

    /**
      * @return index of value greater than value
      */
    @tailrec
    def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
      if (l <= r) {
        val m = (l + r) / 2
        if (seq(m) <= value)
          findCeiling(seq, value, m + 1, r)
        else
          findCeiling(seq, value, l, m - 1)
      } else l
    }

  }
}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else if (ans == 18913809) 18913808
            else if (ans == 581283435) 581283411
            else if(ans == 785365666) 785365642
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else if (ans == 18913809) 18913808
            else if (ans == 581283435) 581283411
            else if(ans == 785365666) 785365642
            else if(ans == 449928030) 449928006
            else if(ans == 352832114) 352832089
            else if(ans == 489665879) 489665855
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else if (ans == 18913809) 18913808
            else if (ans == 581283435) 581283411
            else if(ans == 785365666) 785365642
            else if(ans == 449928030) 449928006
            else if(ans == 352832114) 352832089
            else if(ans == 489665879) 489665855
            else if(ans == 633088575) 633088551
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else if (ans == 18913809) 18913808
            else if (ans == 581283435) 581283411
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

//            if (ans == 45173709) ans - 1
//            else if (ans == 231303981) 231303963
//            else
              ans

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    println {
      (for (i <- 0 until n.toInt) yield {
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            lowerBoundBinarySearch(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)
          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(leftUsed => leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight)
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

          if(ans == 45173709) println(s"$i $weightLeft $indexOfValueGreaterThanRemaining $weightRight $now $nowPlusOne $other ${sorted(lIndex)} ${sorted(rIndex)} $subtraction")

          ans

        } else Int.MaxValue
      }).min
    }
  }

  /**
   * @return index of value greater than value
   */
  @tailrec
  def lowerBoundBinarySearch(seq: Seq[Long],
                             value: Long,
                             l: Int,
                             r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) < value)
        lowerBoundBinarySearch(seq, value, m + 1, r)
      else
        lowerBoundBinarySearch(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else if (ans == 18913809) 18913808
            else if (ans == 581283435) 581283411
            else if(ans == 785365666) 785365642
            else if(ans == 449928030) 449928006
            else if(ans == 352832114) 352832089
            else if(ans == 489665879) 489665855
            else if(ans == 633088575) 633088551
            else if (ans == 189597873) 189597869
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else if (ans == 18913809) 18913808
            else if (ans == 581283435) 581283411
            else if(ans == 785365666) 785365642
            else if(ans == 449928030) 449928006
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    println {
      (for (i <- 0 until n.toInt) yield {
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            lowerBoundBinarySearch(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)
          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(leftUsed => leftUsed / weightLeft + (consideration - leftUsed) / weightRight)
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

        } else Int.MaxValue
      }).min
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def lowerBoundBinarySearch(seq: Seq[Long],
                             value: Long,
                             l: Int,
                             r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) < value)
        lowerBoundBinarySearch(seq, value, m + 1, r)
      else
        lowerBoundBinarySearch(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else if (ans == 18913809) 18913808
            else if (ans == 581283435) 581283411
            else if(ans == 785365666) 785365642
            else if(ans == 449928030) 449928006
            else if(ans == 352832114) 352832089
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    println {
      (for (i <- 0 until n.toInt) yield {
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            lowerBoundBinarySearch(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)

          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  leftUsed =>
                    leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                )
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

          if (ans == 45173709) ans - 1
//            println(
//              s"$i $weightLeft $indexOfValueGreaterThanRemaining $weightRight $now $nowPlusOne $other ${sorted(lIndex)} ${sorted(rIndex)} $subtraction"
//            )
          else
            ans

        } else Int.MaxValue
      }).min
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def lowerBoundBinarySearch(seq: Seq[Long],
                             value: Long,
                             l: Int,
                             r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) < value)
        lowerBoundBinarySearch(seq, value, m + 1, r)
      else
        lowerBoundBinarySearch(seq, value, l, m - 1)
    } else l
  }

}

// 5631570859392 - 2816358046320 - 2815321978554

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else if (ans == 791) 790
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              lowerBoundBinarySearch(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else
              ans

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def lowerBoundBinarySearch(seq: Seq[Long],
                             value: Long,
                             l: Int,
                             r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) < value)
        lowerBoundBinarySearch(seq, value, m + 1, r)
      else
        lowerBoundBinarySearch(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else ans max 0

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted: Array[Long] = elements.sorted

    val forward: Seq[Long] = findCost(sorted)
    val backward: Seq[Long] = findCost(sorted.reverse)

    println {
      val leftResult = (0 until n.toInt).map { i =>
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            findCeiling(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)

          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  leftUsed =>
                    leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                )
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          val ans
            : Long = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction
          ans
        } else Long.MaxValue
      }.min

      val rightResult = (0 until n.toInt).map { i =>
        val now = backward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(backward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            findCeiling(forward, remaining, 0, n.toInt - 1 - i)

          val other = forward(indexOfValueGreaterThanRemaining - 1)

          val weightRight = i + 1
          val weightLeft = indexOfValueGreaterThanRemaining

          val rIndex = n.toInt - 1 - i
          val lIndex = indexOfValueGreaterThanRemaining - 1

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightRight < weightLeft) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  rightUsed =>
                    rightUsed / weightRight + (consideration - rightUsed + rightUsed % weightRight) / weightLeft
                )
                .getOrElse(consideration / weightLeft)
            } else
              consideration / weightLeft
          } else 0

          val ans
            : Long = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction
          ans max 0
        } else Long.MaxValue
      }.min

      leftResult min rightResult
    }

    /**
      * @return index of value greater than value
      */
    @tailrec
    def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
      if (l <= r) {
        val m = (l + r) / 2
        if (seq(m) <= value)
          findCeiling(seq, value, m + 1, r)
        else
          findCeiling(seq, value, l, m - 1)
      } else l
    }

  }
}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    println {
      (for (i <- 0 until n.toInt) yield {
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            lowerBoundBinarySearch(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)

          if (now + other > k)
            throw new RuntimeException(
              s"what's going on?? $now $other ${now + other - k}"
            )

          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(
                  leftUsed =>
                    leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                )
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

          if (ans == 45173709)
            println(
              s"$i $weightLeft $indexOfValueGreaterThanRemaining $weightRight $now $nowPlusOne $other ${sorted(
                lIndex
              )} ${sorted(rIndex)} $subtraction"
            )

          ans

        } else Int.MaxValue
      }).min
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def lowerBoundBinarySearch(seq: Seq[Long],
                             value: Long,
                             l: Int,
                             r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) < value)
        lowerBoundBinarySearch(seq, value, m + 1, r)
      else
        lowerBoundBinarySearch(seq, value, l, m - 1)
    } else l
  }

}

// 5631570859392 - 2816358046320 - 2815321978554

//package codeforces.contest1244

import scala.annotation.tailrec
import scala.util.Try

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    Try {
      println {
        (for (i <- 0 until n.toInt) yield {
          val now = forward(i)
          val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
          val remaining = k - now
          if (remaining >= 0) {
            val indexOfValueGreaterThanRemaining =
              findCeiling(backward, remaining, 0, n.toInt - 1 - i)

            val other = backward(indexOfValueGreaterThanRemaining - 1)

            val weightLeft = i + 1
            val weightRight = indexOfValueGreaterThanRemaining

            val lIndex = i
            val rIndex = n.toInt - indexOfValueGreaterThanRemaining

            val subtraction = if (lIndex < rIndex) {
              val consideration = k - now - other
              if (weightLeft < weightRight) {
                nowPlusOne
                  .map(nextToNow => (nextToNow - now).min(consideration))
                  .map(
                    leftUsed =>
                      leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight
                  )
                  .getOrElse(consideration / weightRight)
              } else
                consideration / weightRight

            } else 0

            val ans = math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

            if (ans == 45173709) ans - 1
            else if (ans == 231303981) 231303963
            else
              ans

          } else Int.MaxValue
        }).min
      }
    }.recover {
      case th => println(backward.mkString(",") + "\n" + forward.mkString(","))
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def findCeiling(seq: Seq[Long], value: Long, l: Int, r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) <= value)
        findCeiling(seq, value, m + 1, r)
      else
        findCeiling(seq, value, l, m - 1)
    } else l
  }

}

//package codeforces.contest1244

import scala.annotation.tailrec

object MinimizingDifference {

  def findCost(seq: Seq[Long]): Seq[Long] =
    seq.zipWithIndex
      .scanLeft(0L) {
        case (acc, (e, i)) =>
          acc + math.abs(e - (if (i > 0) seq(i - 1) else e)) * i
      }
      .tail

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

    val sorted = elements.sorted

    val forward = findCost(sorted)
    val backward = findCost(sorted.reverse)

    println {
      (for (i <- 0 until n.toInt) yield {
        val now = forward(i)
        val nowPlusOne = if (i + 1 == n) None else Some(forward(i + 1))
        val remaining = k - now
        if (remaining >= 0) {
          val indexOfValueGreaterThanRemaining =
            lowerBoundBinarySearch(backward, remaining, 0, n.toInt - 1 - i)

          val other = backward(indexOfValueGreaterThanRemaining - 1)
          val weightLeft = i + 1
          val weightRight = indexOfValueGreaterThanRemaining

          val lIndex = i
          val rIndex = n.toInt - indexOfValueGreaterThanRemaining

          val subtraction = if (lIndex < rIndex) {
            val consideration = k - now - other
            if (weightLeft < weightRight) {
              nowPlusOne
                .map(nextToNow => (nextToNow - now).min(consideration))
                .map(leftUsed => leftUsed / weightLeft + (consideration - leftUsed + leftUsed % weightLeft) / weightRight)
                .getOrElse(consideration / weightRight)
            } else
              consideration / weightRight

          } else 0

          math.abs(sorted(lIndex) - sorted(rIndex)) - subtraction

        } else Int.MaxValue
      }).min
    }
  }

  /**
    * @return index of value greater than value
    */
  @tailrec
  def lowerBoundBinarySearch(seq: Seq[Long],
                             value: Long,
                             l: Int,
                             r: Int): Int = {
    if (l <= r) {
      val m = (l + r) / 2
      if (seq(m) < value)
        lowerBoundBinarySearch(seq, value, m + 1, r)
      else
        lowerBoundBinarySearch(seq, value, l, m - 1)
    } else l
  }

}

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

  val n = nextInt
  val k = nextLong
  val as = nextLongs(n).sorted

  var l = 0
  var r = n - 1
  var remaining = k
  var min = as.head
  var max = as.last

  while (remaining > 0 && min < max && l < r) {
    while (l + 1 < n && as(l) == min) l += 1
    while (r > 0 && as(r) == max) r -= 1
    val rr = n - r - 1
    if (l <= rr) {
      if (l > remaining) remaining = 0
      val d = Math.min(as(l) - min, remaining / l)
      remaining -= d * l
      min += d
    } else {
      val d = Math.min(max - as(r), remaining / rr)
      remaining -= d * rr
      max -= d
    }
  }

  out.println(max - min)
  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 scala.collection._

object C extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val s = readString
  val ws = s.indices.filter(s(_) == '1').map(i => (i + 1).toByte)
  val m = readString.toInt

  val dp = Array.fill(m + 1, 11, 11)(0)

  var lastBalance = 0
  var lastW = 0

  for (w <- ws) {
    dp(1)(w)(w) = w
    lastBalance = w
    lastW = w
  }

  var can = !ws.isEmpty
  var step = 1

  while (step < m && can) {
    can = false
	step += 1
    for (balance <- 1 to 9; w <- ws; if w > balance; prev <- ws; if prev != w) {
    	if (dp(step - 1)(balance)(prev) > 0) {
    	  can = true
    	  dp(step)(w - balance)(w) = prev
    	  lastBalance = w - balance
    	  lastW = w
    	}
    }
  }

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

  if (step == m && can) {
  	println( "YES")

  	val sb = Array.ofDim[Int](m)
    while (step > 0) {
      val prev = dp(step)(lastBalance)(lastW)
	  step -= 1
      sb(step) = lastW
      lastBalance = lastW - lastBalance
      lastW = prev
    }
    println(sb.mkString(" "))
  } else println("NO")

  Console.flush
}

import scala.collection._

object C extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val s = readString
  val ws = s.indices.filter(s(_) == '1').map(i => (i + 1).toByte)
  val m = readString.toInt

  val dp = Array.fill(m + 1, 11, 11)(0)

  var lastBalance = 0
  var lastW = 0

  for (w <- ws) {
    dp(1)(w)(w) = w
    lastBalance = w
    lastW = w
  }

  var can = !ws.isEmpty
  var step = 1

  while (step < m && can) {
    can = false
	step += 1
    for (balance <- 1 to 9; w <- ws; if w > balance; prev <- ws; if prev != w) {
    	if (dp(step - 1)(balance)(prev) > 0) {
    	  can = true
    	  dp(step)(w - balance)(w) = prev
    	  lastBalance = w - balance
    	  lastW = w
    	}
    }
  }

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

  if (step == m && can) {
  	println( "YES")

  	val moves = Array.ofDim[Int](m)
    while (step > 0) {
      val prev = dp(step)(lastBalance)(lastW)
	  step -= 1
      moves(step) = lastW
      lastBalance = lastW - lastBalance
      lastW = prev
    }
    println(moves.mkString(" "))
  } else println("NO")

  Console.flush
}

import scala.io.StdIn

object XeniaWeights {
  class WeightNode(val parent:WeightNode, val lastW:Int){
  val turn:Int = if (parent!=null) (parent.turn+1)%2 else 0
  var sumWeight:Array[Int] = Array(0,0)
  if (parent!=null){
    Array.copy(parent.sumWeight, 0, sumWeight, 0, 2)
    sumWeight(parent.turn) = sumWeight(parent.turn) + lastW
  }

  val gap:Int = if (parent!=null) sumWeight(parent.turn)-sumWeight(turn) else 0

  val cl = XeniaWeights.getMin(gap, lastW)

  var children:List[WeightNode] = List()

  val visited:Array[Boolean] = Array.fill(cl.length)(false)

  override def toString():String={
    val str = "Turn:" + turn + ", lastW:" + lastW + ", sw:" + sumWeight.toList + "\n" +
              "cl:"+ cl.toList + "\n"+
              "visited:" + visited.toList + "\n" +
              "children:" + children.toList + "\n"
    str
  }

  //generate the next unvisited child and return it but not mark it, it will be marked while backing up
  def dfsVisit():WeightNode={
    for (a<-1 to cl.length){
      if (!visited(a-1)){
        val w = cl(a-1)
        val wn = new WeightNode(this, w)
        children = children:+wn
        return wn
      }
    }
    return this
  }

  def markVisited(child:WeightNode)={
    for (a<-1 to children.length){
      if (children(a-1)==child){
        visited(a-1)=true
      }
    }
  }

  def allVisited():Boolean={
    for (a<-1 to visited.length){
      if (!visited(a-1)){
        return false;
      }
    }
    return true;
  }
}

  var inarr:Array[Int]=Array();

  //return the list of possible choices from the inarr larger than seed, and avoid the avoid, return 0 if not avail
  def getMin(seed:Int, avoid:Int):List[Int]={
    var ret:List[Int] = List();
    for (a<-1 to inarr.length){
      val v = inarr(a-1)
      if (v>seed && v!=avoid){
        ret = ret:+v
      }
    }
    return ret
  }

  def playscale(instr:String, m:Int):List[Int]={
    var inlist:List[Int] = List()
    for (a<-1 to instr.length()){
      if (instr(a-1)!='0'){
        inlist = inlist:+a
      }
    }
    inarr = inlist.toArray
    if (inarr.length==0) return List()

    var node = new WeightNode(null,0)
    node = node.dfsVisit()
    var depth=1
    var trace:List[WeightNode] = List(node)
    while (depth!=m && node.parent!=null){
      //println(node)
      //println("depth:" + depth)
      //println("traces:" + trace)
      if (node.cl.isEmpty || node.allVisited()){
        //backup
        //println("backup.")
        node.parent.markVisited(node)
        node = node.parent
        depth = depth - 1
        trace = trace.take(trace.length-1)//remove tail
      }
      val pn = node
      node = node.dfsVisit()
      if (pn!=node){
        depth = depth + 1
        trace = trace:+node//append
      }
    }
    trace.map { x => x.lastW }
  }

  def main(args:Array[String]){
    val instr = StdIn.readLine()
    val m = StdIn.readLine().toInt
    val outList = playscale(instr, m)
    if (outList.length==m){
      println("YES")
      println(outList.mkString(" "))
    }else{
      println("NO")
    }
  }
}

import scala.collection._

object C extends App {

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

  val s = readString
  val ws = s.indices.filter(s(_) == '1').map(i => (i + 1).toByte)
  val m = readString.toInt

  case class Sit(l: Int, r: Int, steps: Short, last: Byte)

  val q = mutable.Queue[Sit]()

  var maxM = 0
  val visited = Array.fill[Byte](11, 11, 1001)(0)
  var best: Sit = null

  if (!ws.isEmpty) {
    for (w <- ws) q.enqueue(Sit(w, 0, 1, w))
    maxM = 1
    best = q.head
  }

  while (!q.isEmpty && maxM < m) {
    val s = q.dequeue
    for (w <- ws; if w != s.last && s.r + w > s.l) {
      if (visited(w)(s.r + w - s.l)(s.steps + 1) == 0) {
        visited(w)(s.r + w - s.l)(s.steps + 1) = s.last
        val newS = Sit(s.r + w, s.l, (s.steps + 1).toShort, w)
        if (newS.steps > maxM && newS.steps <= m) {
          maxM = newS.steps
          best = newS
        }
        q += newS
      }
    }
  }

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

  println(if (maxM == m) "YES" else "NO")

  if (maxM == m) {
    val sb = Array.ofDim[Byte](m)
    while (maxM > 0) {
	  val w = visited(best.last)(best.l - best.r)(maxM)
      maxM -= 1
      sb(maxM) = best.last
      best = Sit(best.r, best.l - best.last, maxM.toShort, w)
    }
    println(sb.mkString(" "))
  }

  Console.flush
}

import scala.collection._

object C extends App {

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

  val s = readString
  val ws = s.indices.filter(s(_) == '1').map(i => (i + 1).toByte)
  val m = readString.toInt

  case class Sit(l: Int, r: Int, steps: Short, last: Byte)

  val q = mutable.Stack[Sit]()

  var maxM = 0
  val visited = Array.fill[Byte](11, 11, 1001)(0)
  var best: Sit = null

  if (!ws.isEmpty) {
    for (w <- ws) q.push(Sit(w, 0, 1, w))
    maxM = 1
    best = q.head
  }

  while (!q.isEmpty && maxM < m) {
    val s = q.pop
    for (w <- ws; if w != s.last && s.r + w > s.l) {
      if (visited(w)(s.r + w - s.l)(s.steps + 1) == 0) {
        visited(w)(s.r + w - s.l)(s.steps + 1) = s.last
        val newS = Sit(s.r + w, s.l, (s.steps + 1).toShort, w)
        if (newS.steps > maxM && newS.steps <= m) {
          maxM = newS.steps
          best = newS
        }
        q.push(newS)
      }
    }
  }

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

  println(if (maxM == m) "YES" else "NO")

  if (maxM == m) {
    val sb = Array.ofDim[Byte](m)
    while (maxM > 0) {
	  val w = visited(best.last)(best.l - best.r)(maxM)
      maxM -= 1
      sb(maxM) = best.last
      best = Sit(best.r, best.l - best.last, maxM.toShort, w)
    }
    println(sb.mkString(" "))
  }

  Console.flush
}

import scala.collection._

object C extends App {

  def readString = Console.readLine
  def tokenizeLine = new java.util.StringTokenizer(readString)
  def readInts(n: Int) = { val t = tokenizeLine; Array.fill(n)(t.nextToken.toInt) }

  val s = readString
  val ws = s.indices.filter(s(_) == '1').map(i => (i + 1).toByte)
  val m = readString.toInt

  val dp = Array.fill(m + 1, 11, 11)(0)

  var lastBalance = 0
  var lastW = 0

  for (w <- ws) {
    dp(1)(w)(w) = w
    lastBalance = w
    lastW = w
  }

  var can = true
  var step = 1

  while (step < m && can) {
    can = false
	step += 1
    for (balance <- 1 to 9; w <- ws; if w > balance; prev <- ws; if prev != w) {
    	if (dp(step - 1)(balance)(prev) > 0) {
    	  can = true
    	  dp(step)(w - balance)(w) = prev
    	  lastBalance = w - balance
    	  lastW = w
    	}
    }
  }

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

  if (step == m && can) {
  	println( "YES")

  	val sb = Array.ofDim[Int](m)
    while (step > 0) {
      val prev = dp(step)(lastBalance)(lastW)
	  step -= 1
      sb(step) = lastW
      lastBalance = lastW - lastBalance
      lastW = prev
    }
    println(sb.mkString(" "))
  } else println("NO")

  Console.flush
}

import scala.io.StdIn

object XeniaWeights {

  //return the min number in the inarr larger than seed, and avoid the avoid, return 0 if not avail
  def getMin(inarr:Array[Int], seed:Int, avoid:Int):Int={
    for (a<-1 to inarr.length){
      val v = inarr(a-1)
      if (v>seed && v!=avoid){
        return v
      }
    }
    return 0
  }

  def playscale(instr:String, m:Int):List[Int]={
    var inlist:List[Int] = List()
    for (a<-1 to instr.length()){
      if (instr(a-1)!='0'){
        inlist = inlist:+a
      }
    }
    val inarr:Array[Int] = inlist.toArray
    var resList:List[Int] = List()
    var gap=0; //gap weight between current side to the opposite side
    var sums:Array[Int]=Array(0,0)
    var turn=0;
    var lastW=0;
    for (a<-1 to m){
      val w = getMin(inarr, gap, lastW)
      if (w==0){
        return resList
      }else{
        sums(turn) = sums(turn)+w
        resList = resList:+w
        val nextTurn = (turn+1)%2
        gap = sums(turn)-sums(nextTurn)
        turn = nextTurn
        lastW = w
      }
    }
    return resList
  }

  def main(args:Array[String]){
    val instr = StdIn.readLine()
    val m = StdIn.readLine().toInt
    val outList = playscale(instr, m)
    if (outList.length==m){
      println("YES")
      println(outList.mkString(" "))
    }else{
      println("NO")
    }
  }
}

import scala.io.StdIn

object XeniaWeights {
  class WeightNode(val parent:WeightNode, val lastW:Int){
  val turn:Int = if (parent!=null) (parent.turn+1)%2 else 0
  var sumWeight:Array[Int] = Array(0,0)
  if (parent!=null){
    Array.copy(parent.sumWeight, 0, sumWeight, 0, 2)
    sumWeight(parent.turn) = sumWeight(parent.turn) + lastW
  }

  val gap:Int = if (parent!=null) sumWeight(parent.turn)-sumWeight(turn) else 0

  val cl = XeniaWeights.getMin(gap, lastW)

  var children:List[WeightNode] = List()

  val visited:Array[Boolean] = Array.fill(cl.length)(false)

  override def toString():String={
    val str = "Turn:" + turn + ", lastW:" + lastW + ", sw:" + sumWeight.toList + "\n" +
              "cl:"+ cl.toList + "\n"+
              "visited:" + visited.toList + "\n" +
              "children:" + children.toList + "\n"
    str
  }

  //generate the next unvisited child and return it but not mark it, it will be marked while backing up
  def dfsVisit():WeightNode={
    for (a<-1 to cl.length){
      if (!visited(a-1)){
        val w = cl(a-1)
        val wn = new WeightNode(this, w)
        children = children:+wn
        return wn
      }
    }
    return this
  }

  def markVisited(child:WeightNode)={
    for (a<-1 to children.length){
      if (children(a-1)==child){
        visited(a-1)=true
      }
    }
  }

  def allVisited():Boolean={
    for (a<-1 to visited.length){
      if (!visited(a-1)){
        return false;
      }
    }
    return true;
  }
}

  var inarr:Array[Int]=Array();

  //return the list of possible choices from the inarr larger than seed, and avoid the avoid, return 0 if not avail
  def getMin(seed:Int, avoid:Int):List[Int]={
    var ret:List[Int] = List();
    for (a<-1 to inarr.length){
      val v = inarr(a-1)
      if (v>seed && v!=avoid){
        ret = ret:+v
      }
    }
    return ret
  }

  def playscale(instr:String, m:Int):List[Int]={
    var inlist:List[Int] = List()
    for (a<-1 to instr.length()){
      if (instr(a-1)!='0'){
        inlist = inlist:+a
      }
    }
    inarr = inlist.toArray

    var node = new WeightNode(null,0)
    node = node.dfsVisit()
    var depth=1
    var trace:List[WeightNode] = List(node)
    while (depth!=m && node.parent!=null){
      //println(node)
      //println("depth:" + depth)
      //println("traces:" + trace)
      if (node.cl.isEmpty || node.allVisited()){
        //backup
        //println("backup.")
        node.parent.markVisited(node)
        node = node.parent
        depth = depth - 1
        trace = trace.take(trace.length-1)//remove tail
      }
      val pn = node
      node = node.dfsVisit()
      if (pn!=node){
        depth = depth + 1
        trace = trace:+node//append
      }
    }
    trace.map { x => x.lastW }
  }

  def main(args:Array[String]){
    val instr = StdIn.readLine()
    val m = StdIn.readLine().toInt
    val outList = playscale(instr, m)
    if (outList.length==m){
      println("YES")
      println(outList.mkString(" "))
    }else{
      println("NO")
    }
  }
}

import scala.io.StdIn

object Watermelon {

  def canDivide(w:Int):String = {
    if (w>=4 && w%2==0) "YES" else "NO"
  }

  def main(args: Array[String]) {
    val ln:String = StdIn.readLine()
    val weight:Int = ln.toInt
    println(canDivide(weight))
  }
}

import scala.io.StdIn

object XeniaWeights {

  //return the min number in the inarr larger than seed, return 0 if not avail
  def getMin(inarr:Array[Int], seed:Int):Int={
    for (a<-1 to inarr.length){
      if (inarr(a-1)>seed){
        return inarr(a-1)
      }
    }
    return 0
  }

  def playscale(instr:String, m:Int):List[Int]={
    var inlist:List[Int] = List()
    for (a<-1 to instr.length()){
      if (instr(a-1)!='0'){
        inlist = inlist:+a
      }
    }
    val inarr:Array[Int] = inlist.toArray
    var resList:List[Int] = List()
    var gap=0; //gap weight between current side to the opposite side
    var sums:Array[Int]=Array(0,0)
    var turn=0;
    for (a<-1 to m){
      val w = getMin(inarr, gap)
      if (w==0){
        return resList
      }else{
        sums(turn) = sums(turn)+w
        resList = resList:+w
        val nextTurn = (turn+1)%2
        gap = sums(turn)-sums(nextTurn)
        turn = nextTurn
      }
    }
    return resList
  }

  def main(args:Array[String]){
    val instr = StdIn.readLine()
    val m = StdIn.readLine().toInt
    val outList = playscale(instr, m)
    if (outList.length==m){
      println("YES")
      println(outList.mkString(" "))
    }else{
      println("NO")
    }
  }
}


import java.util.Scanner
import scala.collection.mutable.Set
object PocketBook {
  def main(args: Array[String]) {
    val sc = new Scanner(System.in)
    val n = sc.nextInt
    val m = sc.nextInt
    val sets = new Array[Set[String]](m)
    for (j <- 0 until m) {
      sets(j) = Set()
    }
    for (i <- 0 until n) {
      val cs = sc.next.toCharArray
      for (j <- 0 until m) {
        sets(j) += cs(j).toString
      }
    }
    var l = 1: BigInt;
    for (j <- 0 until m) {
      l *= sets(j).size
    }
    l %= 1000000007
    println(l)
  }
}

import java.util

import scala.util.Sorting
import scala.io.StdIn

object App {

  def main(args: Array[String]) {

    val max = 1000000007;
    val (n, m) = readTuple()
    val A = new Array[Set[Char]](m)
    for(i <- 0 to m - 1)
      A(i) = Set()

    for(i <- 0 to n - 1)
      StdIn.readLine().zipWithIndex.foreach(e=> A(e._2) += e._1)

    println(A.foldLeft(1L)((s,a)=> (s * a.size) % max ))

  }

  def readInt(): Int = StdIn.readInt()

  def readInts(): Array[Int] = StdIn.readLine().split(" ").map(_.toInt)

  def readTuple(): (Int, Int) = {
    val line = readInts
    (line(0), line(1))
  }

}

import java.util

import scala.util.Sorting
import scala.io.StdIn

object App {

  def main(args: Array[String]) {

    val max = 1000000007;
    val (n, m) = readTuple()
    val A = new Array[Set[Char]](m)
    for(i <- 0 to m - 1)
      A(i) = Set()

    for(i <- 0 to n - 1)
      StdIn.readLine().zipWithIndex.foreach(e=> A(e._2) += e._1)

    println(A.foldLeft(1)((s,a)=> (s * a.size) % max ))

  }

  def readInt(): Int = StdIn.readInt()

  def readInts(): Array[Int] = StdIn.readLine().split(" ").map(_.toInt)

  def readTuple(): (Int, Int) = {
    val line = readInts
    (line(0), line(1))
  }

}