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 <algorithm>

using namespace std;

int main() {
  
  int n, m;
  
  while(cin >> n >> m && (n|m)) {
    int data[1001];
    int Q[1000001];
    
    n++;
    data[0] = 0;
    for(int i=1; i<n; i++) {
      cin >> data[i];
    }
    
    int T = 0;
    
    for(int i=0; i<n; i++) {
      Q[T++] = data[i];
    }
    
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
	Q[T++] = data[i] + data[j];
      }
    }
    
    sort(Q, Q+T);

    // for(int i=0; i<T; i++) cout << Q[i] << endl;
    
    int ans = 0;
    
    for(int i=0; i<T; i++) {
      int M = m - Q[i];
      if(M < 0) break;
      
      int lb = 0, ub = T;
      
      while(ub - lb > 1) {
	int mid = (lb + ub) / 2;
	
	if(Q[mid] <= M) {
	  lb = mid;
	} else {
	  ub = mid;
	}
      }
      ans = max(ans, Q[i]+Q[lb]);
    }
    
    cout << ans << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define Rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define all(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;


int main(){
  ll n,m,p[1010];
  while(cin >> n >> m){
    if(n==0 && m==0)break;

    rep(i,n)cin >> p[i];
    vector<ll> sum;

    sum.push_back(0);
    rep(i,n){
      sum.push_back(p[i]);
      Rep(j,i,n)sum.push_back(p[i]+p[j]);
    }
    sort(all(sum));
    sum.erase(unique(all(sum)),sum.end());

    ll res = 0;
    rep(i,sum.size()){
      if(m<sum[i])break;
      int pos = upper_bound(all(sum),m-sum[i]) - sum.begin();
      if(pos>0)res = max(res, sum[pos-1] + sum[i]);
    }
    cout << res << endl;
  }
}

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

#define loop(i,n) for(int i=1;i<=n;i++)
int n,m;
int p[1002];
set<int> s1;
set<int> s2;

signed main(){
  while(cin>>n>>m,n){
    loop(i,n)cin>>p[i];
    s1.clear();
    s2.clear();
    s1.insert(0);
    loop(a,4){
      for(int x:s1){
        s2.insert(x);
        loop(i,n){
          s2.insert(x+p[i]);
        }
      }
      swap(s1,s2);
    }
    int mx=0;
    for(int x:s1){
      if(x<=m)mx=max(x,mx);
    }
    cout<<mx<<endl;
  }
  return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>

using namespace std;

#define sz			size()
#define lgt			length()
#define rep(i,n)	for (int i = 0; i < n; i++)
#define ALL(X)		(X).begin(),(X).end()
#define RALL(X)		(X).rbegin(),(X).rend()
#define pb			push_back
#define SORT(a)		sort(ALL(a));
#define RSORT(a)	sort(RALL(a));
#define mp			make_pair
#define dump(x)		cerr<<#x<<" = "<<(x)<<endl;
#define fir			first
#define sec			second

typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<string> vs;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
vs form(string a,char s){vs v;string b;rep(i,a.lgt){if(a[i]==s){v.pb(b);b="";}else b+=a[i];}v.pb(b);return v;}
vi formint(string a,char s){vi v;string b;rep(i,a.lgt){if(a[i]==s){v.pb(toInt(b));b="";}else b+=a[i];}v.pb(toInt(b));return v;}

const double EPS=1e-10,PI=acos(-1.0);
const int INF=2100000000;

int main(void)
{
	int n, m, tmp;
	while (cin >> n >> m, n && m){
		int res = 0;
		vi v, v1;
		
		v.pb(0);
		for (int i = 0; i < n; i++){
			cin >> tmp;
			v.pb(tmp);
		}
		for (int i = 0; i < v.sz; i++){
			for (int j = 0; j < v.sz; j++){
				v1.pb(v[i] + v[j]);
			}
		}
		SORT(v1);
		
		for (int i = 0, j = v1.sz - 1; i < v1.sz && 0 <= j;){
			if (m < v1[i] + v1[j]) j--;
			else {
				res = max(res, v1[i] + v1[j]);
				i++;
			}
		}
		
		cout << res << endl;
		v.clear();
	}
	
	return 0;
}

C++: 
/******************************************************************

	AOJ 0529　ダーツの問題

	矢を四本投げるので、2本投げたときの

*******************************************************************/

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define INF 1000000000
#define EPS 1e-10

#define MAX_N 1001

int N, M;
int po[MAX_N];

vector<int> ans_list;

int main(){

	while (true){
		cin >> N >> M;
		if (N == 0 && M == 0)
			break;
		for (int i = 0; i < N; i++){
			cin >> po[i];
		}
		po[N] = 0;
		N++;
		vector<int> A;

		for (int i = 0; i < N; i++){
			for (int j = 0; j < N; j++){
				A.push_back(po[i] + po[j]);
			}
		}

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

		int ans = 0;
		for (int i = 0; i < A.size(); i++){
			int tmp = A[i];
			int max_tmp = M - tmp;
			if (max_tmp < 0)
				break;
			int lb = 0, ub = A.size();
			while (ub - lb > 1){
				int mid = (ub + lb) / 2;
				if (max_tmp < A[mid])
					ub = mid;
				else
					lb = mid;
			}
			ans = max(ans, M - (max_tmp - A[lb]));
		}

		ans_list.push_back(ans);

	}

	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}

}

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(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 <cstdio>
#include <algorithm>
using namespace std;

#define MAX_N 1000
int P[MAX_N + 1];
int N, M;
int table[(MAX_N + 1) * (MAX_N + 1)];

int main(){
	while(1){		
		scanf("%d%d", &N, &M);
		
		if(N == 0 && M == 0) return 0;
		
		P[0] = 0;
		for(int i = 1; i <= N; i++) scanf("%d", &P[i]);
	
		for(int i = 0; i < N + 1; i++){
			for(int j = 0; j < N + 1; j++){
				table[(N + 1) * i + j] = P[i] + P[j];
			}
		}
		sort(table, table + (N + 1) * (N + 1));
		
		int right = (N + 1) * (N + 1) - 1;
		int res = 0;
		for(int i = 0; i < (N + 1) * (N + 1); i++){
			while(right >= 0){
				if(table[i] + table[right] <= M){
					res = max(res, table[i] + table[right]);
					break;
				}
				if(table[i] == table[right]) goto OUT;
				right--;
			}
		}
		OUT:
		
		printf("%d\n", res);
	}
}