import scala.annotation.tailrec import scala.collection.mutable import scala.io.StdIn.* @main def main = val Array(n, m, x) = readLine().split(' ').map(_.toInt) val problem = Array.fill(n){ val Array(a, b) = readLine().split(' ').map(_.toInt) (a, b - 1) } val k = readLine().toInt val contestant = readLine().split(' ').map(_.toInt) val sortedWithQuality = (0 until n).sorted(Ordering.by[Int, Int](problem(_)._1).reverse).toList var all = sortedWithQuality var newGenre = sortedWithQuality val useGenre = Array.fill(m){false} val useProblem = Array.fill(n){false} val contestantCount = Array.fill(n + 1){0} for c <- contestant do contestantCount(c) += 1 var rest = k.toLong - contestantCount(0) @tailrec def popNewGenre(rest: List[Int]): List[Int] = rest match case h::t if useGenre(problem(h)._2) => popNewGenre(t) case _ => rest @tailrec def popAll(rest: List[Int]): List[Int] = rest match case h::t if useProblem(h) => popAll(t) case _ => rest var result = 0L for i <- 1 to n do newGenre = popNewGenre(newGenre) all = popAll(all) (newGenre, all) match case (Nil, h::t) => useProblem(h) = true result += problem(h)._1 * rest case (h::t, Nil) => useProblem(h) = true useGenre(problem(h)._2) = true result += (problem(h)._1 + x) * rest case (hi::ti, hj::tj) => if problem(hi)._1 + x < problem(hj)._1 then useProblem(hj) = true result += problem(hj)._1 * rest else useProblem(hi) = true useGenre(problem(hi)._2) = true result += (problem(hi)._1 + x) * rest rest -= contestantCount(i) println(result)