从两头开始取+在中位数上建巡逻站
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int maxn=1001000;
int n,m;
LL a[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%I64d",a+i);
LL ans=0;
int pos=n/2+1;
for(int i=n;i>=pos;i-=m)
ans+=a[i]-a[pos];
for(int i=1;i<pos;i+=m)
ans+=a[pos]-a[i];
ans*=2;
printf("%I64d\n",ans);
return 0;
}