// AOJ 0529 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0529)
#include<cstdio>
#include<algorithm>
#define rep(i,a) for(int i=0;i<(a);++i)

const int MAX_N = 1001;

int N, M;
int P[MAX_N], S[MAX_N*MAX_N];

int main()
{
	while( scanf( "%d%d", &N, &M ), N|M )
	{
		rep( i, N )
			scanf( "%d", P+i+1 );
		++N;

		rep( i, N ) rep( j, N )
			S[i*N+j] = P[i]+P[j];

		std::sort( S, S+N*N );

		int ans = 0;
		rep( i, N*N )
		{
			int t = S[i]+*(std::upper_bound( S, S+N*N, M-S[i] )-1);

			if( t <= M )
				ans = std::max( ans, t );
		}

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

	return 0;
}