stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include "bits/stdc++.h"

using namespace std;



typedef long long ll;

typedef pair<int, int> P;



const int INF = 1000000007;



int main(void){

	int n, k; cin>>n>>k;

	int x[100010] = {0};

	for(int i=0;i<n;i++) cin>>x[i];



	ll minl = INF, minr = INF, tmpl, tmpr;

	for(int i=0;i < n - k + 1;i++) {

		int l = x[i];

		int r = x[i + k - 1];

		tmpl = abs(l) + abs(l - r);

		tmpr = abs(r) + abs(l - r);

		minl = min(minl, tmpl);

		minr = min(minr, tmpr);

	}

	ll ans = min(minl, minr);



	cout<< ans <<endl;

	

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3983183855452395x):
#include "bits/stdc++.h"



using namespace std;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }



using Matrix = vector< vector<int> >;

typedef long long ll;

typedef pair<int, int> P;

const int INF = 1 << 30;

int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};



int main(void){

	int n, k; cin>>n>>k;

	int x[n] = {};

	for(int i=0;i<n;i++) cin>>x[i];



	int mint = INF;

	for(int i=0;i + k - 1 < n;i++) {

		int d = abs(x[i + k - 1] - x[i]) + min(abs(x[i + k - 1]), abs(x[i]));

		mint = min(mint, d);

	}



	cout<< mint <<endl;

	

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



int main()

{

    int n, k;

    cin >> n >> k;

    long long int a[n];

    int d = 1e9;

    for(int i = 0; i < n; i++){

        cin >> a[i];

        if((i != 0 && a[i - 1] < 0 && a[i] >= 0) || (a[i] >= 0 && d == 1e9)){

            d = i;

        }

    }

    long long int ans = 1e18;

    if(d == 0){

        cout << a[k - 1]; return 0;

    }

    if(d >= 1e9){

        cout << -a[n - k]; return 0;

    }

    for(int i = d; i < n; i++){

        int mosbat = i - d + 1;

        if(d - (k - mosbat) >= 0){

            ans = min(ans, 2 * a[i] - a[d - (k - mosbat)]);

        }

    }

    for(int i = d - 1; i >= 0; i--){

        int manfy = d - i;

        if(d + (k - manfy - 1) < n){

            ans = min(ans, (-1) * 2 * a[i] + a[d + (k - manfy - 1)]);

        }

    }

    cout << ans;

}


// Faster C++ solution for the same problem (measured speedup: 5.035348065939435x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
// Ala be zekrellah tatmaenolgholoob ...

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll,ll> pii;

#define F first

#define S second

#define MP make_pair

const int maxn = 1e6+9;

const ll mod = 1e9+7;



ll x[maxn];



int main () {

	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int n, k;

	cin >> n >> k;

	for (int i = 0; i < n; i++)

		cin >> x[i];

	ll res = 1e18;

	int num = 0;

	for (int i = 0; i < n; i++) {

		if (x[i] >= 0)

			num++;

		if (num == k)

			res = x[i];

	}

	num = 0;

	for (int i = n - 1; i >= 0; i--) {

		if (x[i] < 0)

			num++;

		if (num == k)

			res = -x[i];	

	}

	for (int i = 0; i < n; i++) {

		if (x[i] < 0)

			if (i + k - 1 < n)

				res = min(res, -x[i] + abs(x[i + k - 1] - x[i]));

		if (x[i] >= 0)

			if (i - k + 1 >= 0)

				res = min(res, x[i] + abs(x[i] - x[i - k + 1]));

	}

	cout << res << "\n";

}

// Faster C++ solution for the same problem (measured speedup: 5.109112391009321x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
#include <iostream>

using namespace std;

int Left[200005], Right[200005], p[200005];

int main()

{

    int n, k;

    cin >> n >> k;

    int neg = 0, pos = 0, zero = 0, zeroposition;

    for ( int i = 0; i <= n-1; i++ )

    {

        cin >> p[i];

        if ( p[i] < 0 )

        {

            neg++;

        }

        if ( p[i] == 0 )

        {

            zero++;

            zeroposition = i;

        }

        if ( p[i] > 0 )

        {

            pos++;

        }

    }

    if ( k == 1 && zero == 1 )

    {

        cout << 0;

        return 0;

    }

    else if ( zero == 1 )

    {

        k--;

        n--;

        for ( int i = zeroposition; i <= n; i++ )

        {

            p[i] = p[i + 1];

        }

    }

    int leftmost, rightmost, leftleast, rightleast;

    if ( neg < k )

    {

        leftmost = 0;

        rightleast = k-1;

    }

    else

    {

        leftmost = neg-k;

        rightleast = neg;

    }

    if ( pos < k )

    {

        rightmost = n-1;

        leftleast = neg - ( k - pos );

    }

    else

    {

        rightmost = neg+k-1;

        leftleast = neg-1;

    }

    for ( int i = 0; i <= leftleast - leftmost; i++ )

    {

        if ( neg >= k && i == 0 )

        {

            Left[0] = 0 - p[neg - k];

        }

        else

        {

            Left[i] = p[neg + k - (neg - (leftmost + i)) - 1] - p[leftmost + i] * 2;

        }

    }

    for ( int i = 0; i <= rightmost - rightleast; i++ )

    {

        if ( pos >= k && i == 0 )

        {

            Right[0] = p[neg + k - 1];

        }

        else

        {

            Right[i] = p[rightmost - i] * 2 - p[rightmost - k - i + 1];

        }

    }

    int min = 1000000000;

    for ( int i = 0; i <= leftleast - leftmost; i++ )

    {

        if ( Left[i] <= min && Left[i] > 0 )

        {

            min = Left[i];

        }

    }

    for ( int i = 0; i <= rightmost - rightleast; i++ )

    {

        if ( Right[i] <= min && Right[i] > 0 )

        {

            min = Right[i];

        }

    }

    cout << min;

}

// Faster C++ solution for the same problem (measured speedup: 5.026013117397407x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define read(x) scanf("%d",&x)

using namespace std;

typedef long long LL;

const int maxn=1e5+5;

LL a[maxn];

LL sum1[maxn];

LL sum2[maxn];

LL d1[maxn];

LL d2[maxn];

int main() {

    int n,k;

    read(n),read(k);

    LL ans=1e18;

    for(int i=1;i<=n;i++) {

        scanf("%lld",&a[i]);

    }

    stack<LL>s;

    int cnt1=0,cnt2=0;

    for(int i=1;i<=n;i++) {

        if(a[i]<0)s.push(a[i]);

        else d1[++cnt1]=a[i];

    }

    while(!s.empty()) {

        LL t=s.top();

        s.pop();

        d2[++cnt2]=abs(t);

    }

    for(int i=0;i<=k;i++) {

        if(cnt1>=i && cnt2>=k-i) {

            ans=min(ans,min(d1[i]+d1[i]+d2[k-i],d1[i]+d2[k-i]+d2[k-i]));

        }

    }

    printf("%lld\n",ans);

}


// Faster C++ solution for the same problem (measured speedup: 5.019363583272602x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

inline LL read()

{

    LL x=0,f=1;char ch=getchar();

    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}

    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}

    return x*f;

}



int n,k;

const int maxn=1e5+7;

LL a[maxn];

LL b[maxn];

int cntx,cnty;



int main()

{

    n=read();k=read();

    for(int i=1;i<=n;i++){

        a[i]=read();

    }

    LL ans=0x3f3f3f3f3f3f3f3fll;

    for(int i=1;i+k-1<=n;i++){

        int l=i,r=i+k-1;

        ans=min(ans,a[r]-a[l]+min(abs(a[r]),abs(a[l])));

    }

    printf("%lld\n",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.951804477106908x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
#include <iostream>



using namespace std;



int arr[100001];



int calAbsoluteValue(int x)

{

	if(x >= 0)

	{

		return x;

	}

	else

	{

		return -x;

	}

}



int calDis(int x,int y)

{

	if(x > 0 && y > 0)

	{

		return x > y ? x : y;

	}

	else if(x < 0 && y < 0)

	{

		return x < y ? -x : -y;

	}

	else

	{

		int a = calAbsoluteValue(x);

		int b = calAbsoluteValue(y);

		if(a < b)

		{

			return  2 * a + b;

		}

		else

		{

			return 2 * b + a;

		}

	}

}



int main() {



	int N,K;



	cin >> N >> K;



	int nIndex = 0;

	for(int i = 0; i < N; ++i)

	{

		cin >> arr[i];



		if(arr[i] <= 0)

		{

			nIndex = i;

		}

	}



	int nResult = -1;



	if(nIndex <= K)

	{

		if(arr[nIndex] < 0)

		{

			nResult = calDis(arr[0],arr[K - 1]);

			for(int i = 1; i <= nIndex + 1; ++i)

			{

				if(i + K <= N)

				{

					int nTemp = calDis(arr[i],arr[K - 1 + i]);

					if(nTemp < nResult)

					{

						nResult = nTemp;

					}

				}

			}

		}

		else

		{

			nResult = calDis(arr[0],arr[K - 1]);

			for(int i = 1; i <= nIndex; ++i)

			{

				if(i + K <= N)

				{

					int nTemp = calDis(arr[i],arr[K - 1 + i]);

					if(nTemp < nResult)

					{

						nResult = nTemp;

					}

				}

			}

		}





	}

	else

	{

		if(arr[nIndex] < 0)

		{

			nResult = calDis(arr[nIndex + 1 - K],arr[nIndex]);

			for(int i = nIndex - K + 2; i <= nIndex + 1; ++i)

			{

				if(i + K <= N)

				{

					int nTemp = calDis(arr[i],arr[K - 1 + i]);

					if(nTemp < nResult)

					{

						nResult = nTemp;

					}

				}

			}

		}

		else

		{

			nResult = calDis(arr[nIndex + 1 - K],arr[nIndex]);

			for(int i = nIndex - K + 2; i <= nIndex; ++i)

			{

				if(i + K <= N)

				{

					int nTemp = calDis(arr[i],arr[K - 1 + i]);

					if(nTemp < nResult)

					{

						nResult = nTemp;

					}

				}

			}

		}



	}



	cout << nResult << endl;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.0331260024757345x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;



const int N = (int) 1e5;

int n,k;

long long a[N+5],ans=(long long)2e15;

int main()

{



    cin>>n>>k;

    for(int i=1;i<=n;i++)

    cin>>a[i];

     for(int i=1;i+k-1<=n;i++)

    {  

        int l=i,r=i+k-1;

        ans=min(ans,a[r]-a[l]+min(abs(a[l]),abs(a[r])));

    }

    cout<<ans;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.034504028996008x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define rep(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

int n,k;const int maxn=1e+5+5;

int aa[maxn];

int main(){

	scanf("%d%d",&n,&k);

	rep(i,1,n)scanf("%d",aa+i);

	int ans=0x3f3f3f3f;

	rep(i,1,n){

		int j=i+k-1;

		if(j>n)break;

		int x=abs(aa[i]),y=abs(aa[j]);

		if((aa[j]<0)^(aa[i]<0))ans=min(ans,min(x*2+y,x+2*y));

		else ans=min(ans,max(x,y));

	}

	cout<<ans<<endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.025288799159744x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define reg register

typedef long long ll;

using namespace std;

const int MN=1e5+5;

int n,k,a[MN];

int main(){

	scanf("%d%d",&n,&k);reg int ans=INT_MAX;

	for(reg int i=1;i<=n;i++)scanf("%d",a+i);

	for(reg int i=1;i+k-1<=n;i++){

		reg int l=a[i],r=a[i+k-1];

		if(1ll*l*r>0)ans=min(ans,max(abs(l),abs(r)));

		else ans=min(ans,r-l+min(abs(l),abs(r)));

	}

	printf("%d\n",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.966967441758326x):
#include<cstdio>

#include<queue>

#include<cmath>

#include<vector>

#include<algorithm>

using namespace std;

typedef long long ll;

int main()

{

    priority_queue<int,vector<int>,greater<int>> left,right;

    int n;

    int k;

    int x[100005];

    scanf("%d %d",&n,&k);

    for(int i=1;i<=n;++i)

    {

        scanf("%d",&x[i]);

    }

    ll ti=0x3f3f3f3f3f3f;

    for(int i=1;i+k-1<=n;++i)

    {

        int l=i,r=i+k-1;

        ti=min(ti,(ll)x[r]-x[l]+min(abs(x[l]),abs(x[r])));

    }

    printf("%lld\n",ti);

}
// Slower C++ solution:
#include <iostream>

#include <vector>

#include <cstdlib>

long long INF = 1e18;

using namespace std;

int main(){

    int N, K;

    cin >> N >> K;

    vector<long long> a(N);

    for(int i = 0; i < N; i++) cin >> a[i];

    long long ans = INF;

    for(int i = 0; i < N; i++){

        if(i + K - 1 >= N) break;

        if(a[i] < 0 && a[i + K - 1] > 0) ans = min(ans, abs(a[i]) + abs(a[i + K - 1]) + min(abs(a[i]), abs(a[i + K - 1])));

        else ans = min(ans, max(abs(a[i]), abs(a[i + K - 1])));

    }

    cout << ans << endl;

}

// Faster C++ solution for the same problem (measured speedup: 5.286918755767626x):
#include <stdio.h>

#include <stdlib.h>

long long min(long long a, long long b){

    if(a < b) return a;

    else return b;

}

long long max(long long a, long long b){

    if(a < b) return b;

    else return a;

}

long long INF = 1e18;

int main(){

  int N, K;

  scanf("%d %d", &N, &K);

  long long a[N];

  for(int i = 0; i < N; i++) scanf("%lld", &a[i]);

  long long ans = INF;

  for(int i = 0; i < N; i++){

      if(i + K - 1 >= N) break;

      long long b = a[i], c = a[i + K - 1];

      if(b < 0 && c >= 0) ans = min(ans, llabs(c) + llabs(b) + min(llabs(c), llabs(b)));

      else ans = min(ans, max(llabs(b), llabs(c)));

  }

  printf("%lld\n", ans);

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<cstring>

#include<string>

#include<cmath>

#include<algorithm>

#include<vector>

#include<queue>

#include<stack>

#include<map>



using namespace std;

 

#define memset_clear(_) memset(_,0,sizeof(_))

#define memset_clear_tre(_) memset(_,1,sizeof(_))

#define memset_clear_reg(_) memset(_,-1,sizeof(_))

#define memset_clear_max(_) memset(_,0x3f,sizeof(_))

#define memset_clear_min(_) memset(_,0x80,sizeof(_))

#define sqr(_) ((_)*(_))

 

#define int long long

 

const int maxn=100005;

int n,m;

int a[maxn];

 

inline int read(){

	int ret=0,f=1;char ch=getchar();

	while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}

	while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();

	return ret*f;

}

 

signed main(){

	n=read();m=read();

	for (int i=1;i<=n;i++) a[i]=read();

	if (a[n]<0){

		for (int i=1;i<=n/2;i++) a[i]=-a[i],a[n-i+1]=-a[n-i+1],swap(a[i],a[n-i+1]);

	}

	int ans=(long long)1<<60;

	int x=0;

	if (x<a[1]) x=1; else

	if (x>a[n]) x=n; else

	for (int i=1;i<n;i++)

		if (a[i]<x && x<=a[i+1]) {x=i+1;break;}

	for (int i=x;i<=min(n,x+m-1);i++) if (i-m+1>0){

		if (a[i-m+1]<0){

			ans=min(ans,a[i]*2-a[i-m+1]);

			ans=min(ans,a[i]-a[i-m+1]*2);

		} else if (i>=m) ans=min(ans,a[i]);

	}

	if (ans==((int)1<<60)) printf("0\n"); else printf("%lld\n",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.239682366124228x):
#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long ll;

ll Min(ll a,ll b){return a<b?a:b;}

ll Abs(ll a){return a>0?a:-a;}

inline ll read()

{

	ll x=0,f=1;char c=getchar();

	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}

	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}

	return x*f;

}

ll n,k,a[100005],l,ans=9999999999999999;

int main()

{

	n=read();k=read();

	for(ll i=1;i<=n;i++) a[i]=read();

	l=n+1;

	a[l]=1;

	for(ll i=1;i<=n;i++) if(a[i]>=0){l=i;break;}

	if(n-l+1>=k) ans=a[l+k-1];

	for(ll i=l-1,r=n;i>=1;i--)

	{

		if(n-i+1<k) continue;

		if(l-i+(a[l]==0)==k)

		{

			ans=Min(ans,Abs(a[i]));

			break;

		}

		while(r-i+1>k) r--;

		//printf("%d %d %lld\n",i,r,a[r]+Abs(a[i])+Min(Abs(a[i]),a[r]));

		ans=Min(ans,a[r]+Abs(a[i])+Min(Abs(a[i]),a[r]));

	}

	printf("%lld",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h> //Ithea Myse Valgulious

namespace chtholly{

typedef long long ll;

#define re0 register int

#define rel register ll

#define rec register char

#define gc getchar

#define pc putchar

#define p32 pc(' ')

#define pl puts("")

/*By Citrus*/

inline int read(){

  int x=0,f=1;char c=gc();

  for (;!isdigit(c);c=gc()) f^=c=='-';

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return f?x:-x;

  }

template <typename mitsuha>

inline bool read(mitsuha &x){

  x=0;int f=1;char c=gc();

  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';

  if (!~c) return 0;

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return x=f?x:-x,1;

  }

template <typename mitsuha>

inline int write(mitsuha x){

  if (!x) return 0&pc(48);

  if (x<0) pc('-'),x=-x;

  int bit[20],i,p=0;

  for (;x;x/=10) bit[++p]=x%10;

  for (i=p;i;--i) pc(bit[i]+48);

  return 0;

  }

inline char fuhao(){

  char c=gc();

  for (;isspace(c);c=gc());

  return c;

  }

}using namespace chtholly;

using namespace std;

const int yuzu=1e5;

typedef ll fuko[yuzu|10];

fuko a;

int main(){

int i,n=read(),k=read();

for (i=1;i<=n;++i) a[i]=read();

ll llx=1e18;

for (i=k;i<=n;++i) llx=min(llx,min(abs(a[i-k+1]),abs(a[i]))+a[i]-a[i-k+1]);

write(llx);

}

// Faster C++ solution for the same problem (measured speedup: 5.259194609129236x):
#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long ll;

ll Min(ll a,ll b){return a<b?a:b;}

ll Abs(ll a){return a>0?a:-a;}

inline ll read()

{

	ll x=0,f=1;char c=getchar();

	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}

	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}

	return x*f;

}

ll n,k,a[100005],l,ans=9999999999999999;

int main()

{

	n=read();k=read();

	for(ll i=1;i<=n;i++) a[i]=read();

	l=n+1;

	a[l]=1;

	for(ll i=1;i<=n;i++) if(a[i]>=0){l=i;break;}

	if(n-l+1>=k) ans=a[l+k-1];

	for(ll i=l-1,r=n;i>=1;i--)

	{

		if(n-i+1<k) continue;

		if(l-i+(a[l]==0)==k)

		{

			ans=Min(ans,Abs(a[i]));

			break;

		}

		while(r-i+1>k) r--;

		//printf("%d %d %lld\n",i,r,a[r]+Abs(a[i])+Min(Abs(a[i]),a[r]));

		ans=Min(ans,a[r]+Abs(a[i])+Min(Abs(a[i]),a[r]));

	}

	printf("%lld",ans);

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<algorithm>

using namespace std;

int n,m,a[1000000],ans,x,y;

int main(){

        scanf("%d%d",&n,&m);

        for(int i=1;i<=n;i++){

                scanf("%d",&a[i]);

        }

        ans=1000000000;

        for(int i=1;i<=n-m+1;i++){

                x=a[i];

                y=a[i+m-1];

                if(x>0){

                        ans=min(ans,y);

                        continue;

                }

                if(y<0){

                        ans=min(ans,-x);

                        continue;

                }

                ans=min(ans,y-2*x);

                ans=min(ans,-x+2*y);

        }

        printf("%d",ans);

}

// Faster C++ solution for the same problem (measured speedup: 5.25501069089701x):
#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long ll;

ll Min(ll a,ll b){return a<b?a:b;}

ll Abs(ll a){return a>0?a:-a;}

inline ll read()

{

	ll x=0,f=1;char c=getchar();

	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}

	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}

	return x*f;

}

ll n,k,a[100005],l,ans=9999999999999999;

int main()

{

	n=read();k=read();

	for(ll i=1;i<=n;i++) a[i]=read();

	l=n+1;

	a[l]=1;

	for(ll i=1;i<=n;i++) if(a[i]>=0){l=i;break;}

	if(n-l+1>=k) ans=a[l+k-1];

	for(ll i=l-1,r=n;i>=1;i--)

	{

		if(n-i+1<k) continue;

		if(l-i+(a[l]==0)==k)

		{

			ans=Min(ans,Abs(a[i]));

			break;

		}

		while(r-i+1>k) r--;

		//printf("%d %d %lld\n",i,r,a[r]+Abs(a[i])+Min(Abs(a[i]),a[r]));

		ans=Min(ans,a[r]+Abs(a[i])+Min(Abs(a[i]),a[r]));

	}

	printf("%lld",ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define ll long long

using namespace std;



ll int pos[100005],sum[100005];



int main(void)

{

    int n,k;

    cin >>n>>k;

    for(int i=1; i<=n; i++) {

        scanf("%lld", &pos[i]);

        sum[i]=sum[i-1]+pos[i];

    }



    ll int ans=1e17;

    for(int i=1; i<=n-k+1; i++) {

        ans =min(ans, pos[i+k-1]-pos[i]+min(abs(pos[i+k-1]), abs(pos[i])));

    }



    printf("%lld\n", ans);



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.319072340884295x):
#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long ll;

ll Min(ll a,ll b){return a<b?a:b;}

ll Abs(ll a){return a>0?a:-a;}

inline ll read()

{

	ll x=0,f=1;char c=getchar();

	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}

	while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}

	return x*f;

}

ll n,k,a[100005],l,ans=9999999999999999;

int main()

{

	n=read();k=read();

	for(ll i=1;i<=n;i++) a[i]=read();

	l=n+1;

	a[l]=1;

	for(ll i=1;i<=n;i++) if(a[i]>=0){l=i;break;}

	if(n-l+1>=k) ans=a[l+k-1];

	for(ll i=l-1,r=n;i>=1;i--)

	{

		if(n-i+1<k) continue;

		if(l-i+(a[l]==0)==k)

		{

			ans=Min(ans,Abs(a[i]));

			break;

		}

		while(r-i+1>k) r--;

		//printf("%d %d %lld\n",i,r,a[r]+Abs(a[i])+Min(Abs(a[i]),a[r]));

		ans=Min(ans,a[r]+Abs(a[i])+Min(Abs(a[i]),a[r]));

	}

	printf("%lld",ans);

}