stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>



using namespace std;



int main()

{

    int n,k,s=0,l=0,sum1=0,sum2=0,start=0,m=0;

    cin>>n>>k;

    int A[n];



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

        cin>>A[i];

    }



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

        if(0<=A[0]){

            l=0;

            m=2*A[n-1];

            break;

        }else if(0>=A[n-1]){

                 l=n;

                 m=-2*A[0];

                 break;

                 }

         else if(i!=0 && i!=n-1&& A[i-1]<0 && 0<A[i]){

            l=i;

            m=-2*A[0] + 2*A[n-1];

            break;

         }

        }



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

                if(i>=l-k && i<l && i+k<=n){

                    sum1=A[i];

                    if(i+k==l){

                        sum2=0;

                    }else{

                        sum2=A[i+k-1];

                    }



                }else if(i==l && i+k<=n){

                    sum1=0;

                    sum2=A[i+k-1];

                    }

                if(m>-2*sum1+sum2){

                    m=-2*sum1+sum2;

                }

                if(m>-1*sum1+2*sum2){

                    m=-1*sum1+2*sum2;

                }



        }

        if(l==n){

            m=-1*A[n-k];

        }

        cout<<m;



}




















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

#include<cstring>

#include<algorithm>

#include<cmath>

#define MN 100005

using namespace std;

int n,ans=0x3f3f3f3f,a[MN],k;

int main(){

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

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

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

		if(1ll*a[l]*a[l+k-1]>=0)ans=min(ans,max(abs(a[l+k-1]),abs(a[l])));

		else ans=min(ans,abs(a[l])+abs(a[l+k-1])+min(abs(a[l]),abs(a[l+k-1])));

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

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

#include <iostream>

#define MN 101000



int a[MN];



int main()

{

	int ans = 1e9;

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

	int s = 0;

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

	{

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

		if(a[i] <= 0) s = i; 

	}

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

	{

		int cnt = s - i + 1;

		int sum = 0 - a[i];

		if(cnt < k)

		{

			if(s + k - cnt > n) continue;

			sum += a[s + k - cnt] - a[i];

		}

		ans = std::min(ans, sum);

	}

	if(s == 0 || a[s] < 0) s++;

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

	{

		int cnt = i - s + 1;

		int sum = a[i] - 0;

		if(cnt < k)

		{

			if(s - (k - cnt) <= 0) continue;

			sum += a[i] - a[s - (k - cnt)];

		}

		ans = std::min(ans, sum);

	}

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

	return 0;

}

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

#include<cstring>

#include<algorithm>

#include<cmath>

#define MN 100005

using namespace std;

int n,ans=0x3f3f3f3f,a[MN],k;

int main(){

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

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

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

		if(1ll*a[l]*a[l+k-1]>=0)ans=min(ans,max(abs(a[l+k-1]),abs(a[l])));

		else ans=min(ans,abs(a[l])+abs(a[l+k-1])+min(abs(a[l]),abs(a[l+k-1])));

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

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

#define ll long long

#define max(a,b) ((a)>(b)?(a):(b))

#define min(a,b) ((a)<(b)?(a):(b))

inline int read()

{

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

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

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

	return x*f;

}

int n,k,a[100005];

int ans;

int main()

{

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

	register int i;

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

	ans=1e9;

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

	{

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

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

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

	}

	return 0*printf("%d\n",ans);

	return 0;

}

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

#include<cstring>

#include<algorithm>

#include<cmath>

#define MN 100005

using namespace std;

int n,ans=0x3f3f3f3f,a[MN],k;

int main(){

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

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

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

		if(1ll*a[l]*a[l+k-1]>=0)ans=min(ans,max(abs(a[l+k-1]),abs(a[l])));

		else ans=min(ans,abs(a[l])+abs(a[l+k-1])+min(abs(a[l]),abs(a[l+k-1])));

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

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

#define ll long long



using namespace std;



ll n,k,a[100010],ans;



int main()

{

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

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

	ans=0x3f3f3f3f;

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

	{

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

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

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

	}

	printf("%lld",ans);

	return 0;

}

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

#include<cstring>

#include<algorithm>

#define inf 0x3f3f3f3f

#define MN 100005

using namespace std;

inline int in(){

    int x=0;bool f=0;char c;

    for (;(c=getchar())<'0'||c>'9';f=c=='-');

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

    return f?-x:x;

}

int a[MN],n,k,pos,res;

int main()

{

	n=in();k=in();res=inf;

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

	pos=lower_bound(a+1,a+n+1,0)-a;

	if (pos+k-1<=n) res=min(res,a[pos+k-1]);

	if (a[pos]){

		if (pos-k>0) res=min(res,-a[pos-k]);

	}else{

		if (pos-k+1>0) res=min(res,-a[pos-k+1]);

	}

	for (int i=max(0,((a[pos])?pos-k:pos-k+1))+1;i<pos&&i+k-1<=n;++i)

	res=min(res,2*(a[i+k-1]-a[i])-max(a[i+k-1],-a[i]));

	printf("%d",res);return 0;

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

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#define int long long

using namespace std;



const int N=100000+50;

int n,m,a[N],ans=1e18;



inline int read(){

	int x=0,f=1;char ch;

	for(ch=getchar();!isdigit(ch);ch=getchar())if(ch=='-')f=-1;

	for(;isdigit(ch);ch=getchar())x=(x<<3)+(x<<1)+ch-'0';

	return x*f;

}



signed main(){

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

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

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

		int j=i+m-1;

		if(a[i]>=0)ans=min(ans,a[j]);

		else if(a[j]<=0)ans=min(ans,-a[i]);

		else ans=min(ans,a[j]-a[i]+min(a[j],-a[i]));

	}

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

}

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

#include<cstring>

#include<algorithm>

#define inf 0x3f3f3f3f

#define MN 100005

using namespace std;

inline int in(){

    int x=0;bool f=0;char c;

    for (;(c=getchar())<'0'||c>'9';f=c=='-');

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

    return f?-x:x;

}

int a[MN],n,k,pos,res;

int main()

{

	n=in();k=in();res=inf;

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

	pos=lower_bound(a+1,a+n+1,0)-a;

	if (pos+k-1<=n) res=min(res,a[pos+k-1]);

	if (a[pos]){

		if (pos-k>0) res=min(res,-a[pos-k]);

	}else{

		if (pos-k+1>0) res=min(res,-a[pos-k+1]);

	}

	for (int i=max(0,((a[pos])?pos-k:pos-k+1))+1;i<pos&&i+k-1<=n;++i)

	res=min(res,2*(a[i+k-1]-a[i])-max(a[i+k-1],-a[i]));

	printf("%d",res);return 0;

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

#define ll long long

using namespace std;

const int INF=0x3f3f3f3f;

ll n,k,a[100010],ans=INF;

int main()

{

    cin>>n>>k;

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

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

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

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

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

    }

    cout<<ans<<endl;

    return 0;

}


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

#include<cstring>

#include<algorithm>

#define inf 0x3f3f3f3f

#define MN 100005

using namespace std;

inline int in(){

    int x=0;bool f=0;char c;

    for (;(c=getchar())<'0'||c>'9';f=c=='-');

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

    return f?-x:x;

}

int a[MN],n,k,pos,res;

int main()

{

	n=in();k=in();res=inf;

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

	pos=lower_bound(a+1,a+n+1,0)-a;

	if (pos+k-1<=n) res=min(res,a[pos+k-1]);

	if (a[pos]){

		if (pos-k>0) res=min(res,-a[pos-k]);

	}else{

		if (pos-k+1>0) res=min(res,-a[pos-k+1]);

	}

	for (int i=max(0,((a[pos])?pos-k:pos-k+1))+1;i<pos&&i+k-1<=n;++i)

	res=min(res,2*(a[i+k-1]-a[i])-max(a[i+k-1],-a[i]));

	printf("%d",res);return 0;

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

using namespace std;

typedef long long ll;

typedef long double ld;

const int MOD = (int)1e9 + 7;

const int MAXN = (int)1e5 + 7;

const int infint = (int)1e9;

ll a[MAXN], n, k;

int main()

{

	ios::sync_with_stdio(false);

	cin.tie(0); cout.tie(0);

	cin >> n >> k;

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

		cin >> a[i];

	ll g = -1;

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

	{

		ll ans = abs(a[i] - 0) + abs(a[i] - a[i + k - 1]);

		ll ans2 = abs(a[i + k - 1] - 0) + abs(a[i] - a[i + k - 1]);

		ans = min(ans, ans2);

		if(g == -1 || ans < g)

			g = ans;

	}

	cout << g;

}

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

#include<cstring>

#include<algorithm>

#define inf 0x3f3f3f3f

#define MN 100005

using namespace std;

inline int in(){

    int x=0;bool f=0;char c;

    for (;(c=getchar())<'0'||c>'9';f=c=='-');

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

    return f?-x:x;

}

int a[MN],n,k,pos,res;

int main()

{

	n=in();k=in();res=inf;

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

	pos=lower_bound(a+1,a+n+1,0)-a;

	if (pos+k-1<=n) res=min(res,a[pos+k-1]);

	if (a[pos]){

		if (pos-k>0) res=min(res,-a[pos-k]);

	}else{

		if (pos-k+1>0) res=min(res,-a[pos-k+1]);

	}

	for (int i=max(0,((a[pos])?pos-k:pos-k+1))+1;i<pos&&i+k-1<=n;++i)

	res=min(res,2*(a[i+k-1]-a[i])-max(a[i+k-1],-a[i]));

	printf("%d",res);return 0;

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



using namespace std;



int main()

{

    int n,k,s=0,l=0,sum1=0,sum2=0,start=0,m=0;

    cin>>n>>k;

    int A[n];



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

        cin>>A[i];

    }



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

        if(0<=A[0]){

            l=0;

            m=2*A[n-1];

            break;

        }else if(0>=A[n-1]){

                 l=n;

                 m=-2*A[0];

                 break;

                 }

         else if(i!=0 && i!=n-1&& A[i-1]<0 && 0<A[i]){

            l=i;

            m=-2*A[0] + 2*A[n-1];

            break;

         }

        }



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

                if(i>=l-k && i<l && i+k<=n){

                    sum1=A[i];

                    if(i+k==l){

                        sum2=0;

                    }else{

                        sum2=A[i+k-1];

                    }



                }else if(i==l && i+k<=n){

                    sum1=0;

                    sum2=A[i+k-1];

                    }

                if(m>-2*sum1+sum2){

                    m=-2*sum1+sum2;

                }

                if(m>-1*sum1+2*sum2){

                    m=-1*sum1+2*sum2;

                }



        }

        if(l==n){

            m=-1*A[n-k];

        }

        cout<<m;



}




















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

#include<cstring>

#include<algorithm>

#define inf 0x3f3f3f3f

#define MN 100005

using namespace std;

inline int in(){

    int x=0;bool f=0;char c;

    for (;(c=getchar())<'0'||c>'9';f=c=='-');

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

    return f?-x:x;

}

int a[MN],n,k,pos,res;

int main()

{

	n=in();k=in();res=inf;

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

	pos=lower_bound(a+1,a+n+1,0)-a;

	if (pos+k-1<=n) res=min(res,a[pos+k-1]);

	if (a[pos]){

		if (pos-k>0) res=min(res,-a[pos-k]);

	}else{

		if (pos-k+1>0) res=min(res,-a[pos-k+1]);

	}

	for (int i=max(0,((a[pos])?pos-k:pos-k+1))+1;i<pos&&i+k-1<=n;++i)

	res=min(res,2*(a[i+k-1]-a[i])-max(a[i+k-1],-a[i]));

	printf("%d",res);return 0;

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

#include <iostream>

#define MN 101000



int a[MN];



int main()

{

	int ans = 1e9;

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

	int s = 0;

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

	{

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

		if(a[i] <= 0) s = i; 

	}

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

	{

		int cnt = s - i + 1;

		int sum = 0 - a[i];

		if(cnt < k)

		{

			if(s + k - cnt > n) continue;

			sum += a[s + k - cnt] - a[i];

		}

		ans = std::min(ans, sum);

	}

	if(s == 0 || a[s] < 0) s++;

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

	{

		int cnt = i - s + 1;

		int sum = a[i] - 0;

		if(cnt < k)

		{

			if(s - (k - cnt) <= 0) continue;

			sum += a[i] - a[s - (k - cnt)];

		}

		ans = std::min(ans, sum);

	}

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

	return 0;

}

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

#include<cstring>

#include<algorithm>

#define inf 0x3f3f3f3f

#define MN 100005

using namespace std;

inline int in(){

    int x=0;bool f=0;char c;

    for (;(c=getchar())<'0'||c>'9';f=c=='-');

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

    return f?-x:x;

}

int a[MN],n,k,pos,res;

int main()

{

	n=in();k=in();res=inf;

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

	pos=lower_bound(a+1,a+n+1,0)-a;

	if (pos+k-1<=n) res=min(res,a[pos+k-1]);

	if (a[pos]){

		if (pos-k>0) res=min(res,-a[pos-k]);

	}else{

		if (pos-k+1>0) res=min(res,-a[pos-k+1]);

	}

	for (int i=max(0,((a[pos])?pos-k:pos-k+1))+1;i<pos&&i+k-1<=n;++i)

	res=min(res,2*(a[i+k-1]-a[i])-max(a[i+k-1],-a[i]));

	printf("%d",res);return 0;

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

#define ll long long

#define max(a,b) ((a)>(b)?(a):(b))

#define min(a,b) ((a)<(b)?(a):(b))

inline int read()

{

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

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

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

	return x*f;

}

int n,k,a[100005];

int ans;

int main()

{

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

	register int i;

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

	ans=1e9;

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

	{

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

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

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

	}

	return 0*printf("%d\n",ans);

	return 0;

}

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

#include<cstring>

#include<algorithm>

#define inf 0x3f3f3f3f

#define MN 100005

using namespace std;

inline int in(){

    int x=0;bool f=0;char c;

    for (;(c=getchar())<'0'||c>'9';f=c=='-');

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

    return f?-x:x;

}

int a[MN],n,k,pos,res;

int main()

{

	n=in();k=in();res=inf;

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

	pos=lower_bound(a+1,a+n+1,0)-a;

	if (pos+k-1<=n) res=min(res,a[pos+k-1]);

	if (a[pos]){

		if (pos-k>0) res=min(res,-a[pos-k]);

	}else{

		if (pos-k+1>0) res=min(res,-a[pos-k+1]);

	}

	for (int i=max(0,((a[pos])?pos-k:pos-k+1))+1;i<pos&&i+k-1<=n;++i)

	res=min(res,2*(a[i+k-1]-a[i])-max(a[i+k-1],-a[i]));

	printf("%d",res);return 0;

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

#define ll long long

using namespace std;

const int N=1e5+100;

const int INF=0x3f3f3f3f;

int n,a[N],k;

int sum;





int main()

{

    cin>>n;

    cin>>k;

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

    {

        cin>>a[i];

    }

    int pos=-1;

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

    {

        if(a[i]>=0)

        {

           pos=i;

           break;

        }

    }

    if(a[1]>=0)

    {

        sum=a[k];

    }

    else

    {

        if(a[n]<=0)

        {

            sum=abs(a[n-k+1]);

        }

        else

        {

            sum=INF;

            for(int i=pos-1;i>=1;i--)

            {

                if(i+k-1<=n)

                {

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

                }

                else

                {

                    continue;

                }

            }

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

            {

                if(i>=k)

                {

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

                }

            }





        }

    }

    cout<<sum<<endl;













    return 0;

}


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

#include <algorithm>

#define MN 100000

int n,k,a[MN+5],ans=0x7fffffff;

int calc(int l,int r){

	if(1ll*a[l]*a[r]>=0) return std::max(std::abs(a[l]),std::abs(a[r]));

	return std::abs(a[l])+std::abs(a[r])+std::min(std::abs(a[l]),std::abs(a[r]));

}

int main(){

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

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

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

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

		ans=std::min(ans,calc(l,l+k-1));

	printf("%d\n",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.1665786182796865x):
#include <stdio.h>

#include <algorithm>

#define MN 100000

int n,k,a[MN+5],ans=0x7fffffff;

int calc(int l,int r){

	if(1ll*a[l]*a[r]>=0) return std::max(std::abs(a[l]),std::abs(a[r]));

	return std::abs(a[l])+std::abs(a[r])+std::min(std::abs(a[l]),std::abs(a[r]));

}

int main(){

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

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

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

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

		ans=std::min(ans,calc(l,l+k-1));

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

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

using namespace std;

int n, k, a[100010], ans = 1e9;

int main()

{

    cin >> n >> k;

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

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

    {

        int l = min(a[i], 0), r = max(a[i + k - 1], 0);

        ans = min(ans, r - l * 2);

        ans = min(ans, r * 2 - l);

    }

    cout << ans << endl;

    return 0;

}

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

#include <algorithm>

#define MN 100000

int n,k,a[MN+5],ans=0x7fffffff;

int calc(int l,int r){

	if(1ll*a[l]*a[r]>=0) return std::max(std::abs(a[l]),std::abs(a[r]));

	return std::abs(a[l])+std::abs(a[r])+std::min(std::abs(a[l]),std::abs(a[r]));

}

int main(){

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

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

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

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

		ans=std::min(ans,calc(l,l+k-1));

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

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

using namespace std;

int x[100010];

int n,k,ans=999999999;

int main(){

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

	for(int i=0;i<n;i++)scanf("%d",&x[i]);

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

		int l=x[i];

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

		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: 5.208633239683381x):
#include <stdio.h>

#include <algorithm>

#define MN 100000

int n,k,a[MN+5],ans=0x7fffffff;

int calc(int l,int r){

	if(1ll*a[l]*a[r]>=0) return std::max(std::abs(a[l]),std::abs(a[r]));

	return std::abs(a[l])+std::abs(a[r])+std::min(std::abs(a[l]),std::abs(a[r]));

}

int main(){

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

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

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

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

		ans=std::min(ans,calc(l,l+k-1));

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

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

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define Would

#define you

#define please



int main() {

	int N,K;

	int x[100000];

	cin >> N >> K;



	rep(i, N) cin >> x[i];



	int minimum = 300000001;

	rep(i, N) {

		if (i + K <= N) {

			int kari = abs(x[i]) + abs(x[i + K - 1] - x[i]);

			minimum = min(kari, minimum);

		}

		if (i - K + 1 >= 0) {

			int kari = abs(x[i]) + abs(x[i - K + 1] - x[i]);

			minimum = min(kari, minimum);

		}

	}

	cout << minimum << "\n";



	Would you please return 0;

}


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

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	int pn = 1;

	if (ci - cn + 16 > cm) {

		if ((ct = getcha()) == '-') { pn = -1; ct = getcha(); }

		A = ct - '0';

		while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';;

		return pn * A;

	}

	else {

		if ((ct = *ci++) == '-') { pn = -1; ct = *ci++; }

		A = ct - '0';

		while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';;

		return pn * A;

	}

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), K = getint();

	int x[100000];

	rep(i, N) x[i] = getint();



	int saishou = 300000001;

	rep(i, N) {

		if (i + K <= N) {

			int kari = abs(x[i]) + abs(x[i + K - 1] - x[i]);

			saishou = min(kari, saishou);

		}

		if (i - K + 1 >= 0) {

			int kari = abs(x[i]) + abs(x[i - K + 1] - x[i]);

			saishou = min(kari, saishou);

		}

	}

	printf("%d", saishou);



	Would you please return 0;

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

#include <iostream>

using namespace std;



int main() {

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

  int x[100001] = {};

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



  int cost = INT_MAX;

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

    int cost1 = abs(x[i]) + abs(x[i+k-1]-x[i]);

    int cost2 = abs(x[i+k-1]) + abs(x[i]-x[i+k-1]);

    cost = min(cost, min(cost1, cost2));

  }

  cout << cost << endl;

}

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

#include <bitset>

#include <cassert>

#include <cmath>

#include <climits>

#include <cstdlib>

#include <iostream>

#include <map>

#include <numeric>

#include <queue>

#include <set>

#include <string>

#include <vector>

#define DEBUG 1

using namespace std;

constexpr int kMod = 1000000007;

typedef long long LL;



template<typename T>

istream& operator >> (istream& is, vector<T>& vs){

  for(T& v: vs) is >> v;

  return is;

}



int main() {

  int N, K; cin >> N >> K;

  vector<int> x(N); cin >> x;

  int vmin = 1e9;

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

    int a = x[i], b = x[i + K - 1];

    if (a < 0 && b < 0) vmin = min(vmin, abs(a));

    if (a > 0 && b > 0) vmin = min(vmin, abs(b));

    else                vmin = min(vmin, abs(a) + abs(b) + min(abs(a), abs(b)));

  }

  cout << vmin << endl;

}