Problem:
Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.

Solution in Scala (2.11.7) (judge verdict: Accepted)



import scala.io.StdIn
import scala.reflect.ClassTag


object Main {
  import StdInScanner._
  def main(args: Array[String]): Unit = {
    val Vector(a, b, c, x, y) = scanner.run

    val combs = for {
      cc <- 0 to math.max(2 * x, 2 * y)
      val aa = math.max(0, x - (cc / 2))
      val bb = math.max(0, y - (cc / 2))
    } yield (aa, bb, cc, a * aa + b * bb + c * cc)

    println(combs.minBy(_._4)._4)
  }

  val scanner = readIntTokens
}


case class StdInScanner[A](scan: StdIn.type => A) {
  // transform read result by "f"
  def map[B](f: A => B): StdInScanner[B] =
    StdInScanner(in => f(scan(in)))

  // run this to read A, then obtain new scanner from A using "f", then run the new scanner
  def flatMap[B](f: A => StdInScanner[B]): StdInScanner[B] =
    StdInScanner(in => f(scan(in)).scan(in))

  // run entire scanner and get result
  def run: A = scan(StdIn)
}

object StdInScanner {
  /* pure(read nothing) scanners */
  // read nothing, and return specified "a"
  def apply[A](a: A): StdInScanner[A] = StdInScanner(_ => a)
  // read nothing, and return ()
  def unit: StdInScanner[Unit] = StdInScanner(())

  /* read single line -> produce single value */
  // read a line
  def readALine: StdInScanner[String] = StdInScanner(s => s.readLine().trim)
  // read a line as Int
  def readAnInt: StdInScanner[Int] = StdInScanner(s => s.readInt())
  // read a line as BigInt
  def readALong: StdInScanner[Long] = StdInScanner(s => s.readLong())

  def readAPair[A, B](fa: String => A, fb: String => B): StdInScanner[(A, B)] =
    readTokens.map(v => (fa(v(0)), fb(v(1))))

  def readIntPair: StdInScanner[(Int, Int)] =
    readAPair(_.toInt, _.toInt)

  def readATriple[A, B, C](fa: String => A, fb: String => B, fc: String => C): StdInScanner[(A, B, C)] =
    readTokens.map(v => (fa(v(0)), fb(v(1)), fc(v(2))))

  def readIntTriple: StdInScanner[(Int, Int, Int)] =
    readATriple(_.toInt, _.toInt, _.toInt)

  def readTwoPairs[A, B](fa: String => A, fb: String => B): StdInScanner[((A, B), (A, B))] =
    readTokens.map(v => ((fa(v(0)), fb(v(1))), (fa(v(2)), fb(v(3)))))

  def readTwoIntPairs: StdInScanner[((Int, Int), (Int, Int))] =
    readTwoPairs(_.toInt, _.toInt)

  /* read single line -> produce multiple values */
  // read tokens from a line, then map them with "f"
  def readTokensWithMap[A](f: String => A): StdInScanner[Vector[A]] =
    readALine.map(_.split(" ").toVector.map(f))

  def readTokens: StdInScanner[Vector[String]] =
    readTokensWithMap(identity)

  def readIntTokens: StdInScanner[Vector[Int]] =
    readTokensWithMap(_.toInt)

  def readLongTokens: StdInScanner[Vector[Long]] =
    readTokensWithMap(_.toLong)

  /* read multiple lines -> produce one value per line */
  def readLines(n: Int): StdInScanner[Vector[String]] =
    StdInScanner(s => Vector.fill(n)(s.readLine().trim))

  def readLinesWithMap[A](n: Int, f: String => A): StdInScanner[Vector[A]] =
    StdInScanner(s => Vector.fill(n)(f(s.readLine())))

  def readInts(n: Int): StdInScanner[Vector[Int]] =
    StdInScanner(s => Vector.fill(n)(s.readInt()))

  def readLongs(n: Int): StdInScanner[Vector[Long]] =
    StdInScanner(s => Vector.fill(n)(s.readLong()))

  def readPairs[A, B](n: Int)(fa: String => A, fb: String => B): StdInScanner[Vector[(A, B)]] =
    readLines(n).map(_.map{ line =>
      val t1 :: t2 :: _ = line.split(" ").toList
      (fa(t1), fb(t2))
    })
  def readIntPairs(n: Int): StdInScanner[Vector[(Int, Int)]] =
    readPairs(n)(_.toInt, _.toInt)

  def readLongPairs(n: Int): StdInScanner[Vector[(Long, Long)]] =
    readPairs(n)(_.toLong, _.toLong)

  def readTriples[A, B, C](n: Int)(fa: String => A, fb: String => B, fc: String => C): StdInScanner[Vector[(A, B, C)]] =
    readLines(n).map(_.map{ line =>
      val t1 :: t2 :: t3 :: _ = line.split(" ").toList
      (fa(t1), fb(t2), fc(t3))
    })

  def readIntTriples(n: Int): StdInScanner[Vector[(Int, Int, Int)]] =
    readTriples(n)(_.toInt, _.toInt, _.toInt)

  def readLongTriples(n: Int): StdInScanner[Vector[(Long, Long, Long)]] =
    readTriples(n)(_.toLong, _.toLong, _.toLong)

  /* read multiple lines -> produce multiple value per line */
  // "2D Matrix of A" is Vector of "Vector of A"
  type Matrix[A] = Vector[Vector[A]]

  // read a table of "rows", then map each elements with "f"
  def readTableWithMap[A: ClassTag](rows: Int, f: String => A): StdInScanner[Matrix[A]] =
    readLines(rows).map{ lns =>
      lns.map(ln => ln.split(" ").toVector.map(tkn => f(tkn)))
    }
  def readTable(rows: Int): StdInScanner[Matrix[String]] =
    readTableWithMap(rows, identity)

  def readIntMatrix(rows: Int): StdInScanner[Matrix[Int]] =
    readTableWithMap(rows, _.toInt)

  def readLongMatrix(rows: Int): StdInScanner[Matrix[Long]] =
    readTableWithMap(rows, _.toLong)
}