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

#include <cmath>

#include <cstdlib>

#include <cstring>

#include <string>

#include <iostream>

#include <algorithm>

#include <queue>

#include <stack>

#include <deque>

#include <set>

#include <map>

#include <vector>

using namespace std;

const int maxn = 1e5+5;

typedef long long ll;

int a[20][20], c[20], vis[20];

int n, m, x;

int d[20];



void dfs(int u, int &ans) {

    if (u==n+1) {

        memset(d, 0, sizeof(d));

        int sum = 0;

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

            if (vis[i]==1) {

                sum+=c[i];

                for (int j=1; j<=m; j++) {

                    d[j]+=a[i][j];

                }

            }

        }



        for (int j=1; j<=m; j++) {

            if (d[j] < x) {

                return;

            }

        }

        if (ans == -1) {

            ans = sum;

        } else {

            ans = min(ans, sum);

        }

        return;

    }



    vis[u]=0;

    dfs(u+1, ans);

    vis[u]=1;

    dfs(u+1, ans);

}



int main() {

    cin >> n >> m >> x;

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

        cin >> c[i];

        for (int j=1; j<=m; j++) {

            cin >> a[i][j];

        }

    }   



    int ans = -1;

    dfs(1, ans);

    cout << ans << endl;

    return 0;

}

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

#include <algorithm>

using namespace std;

int n,m,x;

int c[15],a[15][15],b[15],d[15],len,sum=100000000,cost;

void dfs(int step){

	int i,j,k;

	if(step==len+1){

		for(k=1;k<=m;k++)d[k]=0;

		for(j=1;j<=len;j++)

			for(k=1;k<=m;k++)

				d[k]+=a[b[j]][k];

		for(k=1;k<=m;k++)

			if(d[k]<x)return;

		cost=0;

		for(j=1;j<=len;j++)cost+=c[b[j]];

		sum=min(sum,cost);

		return ;

	}

	for(i=b[step-1]+1;i<=n;i++)

		b[step]=i,dfs(step+1);

}

int main(){

	int i,j;

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

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

	{

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

		for(j=1;j<=m;j++)

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

	}

	for(len=1;len<=n;len++)

		b[0]=0,dfs(1);

	if(sum==100000000)printf("-1\n");

	else printf("%d\n",sum);

	return 0;

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

#include<cstring>

#include<cstdio> 

#define INF 0x3f3f3f3f

 

using namespace std;

 

const int N=25;

int c[N];

int n,m,x;

int a[N][N];

int ans=INF;

int main()

{

	cin>>n>>m>>x;

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

		cin>>c[i];

		for(int j=1;j<=m;j++){

			cin>>a[i][j];

		}

	}

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

		int sum=0; 

		int res[N]; 

		memset(res,0,sizeof(res));

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

			if(i&(1<<j)){

				for(int k=1;k<=m;k++){

					res[k]+=a[j+1][k];

				}

				sum+=c[j+1];

			}

		}

		bool flag=0;

		for(int j=1;j<=m;j++){

			if(res[j]<x){

				flag=1;

				break;

			}

		}

		if(!flag) ans=min(sum,ans);

	}

	if(ans==INF) puts("-1");

	else cout<<ans<<endl;	

	return 0;

}


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

#include <algorithm>

using namespace std;

int n,m,x;

int c[15],a[15][15],b[15],d[15],len,sum=100000000,cost;

void dfs(int step){

	int i,j,k;

	if(step==len+1){

		for(k=1;k<=m;k++)d[k]=0;

		for(j=1;j<=len;j++)

			for(k=1;k<=m;k++)

				d[k]+=a[b[j]][k];

		for(k=1;k<=m;k++)

			if(d[k]<x)return;

		cost=0;

		for(j=1;j<=len;j++)cost+=c[b[j]];

		sum=min(sum,cost);

		return ;

	}

	for(i=b[step-1]+1;i<=n;i++)

		b[step]=i,dfs(step+1);

}

int main(){

	int i,j;

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

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

	{

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

		for(j=1;j<=m;j++)

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

	}

	for(len=1;len<=n;len++)

		b[0]=0,dfs(1);

	if(sum==100000000)printf("-1\n");

	else printf("%d\n",sum);

	return 0;

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

using namespace std;

#define ll long long

#define lvector vector<ll>

#define lque queue<ll>

#define lpque priority_queue<ll>

#define dlpque priority_queue<ll,lvector,greater<ll>>

#define P pair<ll,ll> 

#define ALL(a) (a).begin(),(a).end()

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

#define print(a) cout << (a) << endl



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  ll n,m,x,ans=1e12,ans_=0;cin>>n>>m>>x;

  lvector price(n,0),understand(m,0);

  vector<lvector> val(n,lvector(m,0));

  rep(i,n) {

    cin>>price[i];

    rep(j,m) cin>>val[i][j];

  }

  for(ll b=0;b<(1<<12);++b) {

    fill(ALL(understand),0);

    ans_=0;

    for(ll book=0;book<n;++book) {

      if(b&(1<<book)) {

        ans_+=price[book];

        rep(i,m) understand[i]+=val[book][i];

      }

    }

    if(*min_element(ALL(understand))>=x) ans=min(ans,ans_);

  }

  print((ans<1e12)?ans:-1);

  return 0;

}

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

using namespace std;

#define ll long long

#define lvector vector<ll>

#define lque queue<ll>

#define lpque priority_queue<ll>

#define dlpque priority_queue<ll,lvector,greater<ll>>

#define P pair<ll,ll> 

#define ALL(a) a.begin(),a.end()

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

#define print(a) cout << (a) << endl



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  ll n,m,x,ans=1e9,ans_;cin>>n>>m>>x;

  lvector price(n,0),understood(m,0);

  vector<lvector> V(n,lvector(m,0));

  rep(i,n) {

    cin>>price[i];

    rep(j,m) cin>>V[i][j];

  }

  for(ll b=0;b<(1<<n);++b) {

    ans_=0;

    fill(ALL(understood),0);

    for(ll t=0;t<n;++t) {

      if(b&(1<<t)) {

        ans_+=price[t];

        rep(i,m) understood[i]+=V[t][i];

      }

    }

    if(*min_element(ALL(understood))>=x) ans=min(ans,ans_);

  }

  print((ans!=1e9)?ans:-1);

  return 0;

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

using namespace std;

typedef long long ll;

const int mod = 1000000007;



void DFS(int& ans, int i, int n, int m, int x, vector<int>& A, const vector<vector<int>> a)

{

	if (A.size() == i) {

		vector<int> algorithm(m + 1);

		int money = 0;

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

			money += a[A[j]][0];

			for (int k = 1; k <= m; k++) {

				algorithm[k] += a[A[j]][k];

			}

		}

		for (int j = 1; j <= m; j++) {

			if (algorithm[j] < x) return;

		}

		ans = min(ans, money);

		return;

	}



	int start = A.empty() ? 0 : A.back();

	for (int j = start; j < n; j++) {

		A.push_back(j + 1);

		DFS(ans, i, n, m, x, A, a);

		A.pop_back();

	}

}



int main()

{

	int n, m, x;

	cin >> n >> m >> x;



	vector<vector<int>> a(n + 1, vector<int>(m + 1));

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

		for (int j = 0; j < m + 1; j++) cin >> a[i][j];

	}



	int ans = INT_MAX;

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

		vector<int> A;

		DFS(ans, i, n, m, x, A, a);

	}

	if (ans == INT_MAX) cout << -1 << endl;

	else cout << ans << endl;

}


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

using namespace std;

typedef long long ll;



int main()

{

	int n, m, x;

	cin >> n >> m >> x;

	vector<vector<int>> a(n, vector<int>(m + 1));

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

		for (int j = 0; j < m + 1; j++) cin >> a[i][j];

	}



	int ans = INT_MAX;

	for (int bit = 0; bit < (2 << n); bit++) {

		vector<int> algorithm(m);

		int money = 0;

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

			if (bit & (1 << i)) {

				money += a[i][0];

				for (int j = 1; j <= m; j++) algorithm[j - 1] += a[i][j];

			}

		}

		int flag = true;

		for (const auto& item : algorithm) {

			if (item < x) {

				flag = false;

				break;

			}

		}

		if (flag) ans = min(ans, money);

	}



	if (ans == INT_MAX) cout << -1 << endl;

	else cout << ans << endl;

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

using namespace std;

int main()

{

    int w, h, n, a, b, c, d;

    cin>>w>>h>>n;

    a =0, b = w, c =0, d = h;

    while( n--)

    {

        int x, y, m;

        cin>>x>>y>>m;

        if( m ==1) a = max( a, x );

        if( m ==2) b = min( b, x );

        if( m ==3) c = max( c, y );

        if( m ==4) d = min( d, y );

    }

    w =(b-a<0?0:b-a);

    h =(d-c<0?0:d-c);

    cout<<w*h<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.209567295905081x):
// sol1

#include<stdio.h>



int a[1001][1001];

int w, h, x, y, t, n;



void f(int x, int y, int mode)

{

    if(mode==1)

    {

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

            for(int j=1; j<=x; j++)

                a[i][j]=1;

    }

    else if(mode==2)

    {

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

            for(int j=x+1;j<=w; j++)

                a[i][j]=1;

    }

    

    else if(mode==3)

    {

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

            for(int j=1; j<=w; j++)

                a[i][j]=1;

    }

    

    else{

        for(int i=y+1; i<=h; i++)

            for(int j=1; j<=w; j++)

                a[i][j]=1;

    }

}



int main()

{

    scanf("%d %d %d",&w, &h, &n);

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

    {

        scanf("%d %d %d",&x, &y, &t);

        f(x, y, t);

    }

    int cnt=0;

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

        for(int j=1; j<=w; j++)

            if(a[i][j]==0)

                cnt++;

    printf("%d",cnt);

}





// sol2

/*

#include<iostream>

using namespace std;

int main()

{

    int w, h, n, a, b, c, d;

    cin>>w>>h>>n;

    a =0, b = w, c =0, d = h;

    while( n--)

    {

        int x, y, m;

        cin>>x>>y>>m;

        if( m ==1) a = max( a, x );

        if( m ==2) b = min( b, x );

        if( m ==3) c = max( c, y );

        if( m ==4) d = min( d, y );

    }

    w =(b-a<0?0:b-a);

    h =(d-c<0?0:d-c);

    cout<<w*h<<endl;

    return 0;

}

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



using namespace std;

int main(int argc, char *argv[]) {

	int board[1200][1200];

	int W = 0;

	int H = 0;

	int N = 0;

	int X = 0;

	int Y = 0;

	int choice = 0;

	cin>>W>>H>>N;

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

	{

		for(int j=0;j<=1000;j++)

		{

			board[i][j] = 0;

			

		}

	}

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

	{

		for(int j=1;j<=W;j++)

		{

			board[i][j] = 1;

			

		}

	}

	for(int repeat = 1;repeat<=N;repeat++)

	{

		cin>>X>>Y>>choice;

		if(choice == 1)

		{

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

			{

				for(int j=1;j<=X;j++)

				{

					board[i][j] += 1;

					

				}

			}

		}

		if(choice == 2)

		{

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

			{

				for(int j=W;j>X;j--)

				{

					board[i][j] += 1;

					

				}

			}

		}

		if(choice == 3)

		{

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

			{

				for(int j=1;j<=W;j++)

				{

					board[i][j] += 1;

					

				}

			}

		}

		if(choice == 4)

		{

			for(int i=H;i>Y;i--)

			{

				for(int j=1;j<=W;j++)

				{

					board[i][j] += 1;

					

				}

			}

		}

	}

	

	

	

	

	int amount = 0;

	

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

	{

		for(int j=1;j<=W;j++)

		{

			if(board[i][j] == 1)

			{

				amount += 1;

			}

			

		}

	}

	cout<<amount;

	

}

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

using namespace std;

int main()

{

	int w,h,n;

	cin>>w>>h>>n;

	int x[n],y[n],a[n],b[w][h];

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

	{

		cin>>x[i]>>y[i]>>a[i];

	}

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

	{

		for(int j=0;j<h;j++)

		{

			b[i][j]=0;

		}

	}

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

	{

		if(a[i]==1)

		{

			for(int j=0;j<x[i];j++)

			{

				for(int l=0;l<h;l++)

				{

					b[j][l]=1;

				}

			}

		}

		if(a[i]==2)

		{

			for(int j=w-1;j>=x[i];j--)

			{

				for(int l=0;l<h;l++)

				{

					b[j][l]=1;

				}

			}

		}

		if(a[i]==3)

		{

			for(int j=0;j<y[i];j++)

			{

				for(int l=0;l<w;l++)

				{

					b[l][j]=1;

				}

			}

		}

		if(a[i]==4)

		{

			for(int j=h-1;j>=y[i];j--)

			{

				for(int l=0;l<w;l++)

				{

					b[l][j]=1;

				}

			}

		}

	}

	int ans=0;

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

	{

		for(int j=0;j<h;j++)

		{

			if(b[i][j]==0)

			{

				ans++;

			}

		}

	}

	cout<<ans;

	return 0;

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



using namespace std;

int main(int argc, char *argv[]) {

	int board[1200][1200];

	int W = 0;

	int H = 0;

	int N = 0;

	int X = 0;

	int Y = 0;

	int choice = 0;

	cin>>W>>H>>N;

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

	{

		for(int j=0;j<=1000;j++)

		{

			board[i][j] = 0;

			

		}

	}

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

	{

		for(int j=1;j<=W;j++)

		{

			board[i][j] = 1;

			

		}

	}

	for(int repeat = 1;repeat<=N;repeat++)

	{

		cin>>X>>Y>>choice;

		if(choice == 1)

		{

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

			{

				for(int j=1;j<=X;j++)

				{

					board[i][j] += 1;

					

				}

			}

		}

		if(choice == 2)

		{

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

			{

				for(int j=W;j>X;j--)

				{

					board[i][j] += 1;

					

				}

			}

		}

		if(choice == 3)

		{

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

			{

				for(int j=1;j<=W;j++)

				{

					board[i][j] += 1;

					

				}

			}

		}

		if(choice == 4)

		{

			for(int i=H;i>Y;i--)

			{

				for(int j=1;j<=W;j++)

				{

					board[i][j] += 1;

					

				}

			}

		}

	}

	

	

	

	

	int amount = 0;

	

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

	{

		for(int j=1;j<=W;j++)

		{

			if(board[i][j] == 1)

			{

				amount += 1;

			}

			

		}

	}

	cout<<amount;

	

}

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

#include <string>

using namespace std;



int main(){

    int w1=0,h1=0,w2,h2,n,i;

    cin >> w2 >> h2 >> n;

    int a[n];

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

        int x,y;

        cin >> x >> y >> a[i];

        if (a[i] == 1){

            if(x > w1){

                w1 = x;

            }

        }

        if (a[i] == 2){

            if (x < w2){

                w2 = x;

            }

        }

        if (a[i] == 3){

            if (y > h1){

                h1 = y;

            }

        }

        if (a[i] == 4){

            if (y < h2){

                h2 = y;

            }

        }

    }



    int num = (w2 - w1) * (h2 - h1);

    if (w2 < w1 || h2 < h1){

        cout << 0;

    }

    else{

        cout << num;

    }

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

using namespace std;

long long n, k, a[100005], s, ans=1e9;

int main(){

	cin>>n>>k;

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

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

	}

	long long now;

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

		now=a[i+k-1]-a[i];

		now+=min(abs(a[i]), abs(a[i+k-1]));

		ans=min(ans, now);

	}

	cout<<ans;

}

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

#include<algorithm>

using namespace std;

const int maxn = 1e5 + 7;

int a[maxn];

int main(){

    int n, k;

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

    int cnt = -1;

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

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

        if(a[i] >= 0 && cnt == -1){

            cnt = i;

        }

    }

    //a[cnt] >= 0

    if(cnt == -1){

        printf("%d\n", -a[n - k]);

        return 0;

    }

    else if(cnt == 0){

        printf("%d\n", a[k - 1]);

        return 0;

    }



    int ans = 2e9;

    if(cnt - k >= 0){

        ans = min(ans, -a[cnt - k]);

    }

    if(cnt + k - 1 < n){

        ans = min(ans, a[cnt + k - 1]);

    }

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

        if(cnt - i < 0 || cnt + k - i - 1 >= n)

            continue;

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

    }

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

        if(cnt + i - 1 >= n || cnt - k + i < 0)

            continue;

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

    }

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

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

using namespace std;



int main() {



    ios_base::sync_with_stdio(false);

    cin.tie(0);



    long long n, k;

    cin >> n >> k;

    vector <long long> arr(n);

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

        cin >> arr[i];



    long long cost = LONG_LONG_MAX;

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

        if(arr[i] < 0 && arr[i+k-1] <= 0){

            cost = min(cost, abs(arr[i]));

        }

        else if( arr[i]>=0 && arr[i+k-1] > 0){

            cost = min(cost, arr[i+k-1]);

        }

        else{

            cost= min(cost, min(abs(arr[i])*2+arr[i+k-1], abs(arr[i])+arr[i+k-1]*2));

        }

    }

    cout << cost << '\n';

    return 0;

}


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

#include<algorithm>

using namespace std;

const int maxn = 1e5 + 7;

int a[maxn];

int main(){

    int n, k;

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

    int cnt = -1;

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

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

        if(a[i] >= 0 && cnt == -1){

            cnt = i;

        }

    }

    //a[cnt] >= 0

    if(cnt == -1){

        printf("%d\n", -a[n - k]);

        return 0;

    }

    else if(cnt == 0){

        printf("%d\n", a[k - 1]);

        return 0;

    }



    int ans = 2e9;

    if(cnt - k >= 0){

        ans = min(ans, -a[cnt - k]);

    }

    if(cnt + k - 1 < n){

        ans = min(ans, a[cnt + k - 1]);

    }

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

        if(cnt - i < 0 || cnt + k - i - 1 >= n)

            continue;

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

    }

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

        if(cnt + i - 1 >= n || cnt - k + i < 0)

            continue;

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

    }

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

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

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

#define f(i, l, r) for (int i = l; i <= r; i++)

#define rf(i, r, l) for (int i = r; i >= l; i--)

#define all(x) (x).begin(), (x).end()

#define sf(a) scanf("%d",&a)

#define llsf(a) scanf("%lld", &a)

#define l l

#define r r

#define lch (o << 1)

#define rch (o << 1 | 1)

#define mid ((l + r) >> 1)

#define mem(x, y) memset(x, y, sizeof(x))



const int inf = 2e9 + 7;

const ll INF = INT64_MAX;

double eps = 1e-6;

int mod = 1e9 + 7;

const int N = 1e6 + 10;

const double pi = acos(-1.0);



int a[N];

int n,k;

vector<int> l, r;

int main() {

#ifdef local

	freopen("in.txt", "r", stdin);

#endif

	sf(n); sf(k);

	f(i, 1, n) {

		cin >> a[i];

	}

	sort(a + 1, a + 1 + n);

	int l = 1, r = k;

	int res = inf;

	while (r <= n) {

		if (a[l] < 0 && a[r] < 0)res = min(res, -a[l]);

		else if (a[l] < 0 && a[r] >= 0) {

			res = min(res, 2 * min(-a[l], a[r]) + max(-a[l], a[r]));

		}

		else res = min(res, a[r]);

		l++, r++;

	}

	cout << res;

}

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

#include<algorithm>

using namespace std;

const int maxn = 1e5 + 7;

int a[maxn];

int main(){

    int n, k;

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

    int cnt = -1;

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

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

        if(a[i] >= 0 && cnt == -1){

            cnt = i;

        }

    }

    //a[cnt] >= 0

    if(cnt == -1){

        printf("%d\n", -a[n - k]);

        return 0;

    }

    else if(cnt == 0){

        printf("%d\n", a[k - 1]);

        return 0;

    }



    int ans = 2e9;

    if(cnt - k >= 0){

        ans = min(ans, -a[cnt - k]);

    }

    if(cnt + k - 1 < n){

        ans = min(ans, a[cnt + k - 1]);

    }

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

        if(cnt - i < 0 || cnt + k - i - 1 >= n)

            continue;

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

    }

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

        if(cnt + i - 1 >= n || cnt - k + i < 0)

            continue;

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

    }

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

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

typedef long long ll;

using namespace std;

const int N = 1e5+7;

const ll mod = 1e9+7;

int a[N];

int n,k,ans,now;

int main()

{

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

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

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

    if(a[n]<=0){

        ans=-a[n+1-k];

    }

    else if(a[1]>=0){

        ans=a[k];

    }

    else{

        ans=1e9;

        int m;

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

            if(a[i]>=0){

                m=i;break;

            }

        }

        for(int i=max(1,m-k);i<=min(m,n+1-k);i++){

            now=a[i+k-1]-a[i];

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

                ans=min(ans,now-a[i+k-1]);

            }

            else if(a[i]>=0){

                ans=min(ans,now+a[i]);

            }

            else{

                ans=min(ans,now-a[i]);

                ans=min(ans,now+a[i+k-1]);

            }

        }

    }

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

}


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

#include<algorithm>

using namespace std;

const int maxn = 1e5 + 7;

int a[maxn];

int main(){

    int n, k;

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

    int cnt = -1;

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

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

        if(a[i] >= 0 && cnt == -1){

            cnt = i;

        }

    }

    //a[cnt] >= 0

    if(cnt == -1){

        printf("%d\n", -a[n - k]);

        return 0;

    }

    else if(cnt == 0){

        printf("%d\n", a[k - 1]);

        return 0;

    }



    int ans = 2e9;

    if(cnt - k >= 0){

        ans = min(ans, -a[cnt - k]);

    }

    if(cnt + k - 1 < n){

        ans = min(ans, a[cnt + k - 1]);

    }

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

        if(cnt - i < 0 || cnt + k - i - 1 >= n)

            continue;

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

    }

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

        if(cnt + i - 1 >= n || cnt - k + i < 0)

            continue;

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

    }

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

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

#include<cstdio>

#include<cstdlib>

#include<cmath>

#include<cstring>

#include<algorithm>

using namespace std;



int n,m,ans;

int a[200005];

int main()

{

	int i;

	cin>>n>>m;

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

		cin>>a[i];



	ans=500000000;

	for(i=m;i<=n;i++)

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

	cout<<ans;

	return 0;	

} 




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

#include<algorithm>

using namespace std;

const int maxn = 1e5 + 7;

int a[maxn];

int main(){

    int n, k;

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

    int cnt = -1;

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

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

        if(a[i] >= 0 && cnt == -1){

            cnt = i;

        }

    }

    //a[cnt] >= 0

    if(cnt == -1){

        printf("%d\n", -a[n - k]);

        return 0;

    }

    else if(cnt == 0){

        printf("%d\n", a[k - 1]);

        return 0;

    }



    int ans = 2e9;

    if(cnt - k >= 0){

        ans = min(ans, -a[cnt - k]);

    }

    if(cnt + k - 1 < n){

        ans = min(ans, a[cnt + k - 1]);

    }

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

        if(cnt - i < 0 || cnt + k - i - 1 >= n)

            continue;

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

    }

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

        if(cnt + i - 1 >= n || cnt - k + i < 0)

            continue;

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

    }

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

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



using namespace std;



const int MAXN = 100005;

int candles[MAXN];



int main() {

    int n, k, minDist = INT32_MAX;

    cin >> n >> k;

    int zeroIndex = -1;

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

        cin >> candles[i];

    }

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

        if (candles[i] > 0) {

            zeroIndex = i;

            break;

        }

    }

    if (zeroIndex == -1)

        cout << abs(candles[max(0, n - k)]) << endl;

    else if (zeroIndex == 0)

        cout << abs(candles[min(k - 1, n - 1)]) << endl;

    else {

        for (int i = max(0, zeroIndex - k); i < min(zeroIndex, n - k + 1); i++){

            minDist = min(minDist, abs(candles[i]) > abs(candles[i + k - 1]) ? abs(candles[i + k - 1]) * 2 + abs(candles[i]) : abs(candles[i]) * 2 + abs(candles[i + k - 1]));

        }

        cout << minDist << endl;

    }

}






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

#include<cstring>

#include<algorithm>

const int maxn=1e5+10;

int n,k,x[maxn],ans=0x7fffffff;

int main()

{

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

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

    {

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

    }

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

    {

        int r=l+k-1;

        ans=std::min(ans,x[r]-x[l]+std::min(abs(x[l]),abs(x[r])));

    }

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

}