#5324. Problem 3. Fenced In

0

Problem 3. Fenced In

Problem 3. Fenced In

USACO 2016 February Contest, Gold

Farmer John has realized that many of his cows are strangely agoraphobic (being fearful of large open spaces). To try and make them less afraid of grazing, he partitions his large field into a number of smaller regions by building vertical (north-south) and horizontal (east-west) fences.

The large field is a rectangle with corner points at (0,0)(0,0) and (A,B)(A,B). FJ builds nn vertical fences (0n20000 \leq n \leq 2000) at distinct locations a1ana_1 \ldots a_n (0<ai<A0 < a_i < A); each fence runs from (ai,0)(a_i, 0) to (ai,B)(a_i, B). He also builds mm horizontal fences (0m20000 \leq m \leq 2000) at locations b1bmb_1 \ldots b_m (0<bi<B0 < b_i < B); each such fence runs from (0,bi)(0, b_i) to (A,bi)(A, b_i). Each vertical fence crosses through each horizontal fence, subdividing the large field into a total of (n+1)(m+1)(n+1)(m+1) regions.

Unfortunately, FJ completely forgot to build gates into his fences, making it impossible for cows to leave their enclosing region and travel around the entire field! He wants to remedy this situation by removing pieces of some of his fences to allow cows to travel between adjacent regions. He wants to select certain pairs of adjacent regions and remove the entire length of fence separating them; afterwards, he wants cows to be able to wander through these openings so they can travel anywhere in his larger field.

For example, FJ might take a fence pattern looking like this:


+---+--+
|   |  |
+---+--+
|   |  |  
|   |  |
+---+--+

and open it up like so:


+---+--+
|      |  
+---+  +  
|      |  
|      |
+---+--+

Please help FJ determine the minimum total length of fencing he must remove to accomplish his goal.

INPUT FORMAT (file fencedin.in):

The first line of input contains AA, BB, nn, and mm (1A,B1,000,000,0001 \leq A, B \leq 1,000,000,000). The next nn lines contain a1ana_1 \ldots a_n, and the next mm lines after that contain b1bmb_1 \ldots b_m.

OUTPUT FORMAT (file fencedin.out):

Please write the minimum length of fencing FJ must remove. Note that this might be too large to fit into a standard 32-bit integer, so you may need to use 64-bit integer types (e.g., "long long" in C/C++).

SAMPLE INPUT:


15 15 5 2
2
5
10
6
4
11
3

SAMPLE OUTPUT:


44

Problem credits: Brian Dean