Go: ///
// File:  0529.go
// Author: ymiyamoto
//
// Created on Wed Feb 28 20:39:28 2018
//
package main

import (
	"fmt"
	"sort"
)

func max(n, m int64) int64 {
	if n < m {
		return m
	} else {
		return n
	}
}

func main() {
	for {
		var N int
		var M int64
		fmt.Scan(&N, &M)
		if N == 0 && M == 0 {
			break
		}

		arr := make([]int64, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })

		sums := make([]int64, len(arr)*len(arr))
		for i := 0; i < len(arr); i++ {
			for j := 0; j < len(arr); j++ {
				sums[i*len(arr)+j] = arr[i] + arr[j]
			}
		}

		sort.Slice(sums, func(i, j int) bool { return sums[i] < sums[j] })

		var m int64 = 0
		for i := 0; i < len(sums); i++ {
			rem := M - sums[i]
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, sums[i]+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 1005
#define LSIZE 1000005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;
ll M;
ll T[SIZE];
set<ll> S;
ll K;


ll solve()
{
	scanf("%d %lld",&n,&M);
	if(n+M ==0)return -1;
	for(int i=1;i<=n;i++)scanf("%lld",&T[i]);
	T[0]=0;
	S.clear();
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			S.insert(T[i]+T[j]);
		}
	}
	for(ll i=M;i>=0;i--){
		for(set<ll>::iterator it=S.begin();it!=S.end();it++){
			if(i-(*it) < 0)break;
			if(S.find(i-(*it))!=S.end()){
				return i;
			}
		}
	}
	return 0;
}

int main()
{
	while(1){
		ll ans = solve();
		if(ans==-1)break;
		else printf("%lld\n",ans);
	}
	return 0;
}

C++: #include<iostream>
#include<set>
using namespace std;

int main(int argc, char *argv[]){
  int n, m;
  while(cin >> n >> m, n){
    int point[n + 1];
    point[0] = 0;
    set<int> a, b;
    for (int i = 1; i <= n; i++) {
      cin >> point[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a.insert(point[i] + point[j]);
      }
    }
    int ans = 0;
    for (int i: a) {
      if(--a.upper_bound(m - i) != a.end()){
        ans = max(ans, i + *(--a.upper_bound(m - i)));
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD (1000000007)
#define INF (1e9)
#define INFL (1e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} pr(a[a.size()-1]);}
template<class T>void prarr(arr<T>& a){rep(i, a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int n, m;
vector<int> a;

void solve(){
    vector<int> c(n*(n-1)/2 + n + 1);
    int ci=0;
    rep(i, n) FOR(j, i+1, n){
        c[ci++] = a[i] + a[j];
    }
    rep(i, n) c[ci++] = a[i];
    c[ci] = 0;
    sort(ALL(c));
    // prvec(c);
    int ans = 0;
    rep(i, n) rep(j, n){
        if(a[i] + a[j] > m) continue;
        // printf("%d, %d, %d\n", a[i], a[j], m - a[i] - a[j]);
        int k = *(--upper_bound(ALL(c), m - a[i] - a[j]));
        // printf("k=%d\n", k);
        // pr(a[i] + a[j] + k);
        chmax(ans, a[i] + a[j] + k);
    }
    pr(ans);
}

int main()
{
    while(cin >> n >> m){
        if(n==0 && m==0) break;
        a.resize(n);
        rep(i, n) cin >> a[i];
        solve();
    }
    return 0;}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
int main() {
	ll a[1000], n, m;
	while (cin >> n >> m&&n != 0 || m != 0) {
		for (int i = 0; i < n; i++) 
			cin >> a[i];
		long long sum = 0;
		sort(a, a + n);
		for (int i = 0; i < n; i++) {
			if (a[i] <= m) {
				sum = max(a[i], sum);
				for (int j = 0; j < n; j++) {
					if (a[i] + a[j] <= m) {
						sum = max(a[i] + a[j], sum);
						for (int x = 0; x < n; x++) {
							if (a[i] + a[j] + a[x] <= m) {
								sum = max(sum, a[i] + a[j] + a[x]);
								for (int y = 0; y < n; y++) {
									if (a[i] + a[j] + a[x] + a[y] <= m) {
										sum = max(sum, a[i] + a[j] + a[x] + a[y]);
									}
									else break;
								}
							}
							else break;
						}
					}
					else break;
				}
			}
			else break;
		}
		cout << sum << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> a;
vector <int> all;
int n,m;


int main() {

  while(1) {
    cin >> n >> m;
    if(n == 0 && m ==0) break;
    a.resize(n);
    for(int i=0;i<n;i++) cin >>a[i];

    a.push_back(0);
    all.clear();
    for(int i=0;i<a.size();i++) 
      for(int j=i;j<a.size();j++)
	all.push_back(a[i]+a[j]);

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

    int ans = 0;
    int allsize = all.size();
    int b = 0;

    for(int i=allsize-1;i>= 0;i--) {
      for(int j=b;j<allsize;j++) {
	int sum = all[i] + all[j];
	if(sum > m) break;
	ans = max(ans,sum);
      }
      if(ans == m) break;
    }


    cout << ans <<endl;
  }
  return 0;
  
}

C++: #include <cstdio>
#include <fstream>
#include <algorithm>
using namespace std;

#define MAX_N 1000
int P[MAX_N + 1];
int N, M;
int table[5][MAX_N + 1];

int dfs(int a, int b, int c){
	if(c > M) return P[b] * -1;
	if(a == 0) return P[b];
	if(table[a][b] >= 0) return table[a][b];
	
	int cho = b;
	while(cho <= N){
		int d = dfs(a - 1, cho, c + P[b]) + P[b];
		table[a][b] = max(table[a][b], (d > M)? 0 : d);
		cho++;
	}
	
	return table[a][b];
}

int main(){
	ifstream ifs("input.txt");
	
	for(int i = 0; i < 5; i++){
		for(int j = 0; j <= MAX_N; j++){
			table[i][j] = -1;
		}
	}
	
	ifs >> N >> M;
	
	for(int i = 1; i <= N; i++) ifs >> P[i];
	
	printf("%d\n", dfs(4, 0, 0));
	
	return 0;
}

C++: #include<iostream>
#include<cstdlib>
using namespace std;
int main(){
  int n,m,k;
  int *t;
  while(cin>>n>>m){
    if(n==0&&m==0) break;
    t = new int[m+1];
    for(int i=0;i<=m;i++) t[i] = 0;
    for(int i=0;i<n;i++){
      cin >> k;
      for(int j=0;j<4;j++){
	for(int g=m-1;g>=0;g--){
	  if(t[g]>=1&&g+k<=m&&t[g]<4){
	    if(t[g+k]==0||t[g+k]>t[g]+1) t[g+k] = t[g] + 1;
	  }
	}
	t[k] = 1;
      }
    }
    for(int i=m;i>=0;i--){
      if(t[i]!=0){
	cout << i << endl;
	break;
      }
    }
  }
  delete[] t;
  return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;
int main(){
    while(true){
	ll n,m;
	cin >> n >> m;
	if(n == 0 && m == 0) break;
	vector<ll> dp[2];
	vector<ll> a(n);
	REP(i,n){
	    cin >> a[i];
	    dp[0].PB(a[i]);
	}
	dp[1] = dp[0];
	REP(i,3){
	    REP(j,dp[0].size()){
		REP(k,n){
		    if(dp[0][j] + a[k] <= m){
			dp[1].PB(dp[0][j] + a[k]);
		    }
		}
	    }
	    dp[0] = dp[1];
	}
	sort(ALL(dp[0]));
	cout << dp[0].back()<<endl;
    }
    return 0;
}