# [POI2008]砖块Klo

//Lib
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
//Macro
#define	rep(i,a,b)	for(int i=a,tt=b;i<=tt;++i)
#define	drep(i,a,b)	for(int i=a,tt=b;i>=tt;--i)
#define	erep(i,e,x)	for(int i=x;i;i=e[i].next)
#define	irep(i,x)	for(__typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define	sqr(x)	((x)*(x))
#define	pb	push_back
#define	PS	system("pause");
typedef	long long	ll;
typedef	pair<int,int>	pii;
const int oo=~0U>>1;
const double inf=1e100;
const double eps=1e-6;
string name="", in=".in", out=".out";
//Var
struct T
{
int LC,RC,FA,SIZE;
ll SUM,KEY;
#define lc(x) tree[x].LC
#define rc(x) tree[x].RC
#define fa(x) tree[x].FA
#define size(x) tree[x].SIZE
#define sum(x) tree[x].SUM
#define key(x) tree[x].KEY
}tree[100008];
int tot,root;
int n,k;
ll ans,num[100008];
void Update(int x)
{
sum(x)=sum(lc(x))+sum(rc(x))+key(x);
size(x)=size(lc(x))+size(rc(x))+1;
}
void Zig(int x)
{
int y=fa(x);
if(lc(fa(y))==y)lc(fa(y))=x;else rc(fa(y))=x;fa(x)=fa(y);
fa(rc(x))=y;lc(y)=rc(x);rc(x)=y;fa(y)=x;
Update(y);
}
void Zag(int x)
{
int y=fa(x);
if(lc(fa(y))==y)lc(fa(y))=x;else rc(fa(y))=x;fa(x)=fa(y);
fa(lc(x))=y;rc(y)=lc(x);lc(x)=y;fa(y)=x;
Update(y);
}
void Splay(int x)
{
for(int y,z;fa(x);)
{
y=fa(x);z=fa(y);
if(!z)
if(lc(y)==x)Zig(x);
else Zag(x);
else
if(lc(z)==y)
if(lc(y)==x)Zig(y),Zig(x);
else Zag(x),Zig(x);
else
if(rc(y)==x)Zag(y),Zag(x);
else Zig(x),Zag(x);
}
root=x;
Update(x);
}
int Find(int k)
{
int p=root;
while(1)
{
if(size(lc(p))+1==k)return p;
else if(size(lc(p))+1>k)p=lc(p);
else k-=size(lc(p))+1,p=rc(p);
}
}
void Ins(ll x)
{
int f=0,p=root,flag=0;
while(1)
{
if(p==0)
{
key(++tot)=x,fa(tot)=f;
if(flag)rc(f)=tot;else lc(f)=tot;
Splay(tot);
break;
}
if(key(p)>=x)f=p,p=lc(p),flag=0;
else f=p,p=rc(p),flag=1;
}
}
void Del(int p)
{
int q;
Splay(p);
if(!lc(p)&&!rc(p)){root=0;return;}
if(lc(p)==0){fa(rc(p))=0;Splay(rc(p));}
else if(rc(p)==0){fa(lc(p))=0;Splay(lc(p));}
else
{
fa(lc(p))=0;
for(q=lc(p);rc(q);q=rc(q));
Splay(q);
fa(rc(p))=q;rc(q)=rc(p);
Update(q);
}
}
ll Calc()
{
int size=size(root)+1>>1;
int p=Find(size);
Splay(p);
return key(p)*size(lc(p))-sum(lc(p))+sum(rc(p))-key(p)*size(rc(p));
}
void Init()
{
ios::sync_with_stdio(0);
cin>>n>>k;
rep(i,1,n)cin>>num[i];
}
void Work()
{
rep(i,1,k)
Ins(num[i]);
ans=Calc();
rep(i,k+1,n)
{
Del(i-k);
Ins(num[i]);
ans=min(ans,Calc());
}
cout<<ans<<endl;
}
int main()
{
//	freopen((name+in).c_str(),"r",stdin);
//	freopen((name+out).c_str(),"w",stdout);
Init();
Work();
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120