QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#21170. Recintati

Statistics

Farmer John nota che molte mucche sono stranamente agorafobiche (temono grandi spazi aperti). Per ridurre la paura di pascolare divide il campo con recinti verticali (nord-sud) e orizzontali (est-ovest) in regioni minori.

Il campo è rettangolare con angoli $(0,0)$ e $(A,B)$. FJ costruisce $n$ recinti verticali ($0 \leq n \leq 2000$) in posizioni distinte $a_1 \ldots a_n$ ($0 < a_i < A$), da $(a_i, 0)$ a $(a_i, B)$. Anche $m$ orizzontali ($0 \leq m \leq 2000$) in $b_1 \ldots b_m$ ($0 < b_i < B$), da $(0, b_i)$ a $(A, b_i)$. Ogni verticale incrocia ogni orizzontale, formando $(n+1)(m+1)$ regioni.

Ha dimenticato i cancelli e le mucche non possono lasciare la regione e girare il campo! Rimuoverà parti per collegare regioni adiacenti. Sceglie alcune coppie vicine e rimuove tutto il recinto che le divide, affinché attraverso aperture possano andare ovunque.

Ad esempio questo schema:

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

si può aprire così:

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

Trova la lunghezza totale minima da rimuovere per riuscirci.

Input

La prima riga contiene $A$, $B$, $n$, $m$ ($1 \leq A, B \leq 1,000,000,000$). Le successive $n$ contengono $a_1 \ldots a_n$, poi $m$ righe contengono $b_1 \ldots b_m$.

Output

Stampa la minima lunghezza rimossa. Può superare un intero standard di 32 bit; potresti usare tipi di 64 bit, come long long in C/C++.

Esempi

Input 1

15 15 5 2
2
5
10
6
4
11
3

Output 1

44

Autore: Brian Dean

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.