stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
//#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
// 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";

}

// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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);

}


// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}

// Fastest known solution for this problem: