思路:
首先数据规模连扫一遍都不可以
但是可以发现是同构的 也就是每一行每一列的间隔长度之类都相同
那就大胆猜想一定是整行整列地开门
用最小生成树的思想 先开代价小的
一些细节问题最好画一下
/**************************************************************
Problem: 4410
User: DtenSherlock
Language: C++
Result: Accepted
Time:116 ms
Memory:1672 kb
****************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
typedef long long LL;
const int imax=25000+229;
const LL inf=1LL<<60;
LL A,B; int n,m;
LL x[imax],y[imax];
void iread()
{
scanf("%lld%lld%d%d",&A,&B,&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",&x[i]); x[++n]=A;
sort(x+1,x+n+1);
for(int i=n;i>=1;i--) x[i]=x[i]-x[i-1];
sort(x+1,x+n+1);
for(int i=1;i<=m;i++) scanf("%lld",&y[i]); y[++m]=B;
sort(y+1,y+m+1);
for(int i=m;i>=1;i--) y[i]=y[i]-y[i-1];
sort(y+1,y+m+1);
}
void iwork()
{
int Max=n*m-1; LL ans=0;
int nowx=1; int nowy=1;
int countx=0; int county=0;
int i=0;
bool havex=0; bool havey=0;
for(i;i<=Max;)
{
if(x[nowx]<y[nowy])
{
if(!havex||!havey){
ans+=x[nowx]*(LL)(m-1);
i+=m-1;
}
else{
ans+=x[nowx]*(LL)(m-countx);
i+=m-countx;
}
havex=1;
county++;
nowx++;
}
else
{
if(!havex||!havey){
ans+=(LL)y[nowy]*(n-1);
i+=n-1;
}
else{
ans+=(LL)y[nowy]*(n-county);
i+=n-county;
}
havey=1;
countx++;
nowy++;
}
}
printf("%lld\n",ans);
}
int main()
{
iread();
iwork();
return 0;
}