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 <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

int main(void){
	int n, m; cin >> n >> m;
	vector<int> p(n);
	rep(i, n) cin >> p[i];
	int ans = 0;
	rep(i, n)rep(j, n)rep(k, n)rep(l, n){
		int tmp = p[i] + p[j] + p[k] + p[l];
		if(tmp <= m) ans = max(ans, tmp);
	}
	printf("%d\n", ans);
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
while(1){
int i,j,n,m,o=1,s=0;
static int p[1001]={0},up[1001*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(j=i;j>=0;j--){
up[o]=p[i]+p[j];
o++;
}
}
sort(up,up+o);
for(i=0;i<o;i++)printf("%d\n",up[i]);
for(i=0;i<o;i++){
if(up[i]+*(upper_bound(up,up+o,m-up[i])-1)<=m)s=max(s,up[i]+*(upper_bound(up,up+o,m-up[i])-1));
}
printf("%d\n",s);
}
return 0;
}

C++: #include<bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a,b) (cin >> a >> b)
#define cin3(a,b,c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fr first 
#define sc second 
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef vector<pair<int,int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int,int> pii;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n,m;
int a,b;
int p,q;


signed main() {
	
	while(1) {
		cin2(n,m);
		if (n + m == 0) break;

		vi p(n,0);
		rep(i,0,n) {
			cin1(p[i]);
		}
		p.pb(0);

		rep(i,0,n) {
			rep(j,0,n) {
				if (p[i] + p[j] <= m) { 
					p.pb(p[i] + p[j]);	
				}
			}
		}
		sort(p.begin(),p.end());

		int size = p.size();

		int ans = 0;
		rep(i,0,size) {
			int c_d = p[i];
			int a_b = *(upper_bound(p.begin(),p.end(),m - c_d) - 1);
			ans = max(ans,a_b + c_d);
		}
		cout(ans);
	}



	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

int N, M;
int P[1000];
vector<int> D;

int main() {
  while (cin >> N >> M) {
    if (N == 0 && M == 0) break;
    for (int i=0; i<N; i++) cin >> P[i];

    D.clear();
    D.push_back(0);
    for (int i=0; i<N; i++) {
      D.push_back(P[i]);
      for (int j=0; j<N; j++) {
        D.push_back(P[i] + P[j]);
      }
    }

    sort(D.begin(), D.end());
    int ans = 0;
    for (auto x : D) {
      int t = x + *(upper_bound(D.begin(), D.end(), M - x) - 1);
      if (t <= M) ans = max(ans, t);
    }
    cout << ans << "\n";
  }
  return 0;
}

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

int main(int argc, char *argv[]){
  int n, m;
  while(scanf("%d %d\n", &n, &m)){
    if(n == 0 && m == 0)break;
    int point[n + 1];
    point[0] = 0;
    set<int> a;
    for (int i = 1; i <= n; i++) {
      scanf("%d\n", &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){
      ans = max(ans, i + *(--a.upper_bound(m - i)));
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
  
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
  
using namespace std;
  
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
  
const int INF=1<<29;
const double EPS=1e-9;
  
const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
    int N, T;
    cin >> N >> T;
    vector<int> P(N);
    for (int i = 0; i < N; i++) {
        cin >> P[i];
    }

    vector<int> PP;
    PP.push_back(0);
    for (int i = 0; i < N; i++) {
        PP.push_back(P[i]);
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            PP.push_back(P[i] + P[j]);
        }
    }

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

    int ans = 0;
    for (int i = 0; i < PP.size(); i++) {
        int k = upper_bound(PP.begin(), PP.end(), T - PP[i]) - PP.begin();
        if (k != 0) {
            ans = max(ans, PP[i] + PP[k - 1]);
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(true){
		cin>>n>>m;
		if(n==0)	return 0;
		vector<int> p(n);
		for(int i=0;i<n;i++)	cin>>p[i];
		p.push_back(0);
		set<int> s;
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(auto it=s.begin();it!=s.end();it++){
			if(*it>=m)	break;
			auto it2=s.lower_bound(m-*it);
			while(it2!=s.begin()&&*it2+*it>=m)	it2--;
			if(*it+*it2<m)	ans=max(ans,*it+*it2);
		}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>//JOI 2007 ??¬??? 3??????
using namespace std;
int max_p[200000001];
int p[1001];
int main ()
{
	long n,m;
	long i,j;
	int r;
	while(cin >> n >> m,n)
	{
		for(i=0;i<=m;i++)
		{
			max_p[i]=0;
		}
		for(i=0;i<n;i++)
		{
			cin >> p[i];
		}
		max_p[0]=1;
		for(i=0;i<m;i++)
		{
			if(max_p[i]>=1 && max_p[i]<5)
			{
				r=max_p[i];
				for(j=0;j<n;j++)
				{
					if(i+p[j]<=m)
					{
						if(max_p[i+p[j]]==0 || r+1<max_p[i+p[j]])
						{
							max_p[i+p[j]]=r+1;
						}
					}
				}
			}
		}
		for(i=m;i>=0;i--)
		{
			if(max_p[i]!=0)
			{
				cout <<  i << endl;
				break;
			}
		}
	}
	return 0;
}