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 _, val := range sums {
			rem := M - val
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, val+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

C++: #include <iostream>
#include <queue>
using namespace std;
const int MAX_M = 100000000;
int n, m, p[1000], save;
bool  dp[MAX_M];
queue <int> que;
int main() {
	while (cin >> n >> m){
		for (int i = 0; i<n; i++){
			cin >> p[i];
		}
		for(int i=0;i<m;i++){
			dp[i]=0;
		}
		dp[0] = 1;
		que.push(0);
		for (int i = 0; i<n; i++){
			dp[p[i]] = 1;
			que.push(p[i]);
		}
		for (int i = 1; i<4; i++){
			save=que.size();
			for (int k=0;k<save;k++){
				for (int j = 0; j < n; j++){
					if (que.front() + p[j] < m){
						dp[que.front() + p[j]] = 1;
						que.push(que.front() + p[j]);
					}
				}
				que.pop();
			}
		}
		for (int i = m - 1; i >= 0; i--){
			if (dp[i] != 0){
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}

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

int N = 0, M = 0;
int points[1001];
int scores[1002001];

bool check(int a, int c){
    if(a >= (N + 2) * (N + 1) / 2 - 1){
        return true;
    }
    return (scores[a] <= c && scores[a + 1] > c);
}

int search2(int a, int b, int c){
    int pib = a + b - b / 2;
    if(check(pib, c)){
        return pib;
    }else if(scores[pib] < c){
        return search2(pib, b / 2, c);
    }else{
        return search2(a, b / 2, c);
    }
}

int main(){
    for(;;){
        for(int i = 0; i < 1001; i++){
            points[i] = 0;
        }
        for(int i = 0; i < 1002001; i++){
            scores[i] = 0;
        }
        if(cin >> N){
            if(N == 0){
                break;
            }
            cin >> M;
            for(int i = 1; i <= N; i++){
                cin >> points[i];
            }
            for(int i = 0; i <= N; i++){
                for(int j = i; j <= N; j++){
                    scores[(2 * N - i + 1) * i / 2 + j] = points[i] + points[j];
                }
            }
            int mxsco = 0;
            sort(scores, scores + ((N + 2) * (N + 1)) / 2);
            for(int i = 0; i < (N + 2) * (N + 1) / 2; i++){
                if(scores[i] > M){
                    break;
                }
                int nosco = scores[i] + scores[search2(0, (N + 2) * (N + 1) / 2, M - scores[i])];
                if(nosco >= mxsco &&  nosco <= M){
                    mxsco = nosco;
                }
            }
            cout << mxsco;
        }else{
            break;
        }
    }
    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, v2;
		
		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]);
				v2.pb(v[i] + v[j]);
			}
		}
		SORT(v1);
		RSORT(v2);
		
		for (int i = 0, j = 0; i < v1.sz && j < v2.sz;){
			if (m < v1[i] + v2[j]) j++;
			else {
				res = max(res, v1[i] + v2[j]);
				i++;
			}
		}
		
		cout << res << endl;
		v.clear();
	}
	
	return 0;
}

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

int main()
{
while(true) {
int n, m;
vector<int> x, xxx;
scanf("%d%d", &n, &m);
if(n == 0)
break;
x.resize(i);
for(int i = 0; i < n; ++i)
scanf("%d", &x[i]);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j)
xxx.push_back(x[i] + x[j]);
}
xxx.push_back(0);
sort(xxx.begin(), xxx.end());
int ans = 0;
for(int i = 0; i < xxx.size(); ++i) {
int r = m - xxx[i];
if(r < 0)
continue;
int idx = upper_bound(xxx.begin(), xxx.end(), r) - xxx.begin() - 1;
if(idx < 0)
continue;
ans = max(ans, r + xxx[idx]);
}
printf("%d\n", ans);
}
return 0;
}