## BZOJ 3210. 花神的浇花集会

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
{
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
return x*f;
}
const int N=2e5+7;
const ll INF=1e18;
int n,x[N],y[N],bx[N],by[N];
ll sx[N],sy[N],ans[2]={INF,INF},Ans=INF;
int main()
{
for(int i=1;i<=n;i++)
{
bx[i]=x[i]=a+b,by[i]=y[i]=a-b;
}
sort(bx+1,bx+n+1); sort(by+1,by+n+1);
for(int i=1;i<=n;i++) sx[i]=sx[i-1]+bx[i];
for(int i=1;i<=n;i++) sy[i]=sy[i-1]+by[i];
for(int i=-100000;i<=200000;i+=2)
{
ll res=0; int p=lower_bound(bx+1,bx+n+1,i)-bx-1;
res+=1ll*p*i-sx[p]; res+=sx[n]-sx[p]-1ll*(n-p)*i;
ans[0]=min(ans[0],res);
}
for(int i=-99999;i<=199999;i+=2)
{
ll res=0; int p=lower_bound(bx+1,bx+n+1,i)-bx-1;
res+=1ll*p*i-sx[p]; res+=sx[n]-sx[p]-1ll*(n-p)*i;
ans[1]=min(ans[1],res);
}
for(int i=-100000;i<=200000;i++)
{
ll res=0; int p=lower_bound(by+1,by+n+1,i)-by-1;
res+=1ll*p*i-sy[p]; res+=sy[n]-sy[p]-1ll*(n-p)*i;
Ans=min(Ans,res+ans[(i%2+2)%2]);
}
printf("%lld\n",Ans/2);
return 0;
}