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

import (
	"fmt"
	"sort"
)

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

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

		arr := make([]int, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Ints(arr)

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

		m := 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<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;
}

C++: #define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

int main(){
  int n,m;
  while(cin>>n>>m){
    if(n==0 && m==0) return 0;
  
  vector<int> A(n+1);
  rep(i,n) cin>>A[i];
  A[n]=0;
  
  vector<int> B;
  B.push_back(0);
  rep(i,n+1){
    rep(j,n+1){
      if(A[i]+A[j]<=m) B.push_back(A[i]+A[j]);
    }
  }
  sort(ALL(B));
  B.erase(unique(ALL(B)),B.end());

  ll ans=0;
  rep(i,B.size()){
    auto itr=upper_bound(ALL(B),m-B[i])-B.begin();
    ll tmp=B[i]+B[itr-1];
    ans=max(ans,tmp);
  }
  
  cout<<ans<<endl;
  }
  
}

C++: #include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i,x) for(ll i=0;i<x;i++)
#define rep1(i,x) for(ll i=1;i<=x;i++)
#define rrep(i,x) for(ll i=x-1;i>=0;i--)
#define rrep1(i,x) for(ll i=x;i>0;i--)

ll n,m;
ll p[1000];
vector<ll> v;
int main()
{while(1){
	v.clear();
	cin>>n>>m;
	if(!n) break;
	p[0]=0;
	rep1(i,n) cin>>p[i];
	rep(i,n+1) rep(j,n+1){
		v.pb(p[i]+p[j]);
	}
	sort(v.begin(),v.end());
	ll ans;
	ans=0;
	//ans=v[upper_bound(v.begin(),v.end(),m)-v.begin()-1];
	rep(i,n+1) rep(j,n+1){
		if(p[i]+p[j]<=m) ans=max(ans,v[upper_bound(v.begin(),v.end(),m-p[i]-p[j])-v.begin()-1]+p[i]+p[j]);
	}
	//for(ll i=n/2;i<n;i++) if(p[i]<=m) ans=max(ans,v[upper_bound(v.begin(),v.end(),m-p[i])-v.begin()-1]+p[i]);
	//for(ll i=n/2;i<n;i++) for(ll j=i+1;j<n;j++) if(p[i]+p[j]<=m) ans=max(ans,v[upper_bound(v.begin(),v.end(),m-p[i]-p[j])-v.begin()-1]+p[i]+p[j]);
	cout<<ans<<endl;
}}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
int main(){
while(1){
int i,n,j,m,s=0,l,k;
static int p[1001]={0};
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++)scanf("%d",&p[i]);
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
for(l=0;l<=n;l++){
for(k=0;k<=n;k++){
if(p[i]+p[j]+p[l]+p[k]<=m&&p[i]+p[k]+p[j]+p[l]>s)s=p[i]+p[j]+p[l]+p[k];
}
}
}
}
printf("%d\n",s);
}
return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int n, m;

	while (cin >> n >> m, n && m) {
		vector<int> point(n + 1);
		vector<int> sum;
		for (int i = 0; i < n; i++) {
			cin >> point[i];
		}
		for (int i = 0; i < n + 1; i++) {
			for (int j = i; j < n + 1; j++) {
				sum.push_back(point[i] + point[j]);
			}
		}
		sort(sum.begin(), sum.end());

		int ans = 0;
		for (int i = 0; i < sum.size(); i++) {
			int t = m - sum[i];
			cout << "t = " << t << endl;
			vector<int>::iterator it = upper_bound(sum.begin(), sum.end(), t);
			if (it != sum.begin()) {
				ans = max(ans, sum[i] + * (it - 1));
			}
		}
		cout << ans << endl;
	}
}