题意:
思路:
首先可以知道,我们对某个数组加上一个正数数的操作可以转换成对一个数组加上一个任意数,所以我们设变化量为
x
x
x。
对于
∑
i
=
1
n
(
a
i
−
b
i
)
2
\sum_{i=1}^n(a_i-b_i)^2
i=1∑n(ai−bi)2我们将变化量加入变成
∑
i
=
1
n
(
a
i
−
b
i
−
x
)
2
\sum_{i=1}^n(a_i-b_i-x)^2
i=1∑n(ai−bi−x)2考虑大力展开这个式子变成
∑
i
=
1
n
(
a
i
2
+
b
i
2
)
+
n
x
2
+
2
x
∑
i
=
1
n
(
b
i
−
a
i
)
−
2
∑
i
=
1
n
a
i
b
i
\sum_{i=1}^n(a_i^2+b_i^2)+nx^2+2x\sum_{i=1}^n(b_i-a_i)-2\sum_{i=1}^na_ib_i
i=1∑n(ai2+bi2)+nx2+2xi=1∑n(bi−ai)−2i=1∑naibi
可以发现,对于前部分就是一个关于
x
x
x的二次函数,比较容易处理最值的问题,问题就转换成了
∑
i
=
1
n
a
i
b
i
\sum_{i=1}^na_ib_i
∑i=1naibi什么时候最大了。
对于这个问题,考虑其很像卷积的形式,所以套路的将
a
a
a翻转,变成
∑
i
=
1
n
a
n
−
i
+
1
b
i
\sum_{i=1}^na_{n-i+1}b_i
∑i=1nan−i+1bi,由于我们旋转任意一个数组都是等价的,这里选择旋转
b
b
b数组,我们破环成链,将
b
b
b扩展为
[
1
,
2
n
]
[1,2n]
[1,2n],此时设
f
n
+
1
+
k
=
∑
i
=
1
n
a
n
−
i
+
1
b
k
+
i
f_{n+1+k}=\sum_{i=1}^na_{n-i+1}b_{k+i}
fn+1+k=∑i=1nan−i+1bk+i,所以我们直接卷起来,让后取
[
n
+
1
,
2
n
]
[n+1,2n]
[n+1,2n]的最大值即可,最后加上前部分的最小值就是答案啦。
// Problem: P3723 [AH2017/HNOI2017]礼物
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3723
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,m;
int a[N],b[N];
struct FFT {
double PI=acos(-1);
int rev[N];
int bit,limit;
struct Complex {
double x,y;
void init() { x=y=0; }
Complex operator + (const Complex& t) const { return {x+t.x,y+t.y}; }
Complex operator - (const Complex& t) const { return {x-t.x,y-t.y}; }
Complex operator * (const Complex& t) const { return {x*t.x-y*t.y,x*t.y+y*t.x}; }
}a[N];
void init(int n,int m) {
int x=max(n,m)*2; bit=0;
while((1<<bit)<=x) bit++;
limit=1<<bit;
for(int i=0;i<limit;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(bit-1));
}
void fft(Complex a[],int inv) {
for(int i=0;i<limit;i++) if(i<rev[i]) swap(a[i],a[rev[i]]);
for(int mid=1;mid<limit;mid<<=1) {
Complex w1=Complex({cos(PI/mid),inv*sin(PI/mid)});
for(int i=0;i<limit;i+=mid*2) {
Complex wk=Complex({1,0});
for(int j=0;j<mid;j++,wk=wk*w1) {
Complex x=a[i+j],y=wk*a[i+j+mid];
a[i+j]=x+y; a[i+j+mid]=x-y;
}
}
}
if(inv==-1) {
for(int i=0;i<=limit;i++) {
a[i].x/=limit;
a[i].y/=limit;
}
}
}
int solve(int *ans,int *x,int n,int *y,int m) {
for(int i=0;i<=n;i++) a[i].x=x[i];
for(int i=0;i<=m;i++) a[i].y=y[i];
init(n,m);
fft(a,1);
for(int i=0;i<limit;i++) a[i]=a[i]*a[i];
fft(a,-1);
for(int i=0;i<=n+m;i++) ans[i]=(int)(a[i].y/2+0.5);
for(int i=0;i<limit;i++) a[i].init();
return n+m;
}
}FT;
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
scanf("%d%d",&n,&m);
int sum1=0,sum2=0;
for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum1+=a[i]*a[i],sum2-=a[i];
for(int i=1;i<=n;i++) scanf("%d",&b[i]),b[i+n]=b[i],sum1+=b[i]*b[i],sum2+=b[i];
reverse(a+1,a+1+n);
int len=FT.solve(a,a,n,b,2*n);
int mx=0;
for(int i=n+1;i<=n*2+1;i++) mx=max(mx,a[i]);
int ans=INF;
for(int i=-m*2;i<=m*2;i++) ans=min(ans,sum1+n*i*i+2*i*sum2-2*mx);
cout<<ans<<endl;
return 0;
}
/*
*/