# 地址

http://codeforces.com/problemset/problem/344/E

# 代码

#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define LL long long
#define lson lo,mi,rt<<1
#define rson mi+1,hi,rt<<1|1
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define dec(i,a,b) for(int i=(a); i>=(b); i--)

using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double ee = exp(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const double pi = acos(-1.0);

{
char c;
int ret = 0,flg = 0;
while(c = getchar(), (c < '0' || c > '9') && c != '-');
if(c == '-') flg = 1; else ret = c ^ 48;
while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
return flg ? - ret : ret;
}

{
char c;
int flg = 0;
LL ret = 0;
while(c = getchar(), (c < '0' || c > '9') && c != '-');
if(c == '-') flg = 1; else ret = c ^ 48;
while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
return flg ? - ret : ret;
}

LL track[maxn];
int n, m;

bool ok(LL t)
{
int cnt = 0;
LL goTime;
rep(i, 0, n - 1)
{
if (t < abs(head[i] - track[cnt]))
continue;
{
cnt++;
}
{
goTime = t;
}
else
{
LL goLeftFirst = t - 2 * (head[i] - track[cnt]);
LL goRightFirst = (t - (head[i] - track[cnt])) / 2;
goTime = Max(goLeftFirst, goRightFirst);
}
while (cnt < m && track[cnt] - head[i] <= goTime)
cnt++;
}
return cnt >= m;
}

int main()
{
#ifdef LOCAL
FIN;
#endif // LOCAL

rep(i, 0, n - 1)
{
}
rep(i, 0, m - 1)
{
}
LL lo = 0, hi = 20000000000000;
while (lo < hi)
{
LL mi = (lo + hi) >> 1;
if (ok(mi))
{
hi = mi;
}
else
{
lo = mi + 1;
}
}
printf("%I64d\n", lo);

return 0;
}