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<cstdio>
#include<math.h>
#include<algorithm>
#include<string.h>

using namespace std;
const int INF =1001001001;
#define rep(i,n) for(int i=0;i<n;i++)
const int MAX_T = 1002*1002;
int M;



int main(){
	int N,M;
	int P[1002];
	scanf("%d %d",&N,&M);
	do{
		rep(i,N)scanf("%d",&P[i]);
		P[N]=0;//投げない場合
		//2回の合計の場合をだす
		int P2[MAX_T];
		int T=-1;
		rep(i,N+1)rep(j,N+1)P2[++T]=P[i]+P[j];
		sort(P2,P2+T);
		//二分探索
		int ans=0;
		rep(i,T){
			int q=M-P2[i];
			int a=0,b=T;
			if(q<0)continue;
			else{
				while(b-a>1){
				int c=(b+a+1)/2;
				if(P2[c]>q){b=c;}
				else{a=c;}
				}
				ans=max(P2[i]+P2[a],ans);					
			}
		}
		printf("%d\n",ans);
		scanf("%d %d",&N,&M);
	}while(N!=0||M!=0);
	return 0;
}

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


using namespace std;

int main(){
    while(true){
        int N,M;
        cin >> N >> M;
        if(N == 0 and M == 0) break;
        const int C = 4;
        vector<set<int>> ps(C+1);
        ps[0].insert(0);
        for(int i=0;i<N;i++){
            int p;cin >> p;
            for(int j=0;j<C;j++){
                for(auto k : ps[j]){
                    if(k+p <= M){
                        ps[j+1].insert(k+p);
                    }
                }
            }
        }

        int ret = 0;
        for(int i=0;i<C+1;i++){
            for(auto k : ps[i]){
                ret = max(ret,k);
            }
        }
        cout << ret << endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int n,lim;
int point[1010] = {0};
int f(int now,int mem,int maxp)
{
    //printf("%d %d %d\n",now,mem,maxp);
    int sum = 0;
    if(now == n)return maxp;
    for(int i = mem; i < 5 && lim >= maxp + point[now] * (i - mem); i++)
    {
        sum = max(sum,f(now + 1,i,maxp + point[now] * (i - mem)));
    }
    return sum;
}
int main()
{
    while(scanf("%d%d",&n,&lim),n + lim)
    {
        for(int i = 0; i < n; i++)scanf("%d",&point[i]);
        printf("%d\n",f(0,0,0));
    }
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;

int p[1001];
int h[1000*1000+1];

int main(){
	int n,m,ans;
	while(cin>>n>>m){
		if(n==0&&m==0) break;
		p[0]=0;
		ans=0;
		for(int i=1; i<=n; ++i){
			cin >> p[i];
		}
		for(int i=0; i<=n; ++i){	
			for(int j=0; j<=n; ++j){
				h[i*n+j]=p[i]+p[j];
			}
		}
		sort(h,h+n*n+1);
		/*
		for(int i=0; i<=n*n; ++i){
			cout << h[i] << "\n";
		}
		*/
		for(int i=0; i<=n; ++i){	
			for(int j=0; j<=n; ++j){	
				int tmp=p[i]+p[j];
				int l=0,r=n*n;
				int cnt=0;
				while(1){
					int mid=(r+l)/2;
					if(cnt>=80){
						if(h[mid]+tmp<=m) ans=max(ans,h[mid]+tmp);
						break;
					}
					if(h[mid]+tmp>m){
						r=mid;
					} else if(h[mid]+tmp<m){
						l=mid;
					} else if(h[mid]+tmp==m){
						ans=m;
						break;
					}
					++cnt;
				}
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstring>
#include <tuple>
#include <utility>
#include <list>
#include <cmath>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const LL LINF = 1LL << 60;
const LL MOD = 100000;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define all(c) (c).begin(), (c).end()

struct edge{ LL to, cost; edge(int t, LL c) :to(t), cost(c){} };

int dxk[] = { 1, 1, 1, 0, -1, 0 };
int dxg[] = { 0, 1, 0, -1, -1, -1 };
int dy[] = { -1, 0, 1, 1, 0, -1 };

int main()
{
	int n;
	LL m;
	vector<LL> anses;
	while (cin >> n >> m && n)
	{
		vector<LL> nums(n);
		rep(i, n) cin >> nums[i];
		vector<LL> arr(n*n + n);
		rep(i, n) rep(j, n)
		{
			arr[i * n + j] = nums[i] + nums[j];
		}
		rep(i, n) arr[n*n + i] = nums[i];
		sort(all(arr));

		LL ans = 0;
		for (auto x : arr)
		{
			LL rem = m - x;
			auto it = upper_bound(all(arr), rem);
			if (it != arr.begin())
			{
				LL num = *(--it);
				ans = max(ans, num + x);
			}
		}
		cout << ans << endl;
		anses.push_back(ans);
	}
	cout << "anses" << endl;
	for (auto x : anses) cout << x << endl;
	cin >> n;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
/*bool ok(int *q,int m,int h,int count){
  for(int i=0;i<count;i++){
    if(q[i]>m) continue;
    for(int j=0;j<count;j++){
      if(q[i]+q[j]<=m&&q[i]+q[j]>h) return true;
    }
  }
  return false;
  }*/
int main(){
  int n,m;
  while(cin>>n>>m){
    if(n==0&&m==0) return 0;
    int data[n+1];
    int q[(n+1)*(n+1)/2];
    int ans = 0;
    for(int i=0;i<n;i++) cin >> data[i];
    data[n] = 0;
    int count = 0;
    for(int i=0;i<=n;i++){
      for(int j=i;j<=n;j++){
	if(data[i]+data[j]<=m) q[count] = data[i] + data[j];
	count++;
      }
    }
    //sort(q,q+count);
    for(int i=0;i<count;i++){
      for(int j=0;j<count;j++) if(q[i]+q[j]>ans&&q[i]+q[j]<=m) ans = q[i] + q[j];}
    cout << ans << endl;
  }
}

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

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

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

int main(){
  
  int n, m, p[1111];
  int sum[111111];

  while(cin >> n >> m, n||m){
    for(int i = 0; i < n; i++) cin >> p[i];
    
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        sum[i * n + j] = p[i] + p[j];
      }
    }
    
    int maxi = 0;
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        if(sum[i * n + j] <= m){
          maxi = max(maxi, sum[i * n + j]);
          if(sum[i * n + j] + p[i] + p[j] <= m)
            maxi = max(maxi, sum[i * n + j] + p[i] + p[j]);
          if(sum[i * n + j] + p[i] <= m)
            maxi = max(maxi, sum[i * n + j] + p[i]);
          if(sum[i * n + j] + p[j] <= m)
            maxi = max(maxi, sum[i * n + j] + p[j]);
        }
      }
    }
    cout << maxi << endl;
  }
}