#P7090. Lonely Mountain Volume Reconstruction

    ID: 20296 Type: Default 1000ms 256MiB

Lonely Mountain Volume Reconstruction

Lonely Mountain Volume Reconstruction

Inspired by the ancient plan of the Lonely Mountain from Tolkien's classic, you are given two perpendicular projections of a mountain. One projection is viewed from the front and the other from the side. Both projections are representations of the mountain's silhouette on vertical planes. Based on these projections, you are required to calculate the maximum possible volume of the mountain (including any nearby mountains) that is consistent with the given projections.

More formally, you are given two sequences:

  • A front view sequence a_1, a_2, \ldots, a_n.
  • A side view sequence b_1, b_2, \ldots, b_m.

The mountain can be modeled as a 3-dimensional grid of size n by m, where the cell at position (i, j) can have a maximum height of min(ai, bj) (since the actual height cannot exceed what is visible in either projection).

Your task is to compute the maximum volume V achievable under these constraints. That is, you should compute:

[ V ;=; \sum_{i=1}^{n} \sum_{j=1}^{m} \min(a_i,, b_j) ]

where ai is the height in the front view and bj is the height in the side view.

Note: The input sequences represent the maximum heights visible in each respective projection, and the grid cells’ heights can be independently chosen as long as they do not exceed these limits.

inputFormat

The first line contains two space-separated integers n and m [1 ≤ n, m ≤ 1000], representing the number of segments in the front and side views respectively.

The second line contains n space-separated non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 104) representing the heights in the front view.

The third line contains m space-separated non-negative integers b1, b2, ..., bm (0 ≤ bj ≤ 104) representing the heights in the side view.

outputFormat

Output a single integer, the maximum possible volume V of the mountain, calculated as:

[ V ;=; \sum_{i=1}^{n} \sum_{j=1}^{m} \min(a_i,, b_j)]

Print the result on one line.

sample

3 3
1 2 3
3 2 1
14