Problem:
Score : 500 points

Problem Statement

On an xy plane, in an area satisfying 0 ≤ x ≤ W, 0 ≤ y ≤ H, there is one house at each and every point where both x and y are integers.

There are unpaved roads between every pair of points for which either the x coordinates are equal and the difference between the y coordinates is 1, or the y coordinates are equal and the difference between the x coordinates is 1.

The cost of paving a road between houses on coordinates (i,j) and (i+1,j) is p_i for any value of j, while the cost of paving a road between houses on coordinates (i,j) and (i,j+1) is q_j for any value of i.

Mr. Takahashi wants to pave some of these roads and be able to travel between any two houses on paved roads only. Find the solution with the minimum total cost.

Constraints

1 ≦ W,H ≦ 10^5

1 ≦ p_i ≦ 10^8(0 ≦ i ≦ W-1)

1 ≦ q_j ≦ 10^8(0 ≦ j ≦ H-1)

p_i (0 ≦ i ≦ W−1) is an integer.

q_j (0 ≦ j ≦ H−1) is an integer.

Input

Inputs are provided from Standard Input in the following form.

W H
p_0
:
p_{W-1}
q_0
:
q_{H-1}

Output

Output an integer representing the minimum total cost.

Sample Input 1

2 2
3
5
2
7

Sample Output 1

29

It is enough to pave the following eight roads.

Road connecting houses at (0,0) and (0,1)

Road connecting houses at (0,1) and (1,1)

Road connecting houses at (0,2) and (1,2)

Road connecting houses at (1,0) and (1,1)

Road connecting houses at (1,0) and (2,0)

Road connecting houses at (1,1) and (1,2)

Road connecting houses at (1,2) and (2,2)

Road connecting houses at (2,0) and (2,1)

Sample Input 2

4 3
2
4
8
1
2
9
3

Sample Output 2

60

Solution in Scala (2.11.7) (judge verdict: Time Limit Exceeded)

object Main extends App{
  case class Input(p: Seq[Long], q: Seq[Long])

  def kruskal(p: Seq[Long], q: Seq[Long]): Long = {
    type Node = Option[(Int, Int)]

    case class Edge(n1: Node, n2: Node, cost: Long)
    case class Graph(vs: Set[Node], es: Set[Edge])

    val pn = p.last
    val qn = q.last

    import scala.collection.mutable
    val nodes: Set[Node] =
      (0 until p.size map { i => Option((i, q.size - 1)): Node }).toSet ++
      (0 until p.size map { i => Option((p.size - 1, i)): Node }).toSet +
      None

    val F = mutable.Set[Graph]() ++ (nodes map { n => Graph(Set(n), Set()) })
    val S = mutable.Set[Edge]()
    S ++= p.init.zipWithIndex map {
      case (cost, index) => Edge(Option((index, q.size - 1)), Option((index + 1, q.size - 1)), cost)
    }
    S ++= q.init.zipWithIndex map {
      case (cost, index) => Edge(Option((p.size - 1, index)), Option((p.size - 1, index + 1)), cost)
    }
    S ++= (0 to p.size - 2) map { index => Edge(Option((index, q.size - 1)), None, qn) }
    S ++= (0 to q.size - 2) map { index => Edge(Option((p.size - 1, index)), None, pn) }
    S += Edge(Option((p.size - 1, q.size - 1)), None, Math.min(pn, qn))

    def loop() {
      val edge = S minBy (_.cost)
      S -= edge

      val t1 = F find (_.vs contains edge.n1)
      val t2 = F find (_.vs contains edge.n2)

      (t1, t2) match {
        case (Some(x1), Some(x2)) if x1 != x2 =>
          F -= x1
          F -= x2
          F += Graph((x1.vs ++ x2.vs), x1.es ++ x2.es + edge)
        case _ =>
      }

      if (S.nonEmpty) loop()
    }

    loop()
    (F.toSeq(0).es.toSeq map {_.cost}).sum
  }

  def solve(input: Input): Long = {
    val Input(p, q) = input

    (p.size, q.size) match {
      case (0, _) => q.sum
      case (_, 0) => p.sum
      case _ =>
        val prev = solve(Input(p.init, q.init)) - p.init.sum - q.init.sum
        val b = kruskal(p, q)

        prev + p.sum + q.sum + b
    }
  }

  def parse() = {
    val lines = io.Source.stdin.getLines().toList
    val regex = "(\\d+) (\\d+)".r
    val (w, h) = lines.head match {
      case regex(w, h) => (w.toInt, h.toInt)
    }
    val p = (lines drop 1 take w) map (_.toLong)
    val q = (lines takeRight h) map (_.toLong)

    Input(p, q)
  }

  val input = parse()
  val retval = solve(input)
  println(retval)
}