# [AH2017/HNOI2017] 礼物

## 题目分析：

${Y}_{i}={y}_{n-i+1}$$Y_i=y_{n-i+1}$
$Ans\sum _{i=1}^{n}\left({x}_{i}^{2}+{y}_{i}^{2}\right)-2\ast max\sum _{i=1}^{n}{x}_{i+k}\ast {Y}_{n-i+1}$$Ans\sum_{i=1}^{n}(x_{i}^2+y_{i}^2)-2*max\sum_{i=1}^{n}x_{i+k}*Y_{n-i+1}$

$n\ast {c}^{2}+2\ast c{\sum }_{i=1}^{n}\left({x}_{i}-{y}_{i}\right)$$n*c^2+2*c∑_{i=1}^n(x_i−y_i)$

## Ac 代码：

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
const int maxm=51000*12;
const double PI=std::acos(-1);
struct complex{
double real,imag;
complex(){};
complex(double _real,double _imag):real(_real),imag(_imag){}
};
inline complex operator + (complex x,complex y)
{
return (complex){x.real+y.real,x.imag+y.imag};
}
inline complex operator - (complex x,complex y)
{
return (complex){x.real-y.real,x.imag-y.imag};
}
inline complex operator * (complex x,complex y)
{
return (complex){x.real*y.real-x.imag*y.imag,x.real*y.imag+x.imag*y.real};
}
int rev[maxm];
inline void FFT(complex *a,int n,int f)
{
for(int i=0;i<n;i++) if(i<rev[i]) std::swap(a[i],a[rev[i]]);
for(int i=1;i<n;i<<=1)
{
complex wn=(complex){std::cos(PI/i),f*std::sin(PI/i)};
for(int j=0;j<n;j+=(i<<1))
{
complex w=(complex){1,0};
for(int k=0;k<i;k++,w=(w*wn))
{
complex x=a[j+k],y=a[i+j+k]*w;
a[j+k]=x+y,a[i+j+k]=x-y;
}
}
}
if(f==-1) for(int i=0;i<n;i++) a[i].real/=n;
}
complex A[maxm],B[maxm];
int n,m,c,len;
int main()
{
int suma,sumb,s1,s2;
suma=sumb=s1=s2=0;
scanf("%d%d",&n,&c);
for(int i=0,x;i<n;i++)
{
scanf("%d",&x);
s1+=x*x,suma+=x;
A[i].real=A[i+n].real=(double)x;
}
for(int i=n-1,x;i>=0;i--)
{
scanf("%d",&x);
s2+=x*x,sumb+=x;
B[i].real=(double)x;
}
for(m=1;m<=3*n;m<<=1) len++;
for(int i=0;i<m;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(len-1));
FFT(A,m,1),FFT(B,m,1);
for(int i=0;i<m;i++) A[i]=A[i]*B[i];
FFT(A,m,-1);
int ans=-1e9+7;
for(int i=0;i<n;i++)
ans=std::max((int)round(A[n-1+i].real),ans);
int Ans=1e9+7;
for(int i=-c;i<=c;i++)
Ans=std::min(Ans,s1+s2+n*i*i-2*i*(suma-sumb)-2*ans);
printf("%d\n",Ans);
return 0;
}