Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.For each of the m ghosts Anya knows the time at which it comes: the i-th visit will happen wi seconds after midnight, all wi's are distinct. Each visit lasts exactly one second.What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time.

The first line contains three integers m, t, r (1 ≤ m, t, r ≤ 300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.  The next line contains m space-separated numbers wi (1 ≤ i ≤ m, 1 ≤ wi ≤ 300), the i-th of them repesents at what second after the midnight the i-th ghost will come. All wi's are distinct, they follow in the strictly increasing order.

If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that. If that is impossible, print  - 1.

NoteAnya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.In the third sample test the answer is  - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes.

/**
 * Created by helfper on 27/01/2015.
 */
object AnyaAndGhosts {
  def main(args: Array[String]) {
    val Array(m, t, r) = io.StdIn.readLine.split(" ").map(_.toInt)
    val ghosts = io.StdIn.readLine.split(" ").map(_.toInt)

    println(solve(ghosts, t, r))
  }

  def solve(ghosts: Array[Int], t: Int, r: Int): Int = {
    val candles = Array.fill(r)(0)

    def iterate(g: Int): Int = {
      var n = 0
      0.until(r).foreach(i =>
        if (candles(i) < g) {
          candles(i) = g+t-n-1
          n += 1;
        }
      )
      if (t < n) -1
      else n
    }

    ghosts.foldLeft(0) { (r, g) =>
      val it = iterate(g)
      if (it == -1) it
      else r + it
    }
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(m, t, r) = in.next().split(' ').map(_.toInt)
  if (r > t)
    println(-1)
  else {
    val data = in.next().split(' ').map(_.toInt)
    val (count, _, _) = data.tail.foldLeft(r, data.head, (1 to r).map(i => data.head - i).toList) {
      case (acc, el) if acc._1 == -1 => acc
      case ((sum, previous, fired), el) =>
        val firedBefore = el - t
//        println("el = " + el)
//        println("sum = " + sum)
//        println("previous = " + previous)
//        println("fired = " + fired)
        val left = fired.filter(_ >= firedBefore)
        val shouldFire = r - left.size
//        println("shouldFire = " + shouldFire)
        if (shouldFire <= 0)
          (sum, el, left)
        else if (shouldFire > (el - previous))
          (-1, el, left)
        else
          (sum + shouldFire, el, (1 to shouldFire).map(i => el - i).toList ::: left)
    }
    println(count)
  }
}

//5 3 2
//1 2 5 10 20

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

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

  def solve: Int = {
    val m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt + t + 1
    }
    var ans = 0
    val candles = new Array[Int](1000)
    for (i <- 0 until m) {
      var sum = 0
      for (j <- w(i) - 1 to w(i) - t  by -1) {
        sum += candles(j)
      }
      if (sum != r) {
        for (j <- w(i) - 1 to w(i) - t by -1) {
          if (sum < r && candles(j) == 0) {
            candles(j) = 1
            sum += 1
          }
        }
        if (sum < r) {
          out.println(-1)
          return 1
        }
      }
    }
    candles.foreach(ans += _)
    out.println(ans)
    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
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(m, t, r) = in.next().split(' ').map(_.toInt)
  if (r > t)
    println(-1)
  else {
    val data = in.next().split(' ').map(_.toInt)
    val (count, _, _) = data.tail.foldLeft(r, data.head, (1 to r).map(i => data.head - i).toList) {
      case (acc, el) if acc._1 == -1 => acc
      case ((sum, previous, fired), el) =>
        val firedBefore = el - t
//        println("el = " + el)
//        println("sum = " + sum)
//        println("previous = " + previous)
//        println("fired = " + fired)
        val left = fired.filter(_ > firedBefore)
        val shouldFire = r - left.size
        if (shouldFire <= 0)
          (sum, el, left)
        else if (shouldFire > (el - previous))
          (-1, el, left)
        else
          (sum + shouldFire, el, (1 to shouldFire).map(i => el - i).toList ::: left)
    }
    println(count)
  }
}

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

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

  def solve: Int = {
    val m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt
    }
    var ans = 0
    val fullWindow = t - r + 1
    var currWindow = fullWindow
    var currR = r
    ans += r
    for (i <- 1 until m) {
      val diff = w(i) - w(i - 1)
      if (diff <= currWindow) {
          currWindow -= diff
      } else {
        if (diff - currWindow >= r) {
          ans += r
          currWindow = fullWindow
        } else {
          ans += diff - currWindow
          currR = diff - currWindow
          currWindow = fullWindow
        }
      }
    }
    out.println(ans)
    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
  }
}

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

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

  def solve: Int = {
    val m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt
    }
    var ans = 0
    val window = t - r + 1
    ans += r
    for (i <- 1 until m) {
      val diff = w(i) - w(i - 1)
      if (diff <= window) {

      } else {
        if (diff - window > r) {
          ans += r
        } else {
          ans += diff - window
        }
      }
    }
    out.println(ans)
    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
  }
}

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

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

  def solve: Int = {
    val m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt + t + 1
    }
    var ans = 0
    val candles = new Array[Int](500)
    for (i <- 0 until m) {
      var sum = 0
      for (j <- w(i) to w(i) - t by -1) {
        sum += candles(j)
      }
      if (sum != r) {
        for (j <- w(i) to w(i) - t by -1) {
          if (sum < r && candles(j) == 0) {
            candles(j) = 1
            sum += 1
          }
        }
        if (sum < r) {
          out.println(-1)
          return 1
        }
      }
    }
    candles.foreach(ans += _)
    out.println(ans)
    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
  }
}

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

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

  def solve: Int = {
    val m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt + t + 1
    }
    var ans = 0
    val candles = new Array[Int](500)
    for (i <- 0 until m) {
      var sum = 0
      for (j <- w(i) - 1 to w(i) - t by -1) {
        sum += candles(j)
      }
      if (sum != r) {
        for (j <- w(i) to w(i) - t by -1) {
          if (sum < r && candles(j) == 0) {
            candles(j) = 1
            sum += 1
          }
        }
        if (sum < r) {
          out.println(-1)
          return 1
        }
      }
    }
    candles.foreach(ans += _)
    out.println(ans)
    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
  }
}


Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive.Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.

The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets. Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.

If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n). If there are multiple answers, print any of them.

NoteConsider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].

//package edu29

import scala.collection.immutable.IntMap
import scala.io.StdIn

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val tree = {
    var pointMap = IntMap[Int]()
    for (tv <- tvs) {
      pointMap += tv(0) -> (pointMap.getOrElse(tv(0), 0) + 1)
      pointMap += (tv(1) + 1) -> (pointMap.getOrElse(tv(1) + 1, 0) - 1)
    }
    val points = Array.ofDim[Int](pointMap.size)
    val counts = Array.ofDim[Int](pointMap.size)
    var i = 0
    for ((p, c) <- pointMap) {
      points(i) = p
      counts(i) = c
      i += 1
    }

    def sumCount(i: Int): Unit =
      if (i < points.length) {
        counts(i) += counts(i - 1)
        sumCount(i + 1)
      }
    sumCount(1)
    new QueueTree(points, counts)
  }

  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(tvs(i)(0), tvs(i)(1)) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(keys: Array[Int], values: Array[Int]) {
  private[this] val total = Array.ofDim[Int](values.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = values(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }
  initGo(0, values.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (keys(a) > r || keys(b) < l) 1000000000
      else if (keys(a) >= l && keys(b) <= r) total(j)
      else {
        val m = median(a, b)
        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, values.length - 1, 0)
  }
}

//package edu29

import java.util

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val tree = {
    val pointMap = new util.HashMap[Int, Int]
    for (tv <- tvs) {
      pointMap.put(tv(0), pointMap.getOrDefault(tv(0), 0) + 1)
      pointMap.put(tv(1) + 1, pointMap.getOrDefault(tv(1) + 1, 0) - 1)
    }
    val points = Array.ofDim[Int](pointMap.size())
    val counts = Array.ofDim[Int](pointMap.size())
    val iter = pointMap.keySet().iterator()
    def initPoints(i: Int): Unit =
      if (iter.hasNext) {
        points(i) = iter.next()
        initPoints(i + 1)
      }
    initPoints(0)
    util.Arrays.sort(points)
    def initCounts(i: Int): Unit =
      if (i < points.length) {
        counts(i) = pointMap.get(points(i))
        initCounts(i + 1)
      }
    initCounts(0)
    def sumCount(i: Int): Unit =
      if (i < points.length) {
        counts(i) += counts(i - 1)
        sumCount(i + 1)
      }
    sumCount(1)
    new QueueTree(counts, points)
  }

  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(tvs(i)(0), tvs(i)(1)) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(mins: Array[Int], values: Array[Int]) {
  private[this] val total = Array.ofDim[Int](mins.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = mins(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }
  initGo(0, mins.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (values(a) > r || values(b) < l) 1000000000
      else if (values(a) >= l && values(b) <= r) total(j)
      else {
        val m = median(a, b)
        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, mins.length - 1, 0)
  }
}

//package edu29

import java.util

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val tree = {
    val pointMap = new util.TreeMap[Int, Int]
    for (tv <- tvs) {
      pointMap.put(tv(0), pointMap.getOrDefault(tv(0), 0) + 1)
      pointMap.put(tv(1) + 1, pointMap.getOrDefault(tv(1) + 1, 0) - 1)
    }
    val points = Array.ofDim[Int](pointMap.size())
    val counts = Array.ofDim[Int](pointMap.size())
    val iter = pointMap.entrySet().iterator()
    def init(i: Int): Unit =
      if (iter.hasNext) {
        val e = iter.next()
        points(i) = e.getKey
        counts(i) = e.getValue
        init(i + 1)
      }
    init(0)
    def sumCount(i: Int): Unit =
      if (i < points.length) {
        counts(i) += counts(i - 1)
        sumCount(i + 1)
      }
    sumCount(1)
    new QueueTree(counts, points)
  }

  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(tvs(i)(0), tvs(i)(1)) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(mins: Array[Int], values: Array[Int]) {
  private[this] val total = Array.ofDim[Int](mins.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = mins(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }
  initGo(0, mins.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (values(a) > r || values(b) < l) 1000000000
      else if (values(a) >= l && values(b) <= r) total(j)
      else {
        val m = median(a, b)
        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, mins.length - 1, 0)
  }
}

//package edu29

import java.util

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)
  for (tv <- tvs) {tv(1) += 1 }

  val pointArr = tvs.flatten.distinct
  util.Arrays.sort(pointArr)
  val points = mutable.HashMap[Int, Int]()
  def fillPoints(i: Int): Unit =
    if (i < pointArr.length) {
      points(pointArr(i)) = i
      fillPoints(i + 1)
    }
  fillPoints(0)
  val counts = Array.ofDim[Int](points.size + 1)

  for (lr <- tvs) {
    counts(points(lr(0))) += 1
    counts(points(lr(1))) -= 1
  }
  var i = 1
  while (i < counts.length) {
    counts(i) += counts(i - 1)
    i += 1
  }

  val tree = new QueueTree(counts)
  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(points(tvs(i)(0)), points(tvs(i)(1)) - 1) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(init: Array[Int]) extends Traversable[Int] {
  private[this] val total = Array.ofDim[Int](init.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = init(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }

  initGo(0, init.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (a > r || b < l) 1000000000
      else if (a >= l && b <= r) total(j)
      else {
        val m = median(a, b)

        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, init.length - 1, 0)
  }

  def foreach[U](f: Int => U): Unit = {
    def go(a: Int, b: Int, i: Int): Unit =
      if (a == b) f(total(i))
      else {
        val m = median(a, b)
        go(a, m, 2 * i + 1)
        go(m + 1, b, 2 * i + 2)
      }
    go(0, init.length - 1, 0)
  }
}

//package edu29

import scala.collection.immutable.IntMap
import scala.io.StdIn

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val tree = {
    var pointMap = IntMap[Int]().withDefaultValue(0)
    for (tv <- tvs) {
      pointMap += tv(0) -> (pointMap(tv(0)) + 1)
      pointMap += (tv(1) + 1) -> (pointMap(tv(1) + 1) - 1)
    }
    val points = Array.ofDim[Int](pointMap.size)
    val counts = Array.ofDim[Int](pointMap.size)
    var i = 0
    for ((p, c) <- pointMap) {
      points(i) = p
      counts(i) = c
      i += 1
    }

    def sumCount(i: Int): Unit =
      if (i < points.length) {
        counts(i) += counts(i - 1)
        sumCount(i + 1)
      }
    sumCount(1)
    new QueueTree(points, counts)
  }

  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(tvs(i)(0), tvs(i)(1)) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(keys: Array[Int], values: Array[Int]) {
  private[this] val total = Array.ofDim[Int](values.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = values(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }
  initGo(0, values.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (keys(a) > r || keys(b) < l) 1000000000
      else if (keys(a) >= l && keys(b) <= r) total(j)
      else {
        val m = median(a, b)
        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, values.length - 1, 0)
  }
}

//package edu29

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val points = tvs.flatten.distinct.sorted.zipWithIndex.toMap
  val counts = Array.ofDim[Int](points.size + 1)

  for (lr <- tvs) {
    counts(points(lr(0))) += 1
    counts(points(lr(1)) + 1) -= 1
  }
  var i = 1
  while (i < counts.length) {
    counts(i) += counts(i - 1)
    i += 1
  }

  val tree = new QueueTree(counts)
  val result = tvs.iterator.zipWithIndex.collectFirst {
    case (lr, i) if tree.queue(points(lr(0)), points(lr(1))) > 1 => i + 1
  }.getOrElse(-1)

  println(result)
}

class QueueTree(init: Array[Int]) extends Traversable[Int] {
  val total = Array.ofDim[Int](init.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = init(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }

  initGo(0, init.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (a > r || b < l) 1000000000
      else if (a >= l && b <= r) total(j)
      else {
        val m = median(a, b)

        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, init.length - 1, 0)
  }

  def foreach[U](f: Int => U): Unit = {
    def go(a: Int, b: Int, i: Int): Unit =
      if (a == b) f(total(i))
      else {
        val m = median(a, b)
        go(a, m, 2 * i + 1)
        go(m + 1, b, 2 * i + 2)
      }
    go(0, init.length - 1, 0)
  }
}

//package edu29

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val points = tvs.flatten.distinct.sorted.zipWithIndex.toMap
  val counts = Array.ofDim[Int](points.size + 1)

  for (lr <- tvs) {
    counts(points(lr(0))) += 1
    counts(points(lr(1)) + 1) -= 1
  }
  var i = 1
  while (i < counts.length) {
    counts(i) += counts(i - 1)
    i += 1
  }

  val tree = new QueueTree(points.size - 1, counts)
  val result = tvs.iterator.zipWithIndex.collectFirst {
    case (lr, i) if tree.queue(points(lr(0)), points(lr(1))) > 1 => i + 1
  }.getOrElse(-1)

  println(result)
}

class QueueTree(n: Int, init: Array[Int]) extends Traversable[Int] {
  protected val totalSize = {
    var ts = 0
    def go(a: Int, b: Int, i: Int): Unit =
      if (a == b) ts = ts max i else {
        val m = median(a, b)
        go(a, m, 2 * i + 1)
        go(m + 1, b, 2 * i + 2)
      }
    go(0, n, 0)
    ts
  }

  val total = Array.ofDim[Int](totalSize + 1)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = init(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }

  initGo(0, n, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (a > r || b < l) 1000000000
      else if (a >= l && b <= r) total(j)
      else {
        val m = median(a, b)

        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, n, 0)
  }

  def foreach[U](f: Int => U): Unit = {
    def go(a: Int, b: Int, i: Int): Unit =
      if (a == b) f(total(i))
      else {
        val m = median(a, b)
        go(a, m, 2 * i + 1)
        go(m + 1, b, 2 * i + 2)
      }
    go(0, n, 0)
  }
}


Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.

The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.

Output a single integer representing the maximum number of the train routes which can be closed.

import java.util.Scanner

/**
 * Created by kshim on 8/08/2014.
 */
object CF257_1B extends App {
  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  case class Road(u:Int, v:Int, w:Long)
  case class Train(u:Int, w:Long)

  def Process(n:Int, m:Int, k:Long, roads:Seq[Road], trains:Seq[Train]):Long = {
    val trainDists = Array.fill(n + 1) {
      Long.MaxValue
    }
    val roadDists = Array.fill(n + 1) {
      collection.mutable.Map[Int, Long]()
    }
    for (t <- trains) {
      trainDists(t.u) = trainDists(t.u) min t.w
      roadDists(1)(t.u) = roadDists(1).getOrElse(t.u, t.w) min t.w
      roadDists(t.u)(1) = roadDists(1)(t.u)
    }
    for (r <- roads) {
      roadDists(r.u)(r.v) = roadDists(r.u).getOrElse(r.v, Long.MaxValue) min r.w
      roadDists(r.v)(r.u) = roadDists(r.v).getOrElse(r.u, Long.MaxValue) min r.w
      if(r.v == 1 && trainDists(r.u) >= r.w) {
        trainDists(r.u) = Long.MaxValue
      }
      if(r.u == 1 && trainDists(r.v) >= r.w) {
        trainDists(r.v) = Long.MaxValue
      }
    }
    //println(trainDists.toSeq.toString)

    val visited = collection.mutable.BitSet(n + 1)
    val pq = new java.util.PriorityQueue[(Int, Long)](1, Ordering.by(_._2))
    pq.add((1, 0))

    val dist = Array.fill(n + 1) {
      Long.MaxValue
    }
    dist(1) = 0

    while (!pq.isEmpty) {
      val (u, uDist) = pq.poll()
      if (!visited(u)) {
        visited.add(u)
        for ((v, w) <- roadDists(u)) {
          val newDist = uDist + w
          if (newDist < dist(v)) {
            dist(v) = newDist
            pq.add((v, newDist))
          }
          if( trainDists(v) != Long.MaxValue && u > 1 && newDist <= trainDists(v)){
            trainDists(v) = Long.MaxValue
          }
        }
      }
    }
    k - trainDists.count(_ != Long.MaxValue)
  }

  val Array(n,m,k) = readInts(3)
  val roads = (1 to m).map(_ => {
    val arr = readInts(3)
    Road(arr(0), arr(1), arr(2))
  })
  val trains = (1 to k).map(_ => {
    val arr = readInts(2)
    Train(arr(0), arr(1))})
  println(Process(n, m, k, roads, trains))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = other.dist.compare(this.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val edge = pq.dequeue()
    if (!visited(edge.v)) {
      visited += edge.v
      for ((v, d) <- adj(edge.v)) {
        val newDist = edge.dist + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (edge.v > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = java.lang.Long.compare(this.dist, other.dist)
  }

  //val pq = mutable.PriorityQueue.empty[Edge]
  val pq = new java.util.PriorityQueue[Edge](1)
  pq.add(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = if (this.dist == other.dist) this.v.compare(other.v)
      else -this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = this.dist.compare(other.dist)
  }

  val pq = new java.util.PriorityQueue[Edge](1)//, Ordering.by(_._2))
  pq.add(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue[(Int, Long)](1, Ordering.by(_._2))
  pq.add((0, 0))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val (u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add((v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  val pq = new java.util.PriorityQueue[(Int, Long)](1, Ordering.by(_._2))
  pq.add((0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val (u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add((v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = if (this.dist == other.dist) this.v.compare(other.v)
      else -this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val edge = pq.dequeue()
    if (!visited(edge.v)) {
      visited += edge.v
      for ((v, d) <- adj(edge.v)) {
        val newDist = edge.dist + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (edge.v > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = java.lang.Long.compare(other.dist, this.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = java.lang.Long.compare(other.dist, this.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val edge = pq.dequeue()
    if (!visited(edge.v)) {
      visited += edge.v
      for ((v, d) <- adj(edge.v)) {
        val newDist = edge.dist + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (edge.v > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = -this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val edge = pq.dequeue()
    if (!visited(edge.v)) {
      visited += edge.v
      for ((v, d) <- adj(edge.v)) {
        val newDist = edge.dist + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (edge.v > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = java.lang.Long.compare(other.dist, this.dist)
  }

  //val pq = mutable.PriorityQueue.empty[Edge]
  val pq = new java.util.PriorityQueue[Edge](1)
  pq.add(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = if (this.dist == other.dist) this.v.compare(other.v)
      else this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

/**
 * I've tried to make this piece of code as readable as possible.
 * Enjoy hacking it!
 */
import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = mutable.Map.empty[(Int, Int), Int].withDefaultValue(Int.MaxValue)

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u, v)) {
      roads.put((u, v), x)
      roads.put((v, u), x)
    }
  }

  var removed = 0

  val rails = Array.fill(n) { -1 }
  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1) rails(s) = y
    else if (y < rails(s)) {
      rails(s) = y
      removed += 1
    } else removed += 1
  }

  val adjB = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val adjRoadB = Array.fill(n) { new mutable.ArrayBuilder.ofInt }

  for (((u, v), x) <- roads) {
    adjB(u) += v
    adjRoadB(u) += x
  }

  val adj = adjB.map(_.result)
  val adjRoad = adjRoadB.map(_.result)

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  val heap = Array.ofDim[Long](n)
  var heapSize = 0
  val idPos = Array.ofDim[Int](n)
  val posId = Array.ofDim[Int](n)

  @inline def swap[T](as: Array[T], u: Int, v: Int): Unit = {
    val tmp = as(u)
    as(u) = as(v)
    as(v) = tmp
  }

  @inline def swapHeap(u: Int, v: Int): Unit = {
    swap(heap, u, v)
    swap(posId, u, v)
    swap(idPos, u, v)
  }

  def bubbleDown(pos0: Int): Unit = {
    var pos = pos0
    while (pos < (heapSize >> 1)) {
      var child = 2 * pos + 1
      if (child + 1 < heapSize && heap(child + 1) < heap(child)) {
        child += 1
      }
      if (heap(pos) <= heap(child)) pos = heapSize + 1
      else {
        swapHeap(pos, child)
        pos = child
      }
    }
  }

  def bubbleUp(pos0: Int): Unit = {
    var pos = pos0
    while (pos > 0) {
      val parent = (pos - 1) >> 1
      if (heap(pos) >= heap(parent)) pos = -1
      else {
        swapHeap(pos, parent)
        pos = parent
      }
    }
  }

  def extractMin(): Int = {
    val res = posId(0)
    heapSize -= 1
    val lastNode = heap(heapSize)
    if (heapSize > 0) {
      heap(0) = lastNode
      val id = posId(heapSize)
      idPos(id) = 0
      posId(0) = id
      bubbleDown(0)
    }
    res
  }

  def decKey(id: Int, key: Long): Unit = {
    val pos = idPos(id)
    heap(pos) = key
    bubbleUp(pos)
  }

  def add(id: Int, key: Long): Unit = {
    heap(heapSize) = key
    idPos(id) = heapSize
    posId(heapSize) = id
    bubbleUp(heapSize)
    heapSize += 1
  }

  add(0, 0)

  while (heapSize > 0) {
    val u = extractMin()
    if (dist(u) <= rails(u)) removed += 1
    else if (rails(u) >= 0) dist(u) = rails(u)
    for (i <- adj(u).indices) {
      val v = adj(u)(i)
      val newDist = dist(u) + adjRoad(u)(i)
      if (newDist < dist(v)) {
        if (dist(v) == Long.MaxValue) add(v, newDist)
        else decKey(v, newDist)
        dist(v) = newDist
      }
    }
  }

  println(removed)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

/**
 * I've tried to make this piece of code as readable as possible.
 * Enjoy hacking it!
 */
import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = mutable.Map.empty[(Int, Int), Int]

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads.getOrElse((u, v), Int.MaxValue)) {
      roads.put((u, v), x)
      roads.put((v, u), x)
    }
  }

  var removed = 0

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1) rails(s) = y
    else if (y < rails(s)) {
      rails(s) = y
      removed += 1
    } else removed += 1
  }

  for (i <- 1 until n) {
    if (rails(i) > 0 && rails(i) < roads.getOrElse((0, i), Int.MaxValue)) {
      roads.put((0, i), rails(i))
      removed -= 1
    }
  }

  val adjB = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val adjRoadB = Array.fill(n) { new mutable.ArrayBuilder.ofInt }

  for (((u, v), x) <- roads) {
    adjB(u) += v
    adjRoadB(u) += x
  }

  val adj = adjB.map(_.result)
  val adjRoad = adjRoadB.map(_.result)

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      if (dist(u) <= rails(u)) removed += 1
      else if (rails(u) >= 0) dist(u) = rails(u)
      for (i <- adj(u).indices) {
        val v = adj(u)(i)
        val newDist = dist(u) + adjRoad(u)(i)
        if (newDist < dist(v)) {
        	dist(v) = newDist
          pq.add(v)
        }
      }
    }
  }

  println(removed)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) > 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = dist(u) + d
        if (newDist < dist(v)) {
        	dist(v) = newDist
          pq.add(v)
          if (u > 0 && newDist <= rails(v)) rails(v) = -1
        }
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) > 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dist = Array.fill(n) { Long.MaxValue }
  val steps = Array.fill(n) { 0 }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      if (dist(u) <= rails(u) && steps(u) > 1) rails(u) = -1
      for ((v, d) <- roads(u)) {
        val newDist = dist(u) + d
        if (newDist < dist(v)) {
          steps(v) = steps(u) + 1
        	dist(v) = newDist
          pq.add(v)
        }
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = dist(u) + d
        if (newDist < dist(v)) {
        	dist(v) = newDist
          pq.add(v)
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) > 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = dist(u) + d
        if (newDist < dist(v)) {
        	dist(v) = newDist
          pq.add(v)
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0

  val pq = mutable.PriorityQueue((0, 0L))(Ordering.by(_._2))
  //val pq = new java.util.PriorityQueue[(Int, Long)](1, Ordering.by(_._2))
  //pq.add((0, 0))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val (u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue((v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}


You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.

Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.

NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).

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 = {
    REP(ni()) { _ =>
      val N, K = ni()
      val q = N / K
      val m = N % K
      REP(K) { i =>
        val cnt = if (i < m) q + 1 else q
        REP(cnt){_ => out.print((i + 'a').toChar) }
      }
      out.println()
    }
  }

  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]) {

    val std = scala.io.StdIn
    val t = std.readLine().toInt

    for (_ <- 0 until t) {
      val values = std.readLine().split(" ").map(_.toInt)
      val n = values(0)
      val k = values(1)
      val stringBuilder = new StringBuilder("")
      for (i <- 0 until n) {
        stringBuilder.append(('a' + i % k).toChar)
      }
      println(stringBuilder.toString())
    }

  }
}

object CF_527_3_A {
  def solve(xs: Seq[(Int, Int)]): Seq[String] = {
    xs.map{
      case (n, k) =>
        val str = ('a' to ('a' + k - 1).toChar).mkString
        str * (n/k) + str.take(n%k)
    }
  }

  // Specify Input and Output formats here:
  def formatIn(i: Input) = Seq.fill(i.int)((i.int, i.int))
  def formatOut(out: Seq[String]): String = out.mkString("\n")

//     ~~~ Boilerplate & utility methods that don't change ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  def main(args: Array[String]) = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  import java.util.Scanner
  class Input(sc: Scanner) {
    def this(i: java.io.InputStream) = this(new Scanner(i))
    def this(s: String) = this(new Scanner(s.stripMargin))
    def int = sc.nextInt()
    def long = sc.nextLong()
    def double = sc.nextDouble()
    def nextLine = sc.nextLine()
    // Remember to call nextLine to advance past the newline character (if required) before collecting the line:
    def collect[T](f: String => T) = {sc.nextLine().split(" ").toVector.map(f)}
    def intSeq = collect(_.toInt)
    def doubleSeq = collect(_.toDouble)
    def getLines: Vector[String] = if (!sc.hasNextLine) Vector.empty else sc.nextLine +: getLines
    def solveVal = (solve _).tupled(formatIn(this))
    def solveStr = formatOut(solveVal)
  }

  def ceil(a: Int, b: Int) = (a - 1) / b + 1
  def ceil(a: Long, b: Long) = (a - 1) / b + 1
  val modulo = 1000000007

  import language.implicitConversions
  implicit def stringToInput(s: String): Input = new Input(s)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) { def tupled: T => R = x => f(x) }
}

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

  override def apply(io: IO): io.type = {
    val input = io.read[Seq[(Int, Int)]]
    val ans = input map {case (n, k) =>
      val res = for {
        i <- 0 until k
        j <- 1 to (n/k)
      } yield ('a' + i).toChar
      res.padTo(n, res.last).mkString
    }
    io.writeAll(ans, separator = "\n")
  }
}
/****************************[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 CF_527_3_A {
  def solve(xs: Seq[(Int, Int)]): Seq[String] = {
    xs.map{
      case (n, k) =>
        println(s"$n $k")
        val str = ('a' to ('a' + k - 1).toChar).mkString
        str * (n/k) + str.take(n%k)
    }
  }

  // Specify Input and Output formats here:
  def formatIn(i: Input) = Seq.fill(i.int)((i.int, i.int))
  def formatOut(out: Seq[String]): String = out.mkString("\n")

//     ~~~ Boilerplate & utility methods that don't change ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  def main(args: Array[String]) = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  import java.util.Scanner
  class Input(sc: Scanner) {
    def this(i: java.io.InputStream) = this(new Scanner(i))
    def this(s: String) = this(new Scanner(s.stripMargin))
    def int = sc.nextInt()
    def long = sc.nextLong()
    def double = sc.nextDouble()
    def nextLine = sc.nextLine()
    // Remember to call nextLine to advance past the newline character (if required) before collecting the line:
    def collect[T](f: String => T) = {sc.nextLine().split(" ").toVector.map(f)}
    def intSeq = collect(_.toInt)
    def doubleSeq = collect(_.toDouble)
    def getLines: Vector[String] = if (!sc.hasNextLine) Vector.empty else sc.nextLine +: getLines
    def solveVal = (solve _).tupled(formatIn(this))
    def solveStr = formatOut(solveVal)
  }

  def ceil(a: Int, b: Int) = (a - 1) / b + 1
  def ceil(a: Long, b: Long) = (a - 1) / b + 1
  val modulo = 1000000007

  import language.implicitConversions
  implicit def stringToInput(s: String): Input = new Input(s)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) { def tupled: T => R = x => f(x) }
}


While Duff was resting in the beach, she accidentally found a strange array b0, b1, ..., bl - 1 consisting of l positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array, a0, ..., an - 1 that b can be build from a with formula: bi = ai mod n where a mod b denoted the remainder of dividing a by b.  Duff is so curious, she wants to know the number of subsequences of b like bi1, bi2, ..., bix (0 ≤ i1 < i2 < ... < ix < l), such that:   1 ≤ x ≤ k  For each 1 ≤ j ≤ x - 1,   For each 1 ≤ j ≤ x - 1, bij ≤ bij + 1. i.e this subsequence is non-decreasing. Since this number can be very large, she want to know it modulo 109 + 7.Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number.

The first line of input contains three integers, n, l and k (1 ≤ n, k, n × k ≤ 106 and 1 ≤ l ≤ 1018). The second line contains n space separated integers, a0, a1, ..., an - 1 (1 ≤ ai ≤ 109 for each 0 ≤ i ≤ n - 1).

Print the answer modulo 1 000 000 007 in one line.

NoteIn the first sample case, . So all such sequences are: , , , , , , , ,  and .

object B extends App {

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

  val Array(_n, l, _k) = readLongs(3)
  val n = _n.toInt
  val k = if (l % _n == 0) (_k min (l / _n)).toInt else (_k min (l / _n + 1)).toInt
  val remainder = (l % n).toInt
  val MOD = 1000000007L

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

  val _as = readInts(n).zipWithIndex.sorted
  //val _as = Array.fill(n){ Random.nextInt }.zipWithIndex.sorted
  val as = Array.ofDim[(Int, Int)](n)

  as(0) = (1, _as(0)._2)
  for (i <- 1 until n) {
    as(i) = (if (_as(i)._1 == _as(i - 1)._1) as(i - 1)._1 else as(i - 1)._1 + 1, _as(i)._2)
  }

  for (i <- as.indices) dp1(1)(as(i)._1) += 1

  var res = l % MOD

  for (i <- 2 to k) {
    var prev = 0
    var acc = 0
    for (j <- 0 until n) {
      val (a, aInd) = as(j)
      if (a != prev) acc = ((acc + dp1(i - 1)(a)) % MOD).toInt
      val d1 = if (i <= l / n) acc else 0
      dp1(i)(a) = (dp1(i)(a) + d1) % MOD.toInt
      val d2 = if (aInd < remainder) acc else 0
      res = ((res + d1 * (l / n % MOD + MOD - i + 1) % MOD) + d2) % MOD
      prev = a
    }
  }

  println(res % MOD)
}

object B extends App {

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

  val Array(_n, l, _k) = readLongs(3)
  val n = _n.toInt
  val k = if (l % _n == 0) (_k min (l / _n)).toInt else (_k min (l / _n + 1)).toInt
  val remainder = (l % n).toInt
  val MOD = 1000000007L

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

  val _as = readInts(n).zipWithIndex.sorted
  val as = Array.ofDim[(Int, Int)](n)

  as(0) = (1, _as(0)._2)
  for (i <- 1 until n) {
    as(i) = (if (_as(i)._1 == _as(i - 1)._1) as(i - 1)._1 else as(i - 1)._1 + 1, _as(i)._2)
  }

  for (i <- as.indices) dp1(1)(as(i)._1) += 1

  var res = l % MOD

  for (i <- 2 to k) {
    var prev = 0
    var acc = 0
    for (j <- 0 until n) {
      val (a, aInd) = as(j)
      if (a != prev) acc = ((acc + dp1(i - 1)(a)) % MOD).toInt
      val d1 = if (i <= l / n) acc else 0
      dp1(i)(a) = (dp1(i)(a) + d1) % MOD.toInt
      val d2 = if (aInd < remainder) acc else 0
      res = ((res + d1 * (l / n % MOD + MOD - i + 1) % MOD) + d2) % MOD
      prev = a
    }
  }

  println(res % MOD)
}

import scala.collection._

object B extends App {

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

  val Array(_n, l, _k) = readLongs(3)
  val n = _n.toInt
  val k = if (l % _n == 0) (_k min (l / _n)).toInt else (_k min (l / _n + 1)).toInt
  val MOD = 1000000007L

  val _as = readInts(n).zipWithIndex.sorted
  val as = Array.ofDim[(Int, Int)](n)

  as(0) = (1, _as(0)._2)
  for (i <- 1 until n) {
    as(i) = (if (_as(i)._1 == _as(i - 1)._1) as(i - 1)._1 else as(i - 1)._1 + 1, _as(i)._2)
  }

  val cnts = Array.fill(n + 1) { 0 }
  for (a <- as) cnts(a._1) += 1

  val MODB = BigInt(MOD)
  val fs, fs2, fInvs = Array.ofDim[Long](k + 1)

  var f1 = 1L
  fInvs(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fInvs(i) = BigInt(f1).modInverse(MODB).toLong
    f1 = f1 * (i + 1) % MOD
  }

  val base = l / _n
  var f2 = base
  fs(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fs(i) = f2
    f2 = f2 * (base - i) % MOD
  }

  val base2 = l / _n + 1
  var f3 = base2
  fs2(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fs2(i) = f3
    f3 = f3 * (base2 - i) % MOD
  }

  def pascal(_k: Int): Long = {
    fs(_k) * fInvs(_k) % MOD
  }

  def pascal2(_k: Int): Long = {
    fs2(_k) * fInvs(_k) % MOD
  }

  val dp = Array.fill(k + 1, n + 1) { 0L }

  for (j <- dp(1).indices) dp(1)(j) = 1
  //println(pascal(1), pascal(2))
  var res = l % MOD

  for (i <- 2 to k) {
    var prev = 1
    for (j <- 0 until n) {
      val (a, aInd) = as(j)
      dp(i)(a) = (dp(i - 1)(a) * cnts(a) + (if (a > prev) dp(i)(a - 1) else 0)) % MOD
      val p = if (aInd < l % n) pascal2(i) else pascal(i)
      res = (res + dp(i)(a) * p) % MOD
      prev = a
    }
  }

  println(res % MOD)
}

import scala.collection._

object B extends App {

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

  val Array(_n, l, _k) = readLongs(3)
  val n = _n.toInt
  val k = if (l % _n == 0) (_k min (l / _n)).toInt else (_k min (l / _n + 1)).toInt
  val MOD = 1000000007L

  val _as = readInts(n).zipWithIndex.sorted
  val as = Array.ofDim[(Int, Int)](n)

  as(0) = (1, _as(0)._2)
  for (i <- 1 until n) {
    as(i) = (if (_as(i)._1 == _as(i - 1)._1) as(i - 1)._1 else as(i - 1)._1 + 1, _as(i)._2)
  }

  val cnts = Array.fill(n + 1) { 0 }
  for (a <- as) cnts(a._1) += 1

  val MODB = BigInt(MOD)
  val fs, fs2, fInvs = Array.ofDim[Long](k + 1)

  var f1 = 1L
  fInvs(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fInvs(i) = BigInt(f1).modInverse(MODB).toLong
    f1 = f1 * (i + 1) % MOD
  }

  val base = l / _n
  var f2 = base
  fs(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fs(i) = f2
    f2 = f2 * (base - i) % MOD
  }

  val base2 = l / _n + 1
  var f3 = base2
  fs2(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fs2(i) = f3
    f3 = f3 * (base2 - i) % MOD
  }

  def pascal(_k: Int): Long = {
    fs(_k) * fInvs(_k) % MOD
  }

  def pascal2(_k: Int): Long = {
    fs2(_k) * fInvs(_k) % MOD
  }

  val dp = Array.fill(k + 1, n + 1) { 0L }

  for (j <- dp(1).indices) dp(1)(j) = 1
  //println(pascal(1), pascal(2))
  var res = l

  for (i <- 2 to k) {
    var prev = 1
    for (j <- 0 until n) {
      val (a, aInd) = as(j)
      dp(i)(a) = (dp(i - 1)(a) * cnts(a) + (if (a > prev) dp(i)(a - 1) else 0)) % MOD
      val p = if (aInd < l % n) pascal2(i) else pascal(i)
      res = (res + dp(i)(a) * p) % MOD
      prev = a
    }
  }

  println(res)
}


Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:  he chooses two elements of the array ai, aj (i ≠ j);  he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1. The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times. Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.

The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.

Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val n = in.next().toInt
  val sum = in.next().split(' ').map(_.toInt).sum
  if (sum % n == 0)
    println(n)
  else
    println(n - 1)
}

import java.util.Scanner

object Test extends App {
  val sc = new Scanner(System.in);
  val n = sc.nextInt()
  val sum = (0 until n).map(_ => sc.nextInt()).sum
  println(if (sum % n == 0) n else n-1)
}

import java.util.Scanner
import scala.collection.mutable._

object Main extends App {
  val sc = new Scanner(System.in)

  val n = sc.nextInt()
  val a = Array.tabulate(n)(_ => sc.nextInt())

  val sum = a.fold(0)(_ + _)

  println(if (sum % n == 0) n else n-1)

}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

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

  def solve(): Int = {
    val N = sc.nextInt
    val S = List.fill(N)(sc.nextInt).sum
    if (((S % N) + N) % N == 0) N
    else N - 1
  }
  out.println(solve)
  out.close
}

object Main {
  def main(args: Array[String]): Unit = {
    val n = readInt()
    val a = readLine().split(" ").map(_.toInt)
    if (a.sum % n == 0) println(n)
    else println(n - 1)
  }
}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

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

  def solve(): Int = {
    val N = sc.nextInt
    val S = List.fill(N)(sc.nextInt).sum

    if (S % 2 == 0) N
    else N - 1
  }
  out.println(solve)
  out.close
}


You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.

First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \leq h_i \leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.

For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).

NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO.

import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader

object AShiftingStacks {
  def solve(hs: Array[Long]): String = {
    val l = hs.length
    if (l == 1) "yes"
    else {
      var s = 0L
      for (i <- hs.indices) {
        s += hs(i)
        if (s < i*(i+1)/2) return "no"
      }
      "yes"
    }
  }

  def main(args: Array[String]): Unit = {
    val file = new BufferedReader(new InputStreamReader(System.in))
//            val file = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
//"6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000".getBytes)))
//              "1\n3\n0 0 3".getBytes)))
    val inputs = file.readLine.toInt
    var i = 0
    while (i < inputs) {
      file.readLine
      val hs = file.readLine.split(" ").map(_.toLong)
      println(solve(hs))
      i += 1
    }
  }
}

import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader

object AShiftingStacks {
  def solve(hs: Array[Long]): String = {
    val l = hs.length
    if (hs.sum >= ((l-1)*l)/2) "yes" else "no"
  }

  def main(args: Array[String]): Unit = {
    val file = new BufferedReader(new InputStreamReader(System.in))
//            val file = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
//"6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000".getBytes)))
    //          "1\n50000000 4".getBytes)))
    val inputs = file.readLine.toInt
    var i = 0
    while (i < inputs) {
      file.readLine
      val hs = file.readLine.split(" ").map(_.toLong)
      println(solve(hs))
      i += 1
    }
  }
}