import java.util.Scanner

/**
 * Created by hama_du on 2014/06/21.
 */
object ProblemB extends App {
  def solve(persons: Array[Double]): Double = {
    if (persons.size == 1) {
      return persons(0)
    }
    val inverse = persons.map(p => 1.0d - p).fold(1.0)((a, b) => a * b)
    persons.map(p => p * inverse / (1.0d - p)).sum
  }

  val in = new Scanner(System.in)
  val n = in.nextInt
  val persons = (0 until n).map(_ => in.nextDouble()).sorted
  if (persons.max >= 1.0d) {
    println(1.0)
  } else {
    val answers = for {
      i <- 0 until n
      j <- i+1 to n
    } yield solve(persons.slice(i, j).toArray)
    println(answers.max)
  }
}

object Main {

  def main(args: Array[String]) {
    val lines = io.Source.stdin.getLines.toList
    val friendCountLine :: probabilityLine :: Nil = lines

    val friendCount = friendCountLine.toInt
    val probabilities = probabilityLine.split(" ").map(_.toDouble).toList

    val solution = solve(probabilities)
    print(solution)
  }

  def solve(probabilities: List[Double]): Double = {
    def tryNext(probabilities: List[Double], lastSolution: Double, friendAmount: Int): Double = {
      if(friendAmount > probabilities.size){
        lastSolution
      } else {
        val toCheck = probabilities.take(friendAmount)
        val actualSolution = calculateFor(toCheck)
        if(actualSolution <= lastSolution){
          lastSolution
        } else {
          tryNext(probabilities, actualSolution, friendAmount + 1)
        }
      }
    }

    val sortedProbabilities = probabilities.sorted.reverse
    tryNext(sortedProbabilities, 0, 1)
  }

  def calculateFor(probabilities: List[Double]) = {
    if(probabilities.size == 1){
      probabilities.head
    } else {
      val personProbabilities = for ((probability1, ix1) <- probabilities zip Stream.from(0)) yield {
        val otherPeopleReverses = for ((probability2, ix2) <- probabilities zip Stream.from(0) if ix1 != ix2) yield (1 - probability2)
        val otherPeople = otherPeopleReverses.fold(1d)(_ * _)
        probability1 * otherPeople
      }
      personProbabilities.fold(0d)(_ + _)
    }
  }

}

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

import scala.collection.mutable

object D {

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

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

  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 nextDouble: Double = return java.lang.Double.parseDouble(next)

  class MultiHashSet[T <% Comparable[T]] {
    val map = new mutable.HashMap[T, Int]()

    def count(x: T): Int = {
      return map.getOrElse(x, 0)
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  class MultiTreeSet[T <% Comparable[T]] {
    val map = new TreeMap[T, Int]()

    def count(x: T): Int = {
      val res = map.get(x)
      if (res == null)
        return 0
      return res
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def first(): T = return map.firstKey()

    def last(): T = return map.lastKey()

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  /**
   * Segment tree for any commutative function
   * @param values Array of Int
   * @param commutative function like min, max, sum
   * @param zero zero value - e.g. 0 for sum, Inf for min, max
   */
  class SegmentTree(values: Array[Int])(commutative: (Int, Int) => Int)(zero: Int) {
    private val SIZE = 1e5.toInt
    private val n = values.length
    val t = new Array[Int](2 * n)
    Array.copy(values, 0, t, n, n)

    // build segment tree
    def build = {
      for (i <- n - 1 until 0 by -1) {
        t(i) = commutative(t(2 * i), t(2 * i + 1))
      }
    }

    // change value at position p to x
    // TODO beatify
    def modify(p: Int, x: Int) = {
      var pos = p + n
      t(pos) = x
      while (pos > 1) {
        t(pos / 2) = commutative(t(pos), t(pos ^ 1))
        pos /= 2
      }
    }

    // TODO implement me!
    def modify(p: Int, left: Int, right: Int) = ???
    def query(p: Int) = ???

    // sum [l, r)
    // min l = 0
    // max r = n
    // TODO beatify
    def query(left: Int, right: Int): Int = {
      var res = zero
      var r = right + n
      var l = left + n
      while (l < r) {
        if (l % 2 == 1) {
          res = commutative(res, t(l))
          l += 1
        }
        if (r % 2 == 1) {
          r -= 1
          res = commutative(res, t(r))
        }
        l /= 2
        r /= 2
      }
      res
    }
  }

  def solve: Int = {
    val n = nextInt
    var ans = 0.0d
    val values = new Array[Double](n)
    for (i <- 0 until n) {
      values(i) = nextDouble
      ans = Math.max(ans, values(i))
    }
    val sortedValues = values.sorted

    for (i <- 2 to n) {
      var sum = 0.0d
      for (j <- n - 1 to n - i by -1) {
        var partSum = sortedValues(j)
        for (k <- n - 1 to n - i by -1) {
          if (k != j) {
            partSum *= (1.0d - sortedValues(k))
          }
        }
        sum += partSum
      }
      ans = Math.max(ans, sum)
    }
    out.println(ans)
    return 0
  }
}

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

object Main extends App {
  def singleHappensProbability(probabilities: List[Double]): Double = {
    if (probabilities.isEmpty) 0
    else {
      val variousOptions = probabilities.indices map { i =>
        val probabilityPerPerson = probabilities.indices map { j =>
          if (i == j) probabilities(j) else 1 - probabilities(j)
        }

        probabilityPerPerson.product
      }

      variousOptions.sum
    }
  }

  @tailrec
  private def isNextBetter(x: Double, y: Double, rem: List[Double]): Double = {
    if (rem.isEmpty) x
    else {
      val p = rem.head
      val nextX = x * (1-p) + p * y
      if (nextX > x) {
        val nextY = y * (1-p)
        isNextBetter(nextX, nextY, rem.tail)
      } else {
        x
      }
    }
  }

  def solve(probabilities: List[Double]): Double = {
    greedy(probabilities)
  }

  private def greedy(probabilities: List[Double]): Double = {
    val sorted = probabilities.sorted(Ordering.Double.reverse)
    val p0 = sorted.head
    isNextBetter(p0, 1 - p0, sorted.tail)
  }

  private def fullCheck(probabilities: List[Double], acc: List[Double] = Nil): Double = {
    if (probabilities.isEmpty) {
      singleHappensProbability(acc)
    } else {
      Math.max(
        fullCheck(probabilities.tail, acc),
        fullCheck(probabilities.tail, probabilities.head :: acc)
      )
    }
  }

  val n = StdIn.readLine().toInt
  val data = StdIn.readLine().split(" ").map(_.toDouble)
  require(data.length == n, s"Expected $n values but got $data")

  val result = solve(data.toList)

  println(result)
}

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

import scala.collection.mutable

object D {

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

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

  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 nextDouble: Double = return java.lang.Double.parseDouble(next)

  class MultiHashSet[T <% Comparable[T]] {
    val map = new mutable.HashMap[T, Int]()

    def count(x: T): Int = {
      return map.getOrElse(x, 0)
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  class MultiTreeSet[T <% Comparable[T]] {
    val map = new TreeMap[T, Int]()

    def count(x: T): Int = {
      val res = map.get(x)
      if (res == null)
        return 0
      return res
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def first(): T = return map.firstKey()

    def last(): T = return map.lastKey()

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  /**
   * Segment tree for any commutative function
   * @param values Array of Int
   * @param commutative function like min, max, sum
   * @param zero zero value - e.g. 0 for sum, Inf for min, max
   */
  class SegmentTree(values: Array[Int])(commutative: (Int, Int) => Int)(zero: Int) {
    private val SIZE = 1e5.toInt
    private val n = values.length
    val t = new Array[Int](2 * n)
    Array.copy(values, 0, t, n, n)

    // build segment tree
    def build = {
      for (i <- n - 1 until 0 by -1) {
        t(i) = commutative(t(2 * i), t(2 * i + 1))
      }
    }

    // change value at position p to x
    // TODO beatify
    def modify(p: Int, x: Int) = {
      var pos = p + n
      t(pos) = x
      while (pos > 1) {
        t(pos / 2) = commutative(t(pos), t(pos ^ 1))
        pos /= 2
      }
    }

    // TODO implement me!
    def modify(p: Int, left: Int, right: Int) = ???
    def query(p: Int) = ???

    // sum [l, r)
    // min l = 0
    // max r = n
    // TODO beatify
    def query(left: Int, right: Int): Int = {
      var res = zero
      var r = right + n
      var l = left + n
      while (l < r) {
        if (l % 2 == 1) {
          res = commutative(res, t(l))
          l += 1
        }
        if (r % 2 == 1) {
          r -= 1
          res = commutative(res, t(r))
        }
        l /= 2
        r /= 2
      }
      res
    }
  }

  def solve: Int = {
    val n = nextInt
    var ans = 0.0d
    val values = new Array[Double](n)
    for (i <- 0 until n) {
      values(i) = nextDouble
      ans = Math.max(ans, values(i))
    }
    val sortedValues = values.sorted

    for (i <- 2 to n) {
      var sum = 0.0d
      for (j <- n - 1 until n - i by -1) {
        var partSum = sortedValues(j)
        for (k <- n - 1 until n - i by -1) {
          if (k != j) {
            partSum *= (1.0d - sortedValues(k))
          }
        }
        sum += partSum
        ans = Math.max(ans, sum)
      }
    }
    out.println(ans)
    return 0
  }
}

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

import scala.collection.mutable

object D {

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

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

  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 nextDouble: Double = return java.lang.Double.parseDouble(next)

  class MultiHashSet[T <% Comparable[T]] {
    val map = new mutable.HashMap[T, Int]()

    def count(x: T): Int = {
      return map.getOrElse(x, 0)
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  class MultiTreeSet[T <% Comparable[T]] {
    val map = new TreeMap[T, Int]()

    def count(x: T): Int = {
      val res = map.get(x)
      if (res == null)
        return 0
      return res
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def first(): T = return map.firstKey()

    def last(): T = return map.lastKey()

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  /**
   * Segment tree for any commutative function
   * @param values Array of Int
   * @param commutative function like min, max, sum
   * @param zero zero value - e.g. 0 for sum, Inf for min, max
   */
  class SegmentTree(values: Array[Int])(commutative: (Int, Int) => Int)(zero: Int) {
    private val SIZE = 1e5.toInt
    private val n = values.length
    val t = new Array[Int](2 * n)
    Array.copy(values, 0, t, n, n)

    // build segment tree
    def build = {
      for (i <- n - 1 until 0 by -1) {
        t(i) = commutative(t(2 * i), t(2 * i + 1))
      }
    }

    // change value at position p to x
    // TODO beatify
    def modify(p: Int, x: Int) = {
      var pos = p + n
      t(pos) = x
      while (pos > 1) {
        t(pos / 2) = commutative(t(pos), t(pos ^ 1))
        pos /= 2
      }
    }

    // TODO implement me!
    def modify(p: Int, left: Int, right: Int) = ???
    def query(p: Int) = ???

    // sum [l, r)
    // min l = 0
    // max r = n
    // TODO beatify
    def query(left: Int, right: Int): Int = {
      var res = zero
      var r = right + n
      var l = left + n
      while (l < r) {
        if (l % 2 == 1) {
          res = commutative(res, t(l))
          l += 1
        }
        if (r % 2 == 1) {
          r -= 1
          res = commutative(res, t(r))
        }
        l /= 2
        r /= 2
      }
      res
    }
  }

  def solve: Int = {
    val n = nextInt
    var ans = 0.0d
    val values = new Array[Double](n)
    for (i <- 0 until n) {
      values(i) = nextDouble
      ans = Math.max(ans, values(i))
    }
    var sum = 0.0d
    for (i <- 0 until n) {
      var partSum = values(i)
      for (j <- 0 until n) {
        if (j != i) {
          partSum *= (1.0d - values(j))
        }
      }
      sum += partSum
    }
    ans = Math.max(ans, sum)
    out.println(ans)
    return 0
  }
}

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

object Main extends App {
  def singleHappensProbability(probabilities: List[Double]): Double = {
    if (probabilities.isEmpty) 0
    else {
      val variousOptions = probabilities.indices map { i =>
        val probabilityPerPerson = probabilities.indices map { j =>
          if (i == j) probabilities(j) else 1 - probabilities(j)
        }

        probabilityPerPerson.product
      }

      variousOptions.sum
    }
  }

  @tailrec
  def f(x: Double, rem: List[Double]): Double = {
    if (rem.isEmpty) x
    else {
      val y = rem.head
      val next = x + y - 2*x*y
      if (next > x) {
        f(next, rem.tail)
      } else {
        x
      }
    }
  }

  def solve(probabilities: List[Double]): Double = {
    val sorted = probabilities.sorted(Ordering.Double.reverse)
    f(sorted.head, sorted.tail)
  }

  val n = StdIn.readLine().toInt
  val data = StdIn.readLine().split(" ").map(_.toDouble)
  require(data.length == n, s"Expected $n values but got $data")

  val result = solve(data.toList)

  println(result)
}


import io.StdIn._

object Main{
  def main (args: Array[String]){
    val m = readLine.split(' ').map(_.toInt)
    val w = readLine.split(' ').map(_.toInt)

    val Array(hs, hu) = readLine.split(' ').map(_.toInt)

    var sum = 0
    for(i <- 1 to 5){
      // if(m(i-1) > 0){
        sum += Math.max(150*i, (250-m(i-1))*2*i-50*w(i-1)).toInt
      // }
    }

    sum += hs * 100
    sum -= hu * 50

    println(sum)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val data = in.next().split(' ').map(_.toInt)
    .zip(List(500, 1000, 1500, 2000, 2500))
    .zip(in.next().split(' ').map(_.toInt))
    .map {
    case((time, maxScore), trys) => Math.max(maxScore * 3 / 10, maxScore - time * maxScore / 250 - 50 * trys)
  }.sum
  val Array(s, f) = in.next().split(' ').map(_.toInt)
  println(data + 100 * s - 50 * f)

}

object A604 {

  import IO._

  def main(args: Array[String]): Unit = {
    val x = Array(500, 1000, 1500, 2000, 2500)
    val m = readInts(5)
    val w = readInts(5)
    val Array(hs, hu) = readInts(2)

    var score = 0.0d
    score += hs*100
    score -= hu*50
    for(i <- 0 until 5) {
      score += math.max(0.3*x(i), (((250 - m(i))*x(i))/250)-(50*w(i)))
    }

    println(score.toLong)
  }

  object IO {
    @inline def read: String = scala.io.StdIn.readLine

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

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

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

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

}

import scala.collection._

object A extends App {

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

  val xs = Array(500, 1000, 1500, 2000, 2500)
  val ms = readInts(5)
  val ws = readInts(5)
  val ts = readInts(2)

  var res = 0L

  for (i <- 0 until 5) {
    res += Math.max(3 * xs(i) / 10, (xs(i) - xs(i) * ms(i) / 250) - 50 * ws(i))
  }

  res += ts(0) * 100 - ts(1) * 50

  println(res)
}

/**
 * @author pvasilyev
 * @since 14 May 2016
 */
object A604 extends App {

  val x: Array[Int] = Array(500, 1000, 1500, 2000, 2500)
  val m: Array[Int] = scala.io.StdIn.readLine().split(" ").map(_.toInt)
  val w: Array[Int] = scala.io.StdIn.readLine().split(" ").map(_.toInt)
  val Array(hs, hu): Array[Int] = scala.io.StdIn.readLine().split(" ").map(_.toInt)

  // max(0.3x, (1 - m/250)x - 50w)
  // max(0.3x, (250 - m)x/250 - 50w)
  // max(3x/10, [(250 - m)x - 250*50w]/250)
  // 250 * max(25*3x, (250 - m)x - 250*50w)

  val sum: Long = (0 until 5).map(i => 1L * Math.max(25*3*x(i), (250-m(i))*x(i) - 250*50*w(i)) / 250).sum
  println(sum + 100L*hs - 50L*hu)
}

import scala.annotation.tailrec
import scala.collection.mutable
import scala.util._, control._
import scala.math.Ordering.Implicits._

object _604A extends CodeForcesApp {
  override type Result = Int

  override def solve(scanner: Scanner) = {
    import scanner._
    val xs = Seq(500, 1000, 1500, 2000, 2500)
    val ms = Seq.fill(5)(nextInt())
    val ws = Seq.fill(5)(nextInt())

    val t = (xs zip ms zip ws) map {
      case ((x, m), w) =>
        (0.3 * x) max (((250 - m) * (x/250)) - (50 * w)).toDouble
    }

    t.sum.toInt + (100 * nextInt()) + (-50 * nextInt())
  }
}
/****************************[Ignore Template Below]****************************************/
abstract class CodeForcesApp {
  val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: Any): Unit = if (!isOnlineJudge) println(x)
  val mod: Int = 1000000007
  type Result
  def solve(scanner: Scanner): Result
  def format(result: Result): String = result.toString
  def apply(scanner: Scanner): String = format(solve(scanner))
  def main(args: Array[String]): Unit = println(apply(new Scanner(System.in)))
}
/*********************************[Scala Scanner]*******************************************/
import java.io._
import java.nio.file.{Files, Path}
import java.util.StringTokenizer

import scala.io.Codec

/**
 * Scala implementation of a faster java.util.Scanner
 * See: http://codeforces.com/blog/entry/21125
 * All next methods throw NoSuchElementException when !hasNext
 */
class Scanner(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {
  def this(reader: BufferedReader) = this(new LineNumberReader(reader))
  def this(reader: Reader) = this(new BufferedReader(reader))
  def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))
  def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))
  def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)
  def this(str: String) = this(new StringReader(str))

  val lines = Iterator.continually(reader.readLine()).takeWhile(_ != null) //TODO: In Java 8 reader.lines().iterator

  private[this] val tokenizers = lines.map(new StringTokenizer(_)).filter(_.hasMoreTokens)
  private[this] var current: Option[StringTokenizer] = None

  @inline private[this] def tokenizer(): Option[StringTokenizer] = current.find(_.hasMoreTokens) orElse {
    current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
    current
  }

  /**
   * Unlike Java's scanner which returns till end of current line, this actually returns the next line
   * @see line() if you want the Java behaviour
   */
  def nextLine(): String = {
    current = None   // reset
    lines.next()
  }
  def lineNumber: Int = reader.getLineNumber
  def line(): String = tokenizer().get.nextToken("\n\r")
  def nextString(): String = next()
  def nextChar(): Char = next().ensuring(_.length == 1).head
  def nextBoolean(): Boolean = next().toBoolean
  def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)
  def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)
  def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)
  def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)
  def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)
  def nextFloat(): Float = next().toFloat
  def nextDouble(): Double = next().toDouble
  def nextBigDecimal(): BigDecimal = BigDecimal(next())
  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty
  override def close() = reader.close()
}

import scala.io.StdIn._

object A extends App {
  val P = Array(500, 1000, 1500, 2000, 2500)

  val M = readLine().split(" ").map(_.toInt)
  val W = readLine().split(" ").map(_.toInt)
  val Array(hs, hu) = readLine().split(" ").map(_.toInt)

  var total = 0

  for (i <- 0 until 5) {
    val left = 75 * P(i)
    val right = (250 - M(i)) * P(i) - 50 * 250 * W(i)

    val points = if (left > right) left.toInt else right.toInt

    total += points
  }

  total /= 250

  total += 100 * hs - 50 * hu

  println(total)
}

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

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

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

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

  def main(args: Array[String]){

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

    def nextInt = in.nextInt
    def nextLong = in.nextLong
    def nextDouble = in.nextDouble
    def nextString = in.next
    //code starts here
    val x=Array[Double](500.0, 1000.0, 1500.0, 2000.0, 2500.0)
    val m=for(i<-0 until 5) yield nextDouble
    val w=for(i<-0 until 5) yield nextDouble
    val hs=nextInt
    val hu=nextInt
    var sum:Double=0
    for(i<-0 until 5){
      sum+=math.max(0.3*x(i),(1-m(i)/250.0)*x(i)-50*w(i))
    }

    out.println((sum+0.3).toInt+100*hs-50*hu)

    //code ends here
    out.flush
    out.close
  }
}

/*
 * Reference: http://lizan.asia/blog/2012/12/11/scala-competitive/
 */

object Main extends App {
  import java.{util => ju}
  import scala.annotation._
  import scala.collection._
  import scala.collection.{mutable => mu}
  import scala.collection.JavaConverters._
  import scala.math._

  val sc = new ju.Scanner(System.in)

  val m = Array.fill(5)(sc.nextInt)
  val w = Array.fill(5)(sc.nextInt)
  val x = Array.tabulate(5)(i => (i + 1) * 500)
  val hs, hu = sc.nextInt
  var tot = 0
  0 until 5 foreach {
    i => tot += (3 * x(i) / 10) max ((x(i) - x(i) * m(i) / 250) - 50 * w(i))
  }
  println(tot + 100 * hs - 50 * hu)
}

/*
 * Reference: http://lizan.asia/blog/2012/12/11/scala-competitive/
 */

object Main extends App {
  import java.{util => ju}
  import scala.annotation._
  import scala.collection._
  import scala.collection.{mutable => mu}
  import scala.collection.JavaConverters._
  import scala.math._

  val sc = new ju.Scanner(System.in)

  val m = Array.fill(5)(sc.nextInt)
  val w = Array.fill(5)(sc.nextInt)
  val x = Array.tabulate(5)(i => (i + 1) * 500)
  val hs, hu = sc.nextInt
  println((0 until 5).map(i => (3 * x(i) / 10) max (x(i) - x(i) * m(i) / 250 - 50 * w(i))).sum + 100 * hs - 50 * hu)
}

import scala.annotation.tailrec
import scala.collection.mutable
import scala.util._, control._
import scala.math.Ordering.Implicits._

object _604A extends CodeForcesApp {
  override type Result = Int

  override def solve(scanner: Scanner) = {
    import scanner._
    val xs = Seq(500, 1000, 1500, 2000, 2500)
    val ms = Seq.fill(5)(nextInt())
    val ws = Seq.fill(5)(nextInt())

    val t = (xs zip ms zip ws) map {
      case ((x, m), w) =>
        (0.3 * x) max (((250 - m) * (x/250)) - (50 * w)).toDouble
    }

    t.sum.toInt + (100 * nextInt()) + (50 * nextInt())
  }
}
/****************************[Ignore Template Below]****************************************/
abstract class CodeForcesApp {
  val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: Any): Unit = if (!isOnlineJudge) println(x)
  val mod: Int = 1000000007
  type Result
  def solve(scanner: Scanner): Result
  def format(result: Result): String = result.toString
  def apply(scanner: Scanner): String = format(solve(scanner))
  def main(args: Array[String]): Unit = println(apply(new Scanner(System.in)))
}
/*********************************[Scala Scanner]*******************************************/
import java.io._
import java.nio.file.{Files, Path}
import java.util.StringTokenizer

import scala.io.Codec

/**
 * Scala implementation of a faster java.util.Scanner
 * See: http://codeforces.com/blog/entry/21125
 * All next methods throw NoSuchElementException when !hasNext
 */
class Scanner(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {
  def this(reader: BufferedReader) = this(new LineNumberReader(reader))
  def this(reader: Reader) = this(new BufferedReader(reader))
  def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))
  def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))
  def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)
  def this(str: String) = this(new StringReader(str))

  val lines = Iterator.continually(reader.readLine()).takeWhile(_ != null) //TODO: In Java 8 reader.lines().iterator

  private[this] val tokenizers = lines.map(new StringTokenizer(_)).filter(_.hasMoreTokens)
  private[this] var current: Option[StringTokenizer] = None

  @inline private[this] def tokenizer(): Option[StringTokenizer] = current.find(_.hasMoreTokens) orElse {
    current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
    current
  }

  /**
   * Unlike Java's scanner which returns till end of current line, this actually returns the next line
   * @see line() if you want the Java behaviour
   */
  def nextLine(): String = {
    current = None   // reset
    lines.next()
  }
  def lineNumber: Int = reader.getLineNumber
  def line(): String = tokenizer().get.nextToken("\n\r")
  def nextString(): String = next()
  def nextChar(): Char = next().ensuring(_.length == 1).head
  def nextBoolean(): Boolean = next().toBoolean
  def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)
  def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)
  def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)
  def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)
  def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)
  def nextFloat(): Float = next().toFloat
  def nextDouble(): Double = next().toDouble
  def nextBigDecimal(): BigDecimal = BigDecimal(next())
  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty
  override def close() = reader.close()
}

import io.StdIn._

object Main{
  def main (args: Array[String]){
    val m = readLine.split(' ').map(_.toInt)
    val w = readLine.split(' ').map(_.toInt)

    val Array(hs, hu) = readLine.split(' ').map(_.toInt)

    var sum = 0
    for(i <- 1 to 5){
      if(m(i-1) > 0){
        sum += Math.max(150*i, (250-m(i-1))*2*i-50*w(i-1)).toInt
      }
    }

    sum += hs * 100
    sum -= hu * 50

    println(sum)
  }
}


object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private val oj = System.getenv("ATCODER_DEBUG") == null
  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  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 = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from) { i =>
      f(i)
    }
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

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

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer

  // toIntとか+7とかするならvalにしろ
  @inline private def MOD = 1000000007

  def solve(): Unit = {
    REP(ni()) { _ =>
      val N, K = ni()
      val A = na(N)
      var ans = -1
      var fk = 1e9.toInt + 10
      REP(N - K) { i =>
        val x = (A(i) + A(i + K)) / 2
        val y = max(A(i + K) - x, x - A(i))
        if (y < fk) {
          fk = y
          ans = x
        }
      }
      out.println(ans)
    }
  }
}

//package codeforces

/*
https://codeforces.com/contest/1175/problem/C
 */
object Electrification {
  def main(args: Array[String]): Unit = {
    println((1 to io.StdIn.readInt).map { _ =>
      val Array(n, k) = io.StdIn.readLine().split(" ").map(_.toInt)
      val seq = io.StdIn.readLine().split(" ")

      (0 until n - k).map {
        i =>
          val res = (seq(i + k).toInt + seq(i).toInt) / 2
          (res, (seq(i + k).toInt - res) max (res - seq(i).toInt))
      }.minBy(_._2)._1

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


import java.util

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null
  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  @inline private final def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  @inline private final def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }
  private def isDebug[A](debug: => A, online: => A): A = {
    if (oj) online else debug
  }

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

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

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

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

object Workspace {
  import Main._
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer
  import Workspace._

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve(): Unit = {
    val pow = Array.ofDim[Int](2e5.toInt)
    REP(ni()) { _ =>
      val N = ni()
      val A = na(N)

      util.Arrays.fill(pow, 0, N, 0)
      REP(ni()) { _ =>
        val p = ni()
        val s = ni() - 1
        pow(s) = max(pow(s), p)
      }
      REP_r(N - 1) { i =>
        pow(i) = max(pow(i + 1), pow(i))
      }

      debug(pow.take(N))

      var days = 0
      var i = 0
      while(i < N && days != -1) {
        val s = i
        var mx = A(i)
        while (i < N && mx <= pow(i - s)) {
          i += 1
          if (i < N) mx = max(mx, A(i))
        }

        debug(s"i:$i A:$mx")

        if (i == s) days = -1
        else days += 1
      }
      out.println(days)
    }
  }
}

object D1257 {

  import IO._
  import collection.{mutable => cu}
  import scala.collection.mutable.ArrayBuffer

  def main(args: Array[String]): Unit = {
    var t = readInt
    while (t > 0) {
      val n = readInt
      val mons = readInts(n)
      val m = readInt
      // max hero power whose endurance is >= i
      val best = Array.fill(n + 1)(0)
      val heros = Array
        .fill(m)(readInts(2))
      var i = 0
      while (i < m) {
        best(heros(i)(1)) = math.max(best(heros(i)(1)), heros(i)(0))
        i += 1
      }
      i = n - 1
      while (i >= 0) {
        best(i) = math.max(best(i), best(i + 1))
        i -= 1
      }

      if (mons.max > best(1)) {
        out.println(-1)
      } else {
        var day = 0
        var idx = 0
        while (idx < n) {
          var heroPos = 1
          var max = 0
          while (idx < n && math.max(max, mons(idx)) <= best(heroPos)) {
            max = math.max(max, mons(idx))
            heroPos += 1
            idx += 1
          }
          day += 1
        }
        out.println(day)
      }
      t -= 1
    }
    out.close()
  }

  object IO {
    private val input = new java.io.BufferedReader(
      new java.io.InputStreamReader(
        if (System.getProperty("ONLINE_JUDGE") != null) System.in
        else new java.io.FileInputStream("src/input")
      )
    )
    val out = new java.io.PrintWriter(System.out)

    @inline def read: String = input.readLine()

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInt: Int = tokenizeLine.nextToken.toInt

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

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

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

}

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null
  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  @inline def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debugDim(m: Array[Array[Int]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  @inline def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }
  private def isDebug[A](debug: => A, online: => A): A = {
    if (oj) online else debug
  }

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

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

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

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

object Workspace {
  import Main._

  def findMax(f: Int => Boolean, min: Int, max: Int): Int = {
    var l = min
    var h = max + 1
    while(h - l > 1) {
      val x = (h + l) / 2
      if (f(x)) l = x
      else h = x
    }
    l
  }
  case class Hero(power: Int, endurance: Int)
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer
  import Workspace._

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve(): Unit = {
    REP(ni()) { _ =>
      import java.util.Comparator
      val N = ni()
      val A = na(N)
      val heroes = mutable.Map[Int, Hero]()
      REP(ni()) { _ =>
        val p, s = ni()
        if (heroes.contains(s)) {
          if (p > heroes(s).power) {
            heroes(s) = Hero(p, s)
          }
        } else {
          heroes(s) = Hero(p, s)
        }
      }
      val H2 = heroes.toArray.map(_._2)
      // enduranceの昇順はpowerの降順になる
      sort(H2, new Comparator[Hero] {
        override def compare(o1: Hero, o2: Hero): Int = Integer.compare(o1.endurance, o2.endurance)
      })
      val H_buff = ArrayBuffer[Hero]()
      var mxPow = 0
      REP_r(H2.length) { i =>
        if (H2(i).power >= mxPow) H_buff += H2(i)
        mxPow = max(mxPow, H2(i).power)
      }

      val H = H_buff.reverse.toArray
      debug(H.mkString(" "))

      if (H.maxBy(_.power).power < A.max) {
        out.println(-1)
      } else {
        val M = H.length

        var i = 0
        var days = 0
        var j = M - 1

        while (i < N) {
          val s = i
          var continue = true
          while (i < N && continue) {
            def test(ix: Int): Boolean = {
              H(ix).power >= A(i)
            }

            j = findMax(test, 0, j)
            if (H(j).power >= A(i) && H(j).endurance >= i - s + 1) {
              i += 1 // defeat
            } else {
              continue = false // retreat
            }
          }
          assert(i > s)
          days += 1
        }
       out.println(days)
      }
    }
  }
}

object D1257 {

  import IO._
  import collection.{mutable => cu}
  import scala.collection.mutable.ArrayBuffer

  def main(args: Array[String]): Unit = {
    var t = readInt
    while (t > 0) {
      val n = readInt
      val mons = readInts(n)
      val m = readInt
      val heros = Array
        .fill(m)(readInts(2))
        .map(a => (a(0), a(1)))
        .sortBy(-_._1) // sorted by Power, endurance
      if (mons.max > heros.head._1) {
        out.println(-1)
      } else {
        val maxEnduranceAfterThis = {
          val arr = Array.fill(m)(0)
          arr(0) = 0
          var curr = 0
          var i = 0
          while (i < m) {
            if (heros(i)._2 > heros(curr)._2) {
              curr = i
            }
            arr(i) = curr
            i += 1
          }
          arr
        }
        var day = 0
        var i = 0
        while (i < n) {
          var s = 0
          var e = m - 1
          var valid = -1
          while (s <= e) {
            val mid = (s + e) / 2
            if (heros(mid)._1 >= mons(i)) {
              valid = mid
              s = mid + 1
            } else {
              e = mid - 1
            }
          }
          i += 1
          var enduranceLeft = heros(maxEnduranceAfterThis(valid))._2 - 1
          while (i < n && enduranceLeft > 0) {
            if (heros(maxEnduranceAfterThis(valid))._1 >= mons(i)) {
              enduranceLeft -= 1
              i += 1
            } else {
              enduranceLeft = 0
            }
          }
          day += 1
        }
        out.println(day)
      }
      t -= 1
    }
    out.close()
  }

  object IO {
    private val input = new java.io.BufferedReader(
      new java.io.InputStreamReader(
        if (System.getProperty("ONLINE_JUDGE") != null) System.in
        else new java.io.FileInputStream("src/input")
      )
    )
    val out = new java.io.PrintWriter(System.out)

    @inline def read: String = input.readLine()

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInt: Int = tokenizeLine.nextToken.toInt

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

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

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

}


object ProblemA extends App {
  import java.io.{BufferedReader, InputStreamReader, PrintWriter}
  import java.util.StringTokenizer
  val in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)))
  val out = new PrintWriter(System.out)

  class FastScanner(val in: BufferedReader) {
    var st: StringTokenizer = _;

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

    def nextInt(): Integer = {
      Integer.parseInt(nextToken());
    }

    def nextLong(): Long = {
      nextToken().toLong;
    }

    def nextDouble(): Double = {
      nextToken().toDouble;
    }
  }

  val t = in.nextInt()
  for (_ <- 1 to t) {
    val a = in.nextInt()
    val b = in.nextInt()

    var res = (a / 2) * b
    if (a % 2 == 1) {
      res = res + (b / 2) + (if (b % 2 == 1 ) 1 else 0)
    }

    println(res)
  }

}

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

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

    val ans = m / 2 * n + (n * (m - m / 2 * 2) + 1) / 2

    println(ans)
  }
}

import scala.io.StdIn.readLine

object A {

  import scala.math._
  def solution(n: Int, m: Int): Int = {
    val (a, b) = (min(n, m), max(n, m))
    (a / 2) * b + (if (a % 2 == 0) 0 else (b + 1) / 2)
  }

  def main(args: Array[String]): Unit = {
    for {_ <- 0 until readLine.toInt} {
      val Seq(n, k) = readLine.split(" ").toSeq.map(_.toInt)
      println(solution(n, k))
    }
  }
}


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

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

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

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

  def solve(): Unit = {
    val N, M, K = ni()
    val C = nal(N)
    case class Edge(u: Int, v: Int)
    val xorSets = mutable.Map[Long, ArrayBuffer[Edge]]()
    rep(M) { _ =>
      val v, u = ni() - 1
      val x = C(v) ^ C(u)
      if (!xorSets.contains(x)) xorSets(x) = ArrayBuffer()
      xorSets(x) += Edge(v, u)
    }

    def minus(a: Long, b: Long) = (MOD + a - b) % MOD

    var ans = 0L
    ans += powMod(2, N) * minus(powMod(2, K), xorSets.size) % MOD
    xorSets foreach { case (_, es) =>
      val zip = new Zipper()
      es foreach { e =>
        zip(e.u)
        zip(e.v)
      }
      val uf = new UnionFind(zip.length)
      es foreach { e =>
        uf.unite(zip(e.u), zip(e.v))
      }

      ans += powMod(2, N - zip.length + countDisjointSets(uf)) // esに含まれないノードはすべて単独で連結成分になるので disjointSets(V) = |V| - |S| + disjointSets(S)
    }

    ans = ans % MOD

    out.println(ans)
  }

  class Zipper {
    type A = Int
    var gen = 0
    val id = mutable.HashMap[A, Int]()

    def apply(x: A): Int = {
      id.getOrElseUpdate(x, {
        val i = gen
        gen += 1
        i
      })
    }

    def length: Int = gen
  }

  def powMod(x: Int, n: Int): Int = {
    val m = MOD
    def step(x: Long, n: Int, stack: Long): Long = {
      n match {
        case 0 => stack
        case _ => step(x * x % m, n / 2, if (n % 2 == 1) stack * x % m else stack)
      }
    }
    step(x, n, 1).toInt
  }

  class UnionFind(val n: Int) {
    private val par = Array.ofDim[Int](n)
    par.indices foreach (i => par(i) = i)
    private val rank = Array.ofDim[Int](n)

    def find(x: Int): Int = {
      val stack = ListBuffer[Int]()
      def step(x: Int): Int = {
        if (par(x) == x) x
        else {
          stack += x
          step(par(x))
        }
      }

      val res = step(x)
      stack foreach (i => par(i) = res)
      res
    }

    def unite(x: Int, y: Int): Unit = {
      val x1 = find(x)
      val y1 = find(y)
      if (x1 != y1) {
        if (rank(x1) < rank(y)) {
          par(x1) = y1
        } else {
          par(y1) = x1
          if (rank(x1) == rank(y1)) rank(x1) += 1
        }
      }
    }
  }

  def countDisjointSets(uf: UnionFind): Int = {
    var cnt = 0
    rep(uf.n) { i =>
      if (uf.find(i) == i) cnt += 1
    }
    cnt
  }

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N, M, K = ni()
    val C = nal(N)
    case class Edge(u: Int, v: Int)
    val xorSets = mutable.Map[Long, ArrayBuffer[Edge]]()
    rep(M) { _ =>
      val v, u = ni() - 1
      val x = C(v) ^ C(u)
      if (!xorSets.contains(x)) xorSets(x) = ArrayBuffer()
      xorSets(x) += Edge(v, u)
    }

    def minus(a: Long, b: Long) = (MOD + a - b) % MOD

    val pow2 = Array.ofDim[Int](max(N, K) + 1)
    pow2(0) = 1
    rep(pow2.length - 1)(i => pow2(i + 1) = pow2(i) * 2 % MOD)

    var ans = 0L
    ans += pow2(N) * minus(pow2(K), xorSets.size) % MOD
    xorSets foreach { case (_, es) =>
      val zip = new Zipper()
      es foreach { e =>
        zip(e.u)
        zip(e.v)
      }
      val uf = new UnionFind(zip.length)
      es foreach { e =>
        uf.unite(zip(e.u), zip(e.v))
      }

      ans += pow2(N - zip.length + countDisjointSets(uf)) // esに含まれないノードはすべて単独で連結成分になるので disjointSets(V) = |V| - |S| + disjointSets(S)
    }

    ans = ans % MOD

    out.println(ans)
  }

  class Zipper {
    type A = Int
    var gen = 0
    val id = mutable.HashMap[A, Int]()

    def apply(x: A): Int = {
      id.getOrElseUpdate(x, {
        val i = gen
        gen += 1
        i
      })
    }

    def length: Int = gen
  }

  class UnionFind(val n: Int) {
    private val par = Array.ofDim[Int](n)
    par.indices foreach (i => par(i) = i)
    private val rank = Array.ofDim[Int](n)

    def find(x: Int): Int = {
      val stack = ListBuffer[Int]()
      def step(x: Int): Int = {
        if (par(x) == x) x
        else {
          stack += x
          step(par(x))
        }
      }

      val res = step(x)
      stack foreach (i => par(i) = res)
      res
    }

    def unite(x: Int, y: Int): Unit = {
      val x1 = find(x)
      val y1 = find(y)
      if (x1 != y1) {
        if (rank(x1) < rank(y)) {
          par(x1) = y1
        } else {
          par(y1) = x1
          if (rank(x1) == rank(y1)) rank(x1) += 1
        }
      }
    }
  }

  def countDisjointSets(uf: UnionFind): Int = {
    var cnt = 0
    rep(uf.n) { i =>
      if (uf.find(i) == i) cnt += 1
    }
    cnt
  }

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

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

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

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

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

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

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

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

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

  def solve(): Unit = {
    val N, M, K = ni()
    val C = nal(N)
    val xorSets = mutable.Map[Long, mutable.Set[Int]]()
    rep(M) { _ =>
      val v, u = ni() - 1
      val x = C(v) ^ C(u)
      if (!xorSets.contains(x)) xorSets(x) = mutable.Set()
      xorSets(x) ++= Set(v, u)
    }

    def minus(a: Long, b: Long) = (MOD + a - b) % MOD

    var ans = 0L
    ans += powMod(2, N) * minus(powMod(2, K), xorSets.size) % MOD
    xorSets foreach { case (_, set) =>
      ans += powMod(2, N - set.size + 1)
    }

    ans = ans % MOD

    out.println(ans)
  }

  def powMod(x: Int, n: Int): Int = {
    val m = MOD
    def step(x: Long, n: Int, stack: Long): Long = {
      n match {
        case 0 => stack
        case _ => step(x * x % m, n / 2, if (n % 2 == 1) stack * x % m else stack)
      }
    }
    step(x, n, 1).toInt
  }

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

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

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

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

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


object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val data = (1 to n).map{_ =>
    val Array(v, d, p) = in.next().split(" ").map(_.toInt)
    (v, d, p)
  }.toArray
  var result = List.empty[Int]

  def decreaseAll(willCry: List[Int]): Unit = {
    var willCryN = List.empty[Int]
    var dec = data(willCry.head)._2
    var willCryVar = willCry.tail
    (willCry.head + 1 to data.length - 1).foreach {i =>
      if (data(i)._3 >= 0) {
        val newP = data(i)._3 - dec
        data(i) = (data(i)._1, data(i)._2, newP)
        if (newP < 0)
          willCryN ::= i
      }
      if (willCryVar.nonEmpty && willCryVar.head == i) {
        dec += data(willCryVar.head)._2
        willCryVar = willCryVar.tail
      }
    }
    if (willCryN.nonEmpty)
      decreaseAll(willCryN.reverse)
  }

  def decreaseSeq(i: Int) = {
    val (v, d, p) = data(i)
    var j = i + 1
    var j1 = v
    var willCry = List.empty[Int]
    while (j < data.length && j1 > 0) {
      if (data(j)._3 >= 0) {
        val newP = data(j)._3 - j1
        j1 -= 1
        data(j) = (data(j)._1, data(j)._2, newP)
        if (newP < 0)
          willCry ::= j
      }
      j += 1
    }
    if (willCry.nonEmpty)
      decreaseAll(willCry.reverse)
  }

  data.indices.foreach { i =>
    val (v, d, p) = data(i)
    if (p >= 0) {
      decreaseSeq(i)
      result ::= i + 1
    }
  }

  println(result.length)
  println(result.reverse.mkString(" "))
}

import java.io._
import java.util.ArrayList
import scala.collection.mutable.LinkedList

object C_GennadyDentist_b {

  def main(args: Array[String]): Unit = {
    var debugV = false
    def db(x: => String) = if (debugV)  println(x) // nullary function

    class Child(val number:Int, val volume:Int, val volumeHall:Int, var conf:Int) {
      override def toString(): String = {
        return number + "(" + conf + ")"
      }
    }

    val is = System.in

    //COMMENT ME !
//    val is = new ByteArrayInputStream(t3.trim.getBytes("ISO-8859-1"))
//    debugV = true

    val bis = new BufferedInputStream(is);
    val br = new BufferedReader(new InputStreamReader(bis));

    //---------------------------- parameters reading ------------------------------------
    val number = br.readLine().trim().toInt
    var children = List[Child]()
    for (i <- 0 until number) {
      val line = br.readLine().trim().split(" ")
      children = new Child(i + 1, line(0).toInt, line(1).toInt, line(2).toInt) :: children
    }
    //---------------------------- parameters reading end --------------------------------

//    var queue = children.reverse
    var arr = children.reverse.toArray
    var counter = 0
    var resStr = ""
    var ind = 0

    db(printQueue)

//    for (i <- 0 until arr.length) {
    while (ind < arr.length) {
      val ch = arr(ind)
      if (ch != null) {
        db("--- entered " + (ind+1) +"-"+ ch.volume)
        reduceGradualy(ind + 1, ch.volume)
        db("after reduceG: " + printQueue)
        explode(ind + 1)
      }
      ind += 1
    }

    ind = 0
    while (ind < arr.length) {
      val ch = arr(ind)
      if (ch != null) {
        counter += 1
        resStr += (ind+1) + " "
      }
      ind += 1
    }
    println(counter)
    println(resStr)

    def printQueue() = {
      var res = ""
      for (ch <- arr) {
        if (ch != null && ch.number >= (ind+1)) {
          res += ch + " "
        }
      }
      res
    }

    def reduceGradualy(from:Int, volume:Int) {
      var vr = volume
      for (i <- from until arr.length) {
        val ch = arr(i)
        if (ch != null) {
          ch.conf -= vr
          if (ch.conf < -1) ch.conf = -1
          vr -= 1
          if (vr == 0) return
        }
      }
    }

    def explode(from:Int) {
      for (i <- from until arr.length) {
        val ch = arr(i)
        if (ch != null && ch.conf < 0) {
          db("exploded " + (i+1)+"-"+ ch.volumeHall)
          arr(i) = null
          reduce(i+1, ch.volumeHall)
          db("after explosion: " + printQueue)
        }
      }
    }

    def reduce(from:Int, volume:Int) {
      for (i <- from until arr.length) {
        val ch = arr(i)
        if (ch != null) {
          ch.conf -= volume
          if (ch.conf < -1) ch.conf = -1
        }
      }
    }

  }

//========================= samples ========================
val sa1 = """
5
4 2 2
4 1 2
5 2 4
3 3 5
5 1 2
"""

val sa2 = """
5
4 5 1
5 3 9
4 1 2
2 1 8
4 1 9
"""

val t3 = """
10
10 7 10
3 6 11
8 4 10
10 1 11
7 3 13
7 2 13
7 6 14
3 4 17
9 4 20
5 2 24
"""

//========================= samples end ====================
}

import scala.collection.mutable
import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(v: Long, d: Long, var p: Long, var inQueue: Boolean = true)

  val kids = Array.ofDim[Kid](n)
  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  val Q = new mutable.ArrayStack[Integer]()

  for (i <- 0 until n) {
    if (kids(i).inQueue) {
      kids(i).inQueue = false
      var v = kids(i).v
      var j = i + 1
      while (v > 0 && j < n) {
        if (kids(j).inQueue) {
          kids(j).p -= v
          if (kids(j).p < 0) Q.push(j)
          v -= 1
        }
        j += 1
      }

      while (Q.nonEmpty) {
        val curr = Q.pop
        if (kids(curr).inQueue) {
          kids(curr).inQueue = false
          for (j <- curr + 1 until n) {
            if (kids(j).inQueue) {
              kids(j).p -= kids(curr).d
              if (kids(j).p < 0) Q.push(j)
            }
          }
        }
      }

    }

//    println(s"Round $i")
//    println(kids.mkString("\n"))
//    println("----")
  }

  val answer = kids.zipWithIndex.filter(_._1.p >=0).map(_._2 + 1)
  println(answer.length)
  println(answer.mkString(" "))
}

import scala.collection.mutable
import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(v: Long, d: Long, var p: Long, var inQueue: Boolean = true)

  val kids = Array.ofDim[Kid](n)
  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  val Q = new mutable.Queue[Integer]()

  for (i <- 0 until n) {
    if (kids(i).inQueue) {
      kids(i).inQueue = false

      if (kids(i).p >= 0) {
        var v = kids(i).v
        var j = i + 1
        while (v > 0 && j < n) {
          kids(j).p -= v
          if (kids(j).p < 0) Q.enqueue(j)
          v -= 1
          j += 1
        }
      }

      while (Q.nonEmpty) {
        val curr = Q.dequeue()
        if (kids(curr).inQueue) {
          kids(curr).inQueue = false
          for (j <- curr + 1 until n) {
            if (kids(j).inQueue) {
              kids(j).p -= kids(curr).d
              if (kids(j).p < 0) Q.enqueue(j)
            }
          }
        }
      }
    }

//    println(s"Round $i")
//    println(kids.mkString("\n"))
//    println("----")
  }

  val answer = kids.zipWithIndex.filter(_._1.p >=0).map(_._2 + 1)
  println(answer.length)
  println(answer.mkString(" "))
}

import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      var wasChange = true
      var positive = true
      while (wasChange) {
        wasChange = false
        for (j <- i + 1 until n) {
          positive = kids(j).p >= 0
          kids(j).p -= kids(i).d
          if (positive && kids(j).p < 0) wasChange = true
        }
      }
    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        if (kids(curr).p > 0) {
          kids(curr).p -= j
          j -= 1
        }

        curr += 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(var v: Int, var d: Int, var p: Int)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toInt)
    kids(i) = Kid(v, d, p)
  }

  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      for (j <- i + 1 until n) {
        kids(j).p -= kids(i).d
      }
    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j
        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      for (j <- i + 1 until n) {
        kids(j).p -= kids(i).d
      }
    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j
        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).sorted.mkString(" "))

}

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

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  def moveRight(curr: Int): Unit = {
    for (i <- curr + 1 until n) {
      kids(i).p -= kids(curr).d
    }

    for (i <- curr + 1 until n) {
      if (kids(i).p < 0) moveRight(i)
    }
  }

  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      moveRight(i)
    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j
        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

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

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  @tailrec
  def moveRight(c: Int): Unit = {
    var lastLessThanZero = c
    var curr = c

    for (i <- curr + 1 until n) {
      kids(i).p -= kids(curr).d
      if (kids(i).p < 0 && lastLessThanZero == c) lastLessThanZero = i
    }

    if (lastLessThanZero != c) moveRight(lastLessThanZero)
  }

  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      moveRight(i)
    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j
        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

import scala.collection.mutable
import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  val Q = new mutable.Queue[Int]()
  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      Q.enqueue(i)
      while (Q.nonEmpty) {
        val curr = Q.dequeue()
        for (j <- curr + 1 until n) {
          kids(j).p -= kids(curr).d
          if (kids(j).p < 0) Q.enqueue(j)
        }
      }

    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j
        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

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

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  def moveRight(curr: Int): Unit = {
    for (i <- curr + 1 until n) {
      kids(i).p -= kids(curr).d
      if (kids(i).p < 0) {
        moveRight(i)
      }
    }

  }

  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      moveRight(i)
    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j
        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

import scala.collection.mutable
import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(v: Long, d: Long, var p: Long, var inQueue: Boolean = true)

  val kids = Array.ofDim[Kid](n)
  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  for (i <- 0 until n) {
    if (kids(i).inQueue) {
      if (kids(i).p >= 0) {
        kids(i).inQueue = false
        var v = kids(i).v
        var j = i + 1
        while (v > 0 && j < n) {
          if (kids(j).p >= 0 && kids(j).inQueue) {
            kids(j).p -= v
            v -= 1
          }
          j += 1
        }
      } else {
          if (kids(i).inQueue) {
            kids(i).inQueue = false
            for (j <- i + 1 until n) {
              if (kids(j).inQueue) {
                kids(j).p -= kids(i).d
              }
            }
          }

      }

    }

//    println(s"Round $i")
//    println(kids.mkString("\n"))
//    println("----")
  }

  val answer = kids.zipWithIndex.filter(_._1.p >=0).map(_._2 + 1)
  println(answer.length)
  println(answer.mkString(" "))
}

import scala.collection.mutable
import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  val Q = new mutable.Queue[Int]()
  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      Q.enqueue(i)
      while (Q.nonEmpty) {
        val curr = Q.dequeue()
        for (j <- curr + 1 until n) {
          val wasMoreOrEqualZero = kids(j).p >= 0
          kids(j).p -= kids(curr).d
          if (wasMoreOrEqualZero && kids(j).p < 0) Q.enqueue(j)
        }
      }

    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j
        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

import scala.collection.mutable
import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(v: Long, d: Long, var p: Long, var inQueue: Boolean = true)

  val kids = Array.ofDim[Kid](n)
  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  val Q = new mutable.Queue[Integer]()

  for (i <- 0 until n) {
    if (kids(i).inQueue) {
      if (kids(i).p >= 0) {
        kids(i).inQueue = false
        var v = kids(i).v
        var j = i + 1
        while (v > 0 && j < n) {
          kids(j).p -= v
          if (kids(j).p < 0) Q.enqueue(j)
          v -= 1
          j += 1
        }
      }

      while (Q.nonEmpty) {
        val curr = Q.dequeue()
        if (kids(curr).inQueue) {
          kids(curr).inQueue = false
          for (j <- curr + 1 until n) {
            if (kids(j).inQueue) {
              kids(j).p -= kids(curr).d
              if (kids(j).p < 0) Q.enqueue(j)
            }
          }
        }
      }
    }

//    println(s"Round $i")
//    println(kids.mkString("\n"))
//    println("----")
  }

  val answer = kids.zipWithIndex.filter(_._1.p >=0).map(_._2 + 1)
  println(answer.length)
  println(answer.mkString(" "))
}

import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {
      for (j <- i + 1 until n) {
        kids(j).p -= kids(i).d
      }
    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j
        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

import scala.collection.mutable
import scala.io.StdIn._

object C extends App {
  val n = readInt()

  case class Kid(var v: Long, var d: Long, var p: Long)

  val kids = Array.ofDim[Kid](n)

  for (i <- 0 until n) {
    val Array(v, d, p) = readLine().split(" ").map(_.toLong)
    kids(i) = Kid(v, d, p)
  }

  val Q = new mutable.Queue[Int]()
  for (i <- 0 until n - 1) {
    if (kids(i).p < 0) {

    } else {
      var curr = i + 1
      var j = kids(i).v
      while (j > 0 && curr < n) {
        kids(curr).p -= j

        Q.enqueue(curr)
        while (Q.nonEmpty) {
          val c = Q.dequeue()
          for (j <- c + 1 until n) {
            val wasMoreOrEqualZero = kids(j).p >= 0
            kids(j).p -= kids(c).d
            if (wasMoreOrEqualZero && kids(j).p < 0) Q.enqueue(j)
          }
        }

        curr += 1
        j -= 1
      }
    }

//    println(s"After #$i")
//    println(kids.mkString("\n"))
  }

//  println()
//  println()
//  println(kids.mkString("\n"))

  val answer = kids.zipWithIndex.filter { case (kid, id) =>
    kid.p >= 0
  }

  println(answer.length)
  println(answer.map(_._2 + 1).mkString(" "))

}

import java.io._
import java.util.ArrayList
import scala.collection.mutable.LinkedList

object C_GennadyDentist_b {

  def main(args: Array[String]): Unit = {
    var debugV = false
    def db(x: => String) = if (debugV)  println(x) // nullary function

    class Child(val number:Int, val volume:Int, val volumeHall:Int, var conf:Int) {
      override def toString(): String = {
        return number + "(" + conf + ")"
      }
    }

    val is = System.in

    //COMMENT ME !
//    val is = new ByteArrayInputStream(t3.trim.getBytes("ISO-8859-1"))
//    debugV = true

    val bis = new BufferedInputStream(is);
    val br = new BufferedReader(new InputStreamReader(bis));

    //---------------------------- parameters reading ------------------------------------
    val number = br.readLine().trim().toInt
    var children = List[Child]()
    for (i <- 0 until number) {
      val line = br.readLine().trim().split(" ")
      children = new Child(i + 1, line(0).toInt, line(1).toInt, line(2).toInt) :: children
    }
    //---------------------------- parameters reading end --------------------------------

//    var queue = children.reverse
    var arr = children.reverse.toArray
    var counter = 0
    var resStr = ""
    var ind = 0

    db(printQueue)

//    for (i <- 0 until arr.length) {
    while (ind < arr.length) {
      val ch = arr(ind)
      if (ch != null) {
        db("--- entered " + (ind+1) +"-"+ ch.volume)
        reduceGradualy(ind + 1, ch.volume)
        db("after reduceG: " + printQueue)
        explode(ind + 1)
      }
      ind += 1
    }

    ind = 0
    while (ind < arr.length) {
      val ch = arr(ind)
      if (ch != null) {
        counter += 1
        resStr += (ind+1) + " "
      }
      ind += 1
    }
    println(counter)
    println(resStr)

    def printQueue() = {
      var res = ""
      for (ch <- arr) {
        if (ch != null && ch.number >= (ind+1)) {
          res += ch + " "
        }
      }
      res
    }

    def reduceGradualy(from:Int, volume:Int) {
      var vr = volume
      for (i <- from until arr.length) {
        val ch = arr(i)
        if (ch != null) {
          ch.conf -= vr
          vr -= 1
          if (vr == 0) return
        }
      }
    }

    def explode(from:Int) {
      for (i <- from until arr.length) {
        val ch = arr(i)
        if (ch != null && ch.conf < 0) {
          db("exploded " + (i+1)+"-"+ ch.volumeHall)
          arr(i) = null
          reduce(i+1, ch.volumeHall)
          db("after explosion: " + printQueue)
        }
      }
    }

    def reduce(from:Int, volume:Int) {
      for (i <- from until arr.length) {
        val ch = arr(i)
        if (ch != null) ch.conf -= volume
      }
    }

  }

//========================= samples ========================
val sa1 = """
5
4 2 2
4 1 2
5 2 4
3 3 5
5 1 2
"""

val sa2 = """
5
4 5 1
5 3 9
4 1 2
2 1 8
4 1 9
"""

val t3 = """
10
10 7 10
3 6 11
8 4 10
10 1 11
7 3 13
7 2 13
7 6 14
3 4 17
9 4 20
5 2 24
"""

//========================= samples end ====================
}

import java.io._

object C_GennadyDentist {

  def main(args: Array[String]): Unit = {
    var debugV = false;
    def debug(msg:String) = {
      if (debugV) {
        println(msg)
      }
    }
    class Child(val number:Int, val volume:Int, val volumeHall:Int, var conf:Int) {
      var exploded = false

      override def toString(): String = {
        return number + "(" + conf + ")"
      }
    }

    val bis = new BufferedInputStream(System.in);
    val br = new BufferedReader(new InputStreamReader(bis));
    var children = List[Child]()

    val number = br.readLine().trim().toInt
    for (i <- 0 until number) {
     val line = br.readLine().trim().split(" ")
     children = new Child(i+1, line(0).toInt, line(1).toInt, line(2).toInt) :: children
    }

//    val number = 5
//    val strs = """4 2 2
//4 1 2
//5 2 4
//3 3 5
//5 1 2""".split("\n")
//    debugV = true
//    val number = 5
//    val strs = """4 5 1
//5 3 9
//4 1 2
//2 1 8
//4 1 9""".split("\n")
//  for(i <- 0 until strs.length) {
//    var line = strs(i).trim().split(" ")
//    children = new Child(i+1, line(0).toInt, line(1).toInt, line(2).toInt) :: children
//  }

    def reduceFromCab(li:List[Child], vol:Int) {
      if (li == Nil || vol == 0) return
      li.head.conf -= vol
      debug("reduceFromCab " + li.head)
      if (li.head.conf < -1) li.head.conf = -1
      reduceFromCab(li.tail, vol-1)
      if (li.head.conf <= 0 && !li.head.exploded) {
        li.head.exploded = true
        debug("exploded " + li.head)
        reduceExploded(li.tail, li.head.volumeHall)
      }
    }
    def reduceExploded(li:List[Child], vol:Int) {
      if (li == Nil) return
      val ch = li.head
      ch.conf -= vol
      debug("reduceExploded " + ch)
      if (ch.conf < -1) ch.conf = -1
      if (ch.conf <= 0 && !ch.exploded) {
        ch.exploded = true
        debug("exploded secondary " + ch)
        reduceExploded(li.tail, li.head.volumeHall)
      }
      debug("after exploded reduce " + li.mkString(", "))
    }

    var toProcess = children.reverse

    var result = List[Child]()
    while (toProcess != Nil) {
      val ch = toProcess.head
      if (ch.conf > 0) {
        result = ch :: result
        reduceFromCab(toProcess.tail, ch.volume-1)
      } else {

      }
      toProcess = toProcess.tail
      debug(toProcess.mkString(", "))
    }

    println(result.length)
    var resStr = ""
    result.foreach { x => resStr+=x.number + " "}
    println(resStr.reverse.trim())

  }
}

import java.io._

object C_GennadyDentist {

  def main(args: Array[String]): Unit = {
    class Child(val number:Int, val volume:Int, val volumeHall:Int, var conf:Int) {
      var exploded = false

      override def toString(): String = {
        return number + "(" + conf + ")"
      }
    }

    val bis = new BufferedInputStream(System.in);
    val br = new BufferedReader(new InputStreamReader(bis));
    var children = List[Child]()

    val number = br.readLine().trim().toInt
    for (i <- 0 until number) {
     val line = br.readLine().trim().split(" ")
     children = new Child(i+1, line(0).toInt, line(1).toInt, line(2).toInt) :: children
    }

//    val number = 5
//    val strs = """4 2 2
//4 1 2
//5 2 4
//3 3 5
//5 1 2""".split("\n")
//  for(i <- 0 until strs.length) {
//    var line = strs(i).trim().split(" ")
//    children = new Child(i+1, line(0).toInt, line(1).toInt, line(2).toInt) :: children
//  }

    def reduceFromCab(li:List[Child], vol:Int) {
      if (li == Nil || vol == 0) return
      li.head.conf -= vol
//      println("reduceFromCab " + li.head)
      if (li.head.conf < -1) li.head.conf = -1
      reduceFromCab(li.tail, vol-1)
      if (li.head.conf <= 0 && !li.head.exploded) {
        li.head.exploded = true
//        println("exploded " + li.head)
        reduceExploded(li.tail, li.head.volumeHall)
      }
    }
    def reduceExploded(li:List[Child], vol:Int) {
      if (li == Nil) return
      val ch = li.head
      ch.conf -= vol
//      println("reduceExploded " + ch)
      if (ch.conf < -1) ch.conf = -1
      if (ch.conf <= 0 && !ch.exploded) {
        ch.exploded = true
//        println("exploded secondary " + ch)
        reduceExploded(li.tail, li.head.volumeHall)
      }
//      println("after exploded reduce " + li.mkString(", "))
    }

    var toProcess = children.reverse

    var result = List[Child]()
    while (toProcess != Nil) {
      val ch = toProcess.head
      if (ch.conf > 0) {
        result = ch :: result
        reduceFromCab(toProcess.tail, ch.volume)
      } else {

      }
      toProcess = toProcess.tail
//      println(toProcess.mkString(", "))
    }

    println(result.length)
    var resStr = ""
    result.foreach { x => resStr+=x.number + " "}
    println(resStr)

  }
}