stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<iostream>

#include<cstring>

#include<algorithm>

using namespace std;



const int H=60*60;

const int M=60;

const int S =1;



int train[24*H+1];

int unit[3]={H,M,S};



int getTime(string s){

  string tmp;

  int res=0;

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

    tmp=s.substr(3*i,2);

    int val=atoi(tmp.c_str());

    res+=unit[i]*val;

  }

  return res;

}



int main(){

  int n;

  while(cin>>n,n){

    memset(train,0,sizeof(train));

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

      string start,arrival;

      int st=0,at=0;

      cin>>start>> arrival;

      st=getTime(start);

      at=getTime(arrival);

      for(int i=st;i<at;i++)train[i]++;

    }

    int ans=0;

    for(int i=0;i<=24*H;i++){

      ans=max(ans,train[i]);

      //cout << train[i] << " ";

    }

    cout << ans << endl;

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<vector>

#include<cstring>

#include<algorithm>

using namespace std;



typedef pair<int,int> pii;



vector<pii>train;

const int H=60*60;

const int M=60;

const int S=1;

int unit[3]={H,M,S};



int getTime(string s){

  string tmp;

  int res=0;

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

    tmp=s.substr(3*i,2);

    int val=unit[i]*atoi(tmp.c_str());

    res+=val;

  }

  return res;

}



void makeEvent(string start,string arrival){

  int st=getTime(start);

  int at=getTime(arrival);

  train.push_back(make_pair(st,1));

  train.push_back(make_pair(at,0));

}



int main(){

  int n;

  while(cin>>n,n){

    train.clear();

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

    string start,arrival;

    cin>>start>>arrival;

    makeEvent(start,arrival);

    }

    int ans=0,tmp=0;

    sort(train.begin(), train.end());

    for(pii i:train){

      if(i.second==1){

	tmp++;

      }else{

	tmp--;

      }

      ans=max(tmp,ans);

    }

    cout << ans << endl;

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>



using namespace std;



typedef long long ll;



int main(){



  int q;

  cin >> q;

  ll ans = 0;

  while(q--){

    ll p,n;

    char c;

    cin >> p >> c >> n;

    if(c == ')') ans -= n;

    if(c == '(') ans += n;

    if(ans == 0) cout << "Yes" << endl;

    else cout << "No" << endl;

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>



using namespace std;



typedef long long ll;



int main(){



  int q;

  scanf("%d",&q);

  ll ans = 0;

  while(q--){

    int p,n;

    char c;

    scanf("%d %c %d",&p,&c,&n);

    if(c == ')') ans -= n;

    else ans += n;

    if(ans == 0) printf("Yes\n");

    else printf("No\n");

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=1505,p=1000000007;

int read(){

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

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

	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';

	return f*g;

}

int n,a[N][N],s[N][N],l[N][N],r[N][N];

ll ans;

char ch[N];

void del(int x,int y){

	for (int i=y;i<=n;i++) s[x][i]-=a[x][y];

	a[x][y]=0;

	if ((a[x][y+1])&&(!a[x-1][y+1])) del(x,y+1);

	if ((a[x+1][y])&&(!a[x+1][y-1])) del(x+1,y);

}

void chkmin(int &x,int y){if (x>y) x=y;}

void chkmax(int &x,int y){if (x<y) x=y;}

int main(){

	n=read();

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

		scanf(" %s",ch+1);

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

		a[i][j]=(ch[j]=='#') ? 0 : (ch[j]-'0'),s[i][j]=s[i][j-1]+a[i][j];

	}

	memset(l,0x3f,sizeof(l));

	for (int i=n;i;i--)

	for (int j=n;j;j--)

	if (a[i][j]){

		int t=-a[i][j];

		l[j][i]=r[j][i]=j;

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

			chkmin(l[j][x],l[j+1][x]);chkmax(r[j][x],r[j+1][x]);

			if (l[j][x]<=r[j][x]) t+=s[x][r[j][x]]-s[x][l[j][x]-1];

		}

		ans+=a[i][j]*t;

		if (a[i-1][j]+a[i][j-1]==0) del(i,j);

	}

	else{memset(l[j]+i,0x3f,(n-i+1)*4);memset(r[j]+i,0,(n-i+1)*4);}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=1505,p=1000000007;

int read(){

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

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

	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';

	return f*g;

}

int n,f[N][N],d[N][N];

typedef unsigned short us;

us a[N][N],l[N][N],r[N][N];

ll ans;

char ch[N];

inline void chkmin(us &x,us y){if (x>y) x=y;}

inline void chkmax(us &x,us y){if (x<y) x=y;}

int main(){

	n=read();

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

		scanf(" %s",ch+1);

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

		a[i][j]=(ch[j]=='#') ? 0 : (ch[j]-'0');

	}

	memset(l,0x3f,sizeof(l));

	for (int i=n;i;i--)

	for (int j=n;j;j--)

	if (a[i][j]){

		int t=f[i+1][j]+f[i][j+1];

		d[i][j]=max(max(d[i][j+1],d[i+1][j]),i);

		if (!a[i][j+1]){l[j][i]=r[j][i]=j;}

		else if (!a[i+1][j]){

			memcpy(l[j]+i,l[j+1]+i,(n-i+1)*2);

			memcpy(r[j]+i,r[j+1]+i,(n-i+1)*2);

			l[j][i]=j;

		}

		else{

			l[j][i]=j;r[j][i]=r[j+1][i];

			us las=0;

			for (int x=i+1;x<=d[i][j];x++){

				if ((l[j+1][x]<=r[j][x])&&(las<l[j+1][x])) t-=f[x][l[j+1][x]];

				chkmax(las,r[j][x]);

				chkmin(l[j][x],l[j+1][x]);chkmax(r[j][x],r[j+1][x]);

			}

		}

		ans+=a[i][j]*t;

		f[i][j]=a[i][j]+t;

	}

	else{memset(l[j]+i,0x3f,(n-i+1)*2);memset(r[j]+i,0,(n-i+1)*2);}

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

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=1505,p=1000000007;

int read(){

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

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

	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';

	return f*g;

}

int n,a[N][N],s[N][N],l[N][N],r[N][N];

ll ans;

char ch[N];

void del(int x,int y){

	for (int i=y;i<=n;i++) s[x][i]-=a[x][y];

	a[x][y]=0;

	if ((a[x][y+1])&&(!a[x-1][y+1])) del(x,y+1);

	if ((a[x+1][y])&&(!a[x+1][y-1])) del(x+1,y);

}

void chkmin(int &x,int y){if (x>y) x=y;}

void chkmax(int &x,int y){if (x<y) x=y;}

int main(){

	n=read();

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

		scanf(" %s",ch+1);

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

		a[i][j]=(ch[j]=='#') ? 0 : (ch[j]-'0'),s[i][j]=s[i][j-1]+a[i][j];

	}

	memset(l,0x3f,sizeof(l));

	for (int i=n;i;i--)

	for (int j=n;j;j--)

	if (a[i][j]){

		int t=-a[i][j];

		l[j][i]=r[j][i]=j;

		if (a[i][j+1]) for (int x=i;x<=n;x++){

			chkmin(l[j][x],l[j+1][x]);chkmax(r[j][x],r[j+1][x]);

			if (l[j][x]<=r[j][x]) t+=s[x][r[j][x]]-s[x][l[j][x]-1];else break;

		}

		else for (int x=i;(x<=n)&&(l[j][x]<=r[j][x]);x++)

			t+=s[x][r[j][x]]-s[x][l[j][x]-1];

		ans+=a[i][j]*t;

		if (a[i-1][j]+a[i][j-1]==0) del(i,j);

	}

	else{memset(l[j]+i,0x3f,(n-i+1)*4);memset(r[j]+i,0,(n-i+1)*4);}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=1505,p=1000000007;

int read(){

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

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

	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';

	return f*g;

}

int n,f[N][N],d[N][N];

typedef unsigned short us;

us a[N][N],l[N][N],r[N][N];

ll ans;

char ch[N];

inline void chkmin(us &x,us y){if (x>y) x=y;}

inline void chkmax(us &x,us y){if (x<y) x=y;}

int main(){

	n=read();

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

		scanf(" %s",ch+1);

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

		a[i][j]=(ch[j]=='#') ? 0 : (ch[j]-'0');

	}

	memset(l,0x3f,sizeof(l));

	for (int i=n;i;i--)

	for (int j=n;j;j--)

	if (a[i][j]){

		int t=f[i+1][j]+f[i][j+1];

		d[i][j]=max(max(d[i][j+1],d[i+1][j]),i);

		if (!a[i][j+1]){l[j][i]=r[j][i]=j;}

		else if (!a[i+1][j]){

			memcpy(l[j]+i,l[j+1]+i,(n-i+1)*2);

			memcpy(r[j]+i,r[j+1]+i,(n-i+1)*2);

			l[j][i]=j;

		}

		else{

			l[j][i]=j;r[j][i]=r[j+1][i];

			us las=0;

			for (int x=i+1;x<=d[i][j];x++){

				if ((l[j+1][x]<=r[j][x])&&(las<l[j+1][x])) t-=f[x][l[j+1][x]];

				chkmax(las,r[j][x]);

				chkmin(l[j][x],l[j+1][x]);chkmax(r[j][x],r[j+1][x]);

			}

		}

		ans+=a[i][j]*t;

		f[i][j]=a[i][j]+t;

	}

	else{memset(l[j]+i,0x3f,(n-i+1)*2);memset(r[j]+i,0,(n-i+1)*2);}

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

	return 0;

}

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

}

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

}

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

}

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

}

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

}


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

}

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

}

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

}

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



}




















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

}

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

}

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

}

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

}

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

}

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

}


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

}

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

}

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

}

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

}

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

}

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

}

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

}

// Fastest known solution for this problem: