P26397 [AtCoder] Gr-idian MST
提高-
题目描述

Problem Description

In the region of the $xy$ plane where $0 ≦ x ≦ W$ and $0 ≦ y ≦ H$, there is a house at every point where both $x$ and $y$ are integers.

For all pairs of points where the $x$ coordinates are equal and the $y$ coordinates differ by 1, or where the $y$ coordinates are equal and the $x$ coordinates differ by 1, and both points have houses, there is an unpaved road between these two points.

The cost to pave the road between houses at coordinates $(i,j)$ and $(i+1,j)$ is $p_i$, regardless of the value of $j$. The cost to pave the road between houses at coordinates $(i,j)$ and $(i,j+1)$ is $q_j$, regardless of the value of $i$.

Takahashi wants to pave some of these roads so that he can travel between any two houses using only the paved roads. Find the minimum total cost required to achieve this.

Input Format

The input is given from the standard input in the following format:

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

Output Format

Output an integer representing the minimum total cost.

Sample Input and Output

Sample Input #1

2 2
3
5
2
7

Sample Output #1

29

Sample Input #2

4 3
2
4
8
1
2
9
3

Sample Output #2

60

Notes/Hints

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) $ are integers
  • $ q_j (0 ≦ j ≦ H-1) $ are integers

Sample Explanation 1

The following 8 roads should be paved:
- Road between houses at $(0,0)$ and $(0,1)$
- Road between houses at $(0,1)$ and $(1,1)$
- Road between houses at $(0,2)$ and $(1,2)$
- Road between houses at $(1,0)$ and $(1,1)$
- Road between houses at $(1,0)$ and $(2,0)$
- Road between houses at $(1,1)$ and $(1,2)$
- Road between houses at $(1,2)$ and $(2,2)$
- Road between houses at $(2,0)$ and $(2,1)$

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 2 3 5 2 7
样例输出
29