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>
using namespace std;
 
int main() {
	for(;;){
		/*入力*/
		int N,M,count=0;
		cin >> N >> M;
		if(N==0 && M==0)exit(0);
		int P[N+1],array[M+1]={0};
		P[0]=0;
		for(int i=1;i<=N;i++)cin >> P[i];
		for(int i=0;i<=N;i++)array[P[i]]=1;
 
		/*２投(０投１投を含む)で1からMまでが可能か判定*/
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				if(P[i]+P[j] <= M)array[P[i]+P[j]]=1;
			}
		}
 
		/*４投に拡張*/
		for(int i=0;i<=M;i++){
			if(array[i]==1)count++;
		}
		int k=0,p[count];
		for(int i=0;i<=M;i++){
			if(array[i]==1){
				p[k]=i;
				k++;
			}
		}
		for(int i=0;i<count;i++){
			for(int j=0;j<count;j++){
				if(p[i]+p[j] <= M)array[p[i]+p[j]]=1;
			}
		}
 
		/*判定出力*/
		for(int i=M;i>0;i--){
			if(array[i]==1){
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}

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

int main()
{
	int n, m;
	int point[1000 + 10] = {0};
	vector<int> sec;

	int sum;

	int i, j;

	while (cin >> n >> m, n != 0 && m != 0)
	{
		sec.clear();
		sum = 0;
		for (i = 1; i <= n; i++)
		{
			cin >> point[i];
		}

		for (i = 0; i <= n; i++)
		{
			for (j = i; j <= n; j++)
			{
				sec.push_back(point[i]+point[j]);
			}
		}

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

		for (i = sec.size() - 1; i >= 0; i--)
		{
			for (j = i; j >= 0; j--)
			{
				if (sec[i] + sec[j] <= m)
				{
					sum = max(sum, sec[i] + sec[j]);
					break;
				}
			}
		}

		cout << sum << endl;
	}

	return 0;
}

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


#define rep(X, Y) for(int (X) = 0; (X) < (Y); ++(X))
#define MAX 1001

typedef long long ll;

int n;
ll m, b[MAX], h[MAX*MAX], res = -1;

int main(){
	while(scanf("%d%lld", &n, &m), n|m){

		rep(i, n){
			scanf("%lld", b + i + 1);
		}
		++n;

		rep(i, n){
			rep(l, n){
				h[i * n + l] = b[i] + b[l];
			}
		}

		std::sort(h, h + n * n);

		res = -1;
		rep(i, n*n){
			res = std::max(res, *(std::upper_bound(h, h + n * n, m - h[i]) - 1) + h[i]);
		}

		printf("%lld\n", res);
	}

	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
	int n,m,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&P[i]);
		}
		for(int i = 0; i < n; i++){
			for(int j = i; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			if(P[i] > m){
				break;
			}
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			MAX = max(MAX,P[i] + *it);
		}
	
		printf("%d\n",MAX);
	}
	
	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;
	n++;
	vector<ll> dp[2];
	vector<ll> a(n);
	a[0] = 0;
	FOR(i,1,n){
	    cin >> a[i];
	}
	ll ans = 0;
	FOR(i,0,n)FOR(j,i,n)FOR(k,j,n)FOR(l,k,n){
	    if(a[i]+a[j]+a[k]+a[l] <= m) ans = max(ans,a[i]+a[j]+a[k]+a[l]);
	}
	cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

void solve(int N, int M, int P[1010])
{
	int score = 0;
	sort(P, P + N, greater<int>());
	
	for(int j = 0; j < 4; j++){		
		int i = 0;
		while(true){
			if(score + P[i] > M){
				i++;
			}
			else{
				score += P[i];
				break;
			}
		}
	}
	
	cout << score << endl;
}


int main()
{
		int N, M;
		int P[1010];

	while(true){
		cin >> N;
		cin >> M;
		if(N == 0 && M == 0) break;

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

		solve(N, M, P);
	}

	return 0;
}

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

int n;
long m, p[1001];
long dp[6][1001];

long rec(int a, int b){ //a本目，最後に当てた位置b
	if(dp[a][b]>=0) return dp[a][b];
	
	long ret=0;
	
	if(a==4) ret=0;
	else{
		for(int i=b; i<=n; ++i){
			long tmp=rec(a+1, i)+p[i];
			if(tmp>m) tmp=0;	
			
			if(ret<tmp) ret=tmp;
		}		
	}
	
	return dp[a][b]=ret;
}

int main(){
	while(1){
		scanf(" %d %ld", &n, &m);
		if(n==0) break;
		p[0]=0; //投げない選択肢
		for(int i=1; i<=n; ++i) scanf(" %ld", &p[i]);
		sort(p, p+n);
		
		memset(dp, -1, sizeof(dp));
		printf("%ld\n", rec(0,0));
	}
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <set>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	ll N, M;
	ll P[1010];
	ll res = 0;
	vector<int> two;
	cin >> N >> M;
	REP(i, N){
		cin >> P[i];
	}
	P[N] = 0;
	REP(i, N+1){
		REP(j, N+1){
			if(P[i]+P[j] <= M)
				two.push_back(P[i]+P[j]);
		}
	}
	sort(all(two));
	REP(i, N+1){
		REP(j, N+1){
			ll a = P[i]+P[j];
			if(a > M) continue;
			res = max(res, a+(*(upper_bound(all(two), M-a)-1)));
		}
	}
	cout << res << endl;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

#define f first
#define s second

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		
		if(n==0)break;
		
		vector<int> ya;
		ya.push_back(0);
		
		rep(i,n){
			int a;
			cin>>a;
			ya.push_back(a);
		}
		
		vector<int> y2;
		rep(i,ya.size()){
			rep(j,ya.size()){
				y2.push_back(ya[i]+ya[j]);
			}
		}
		
		sort(y2.begin(),y2.end());
		
		
		int maxi=0;
		
		rep(i,y2.size()){
			vector<int>::iterator a = lower_bound(y2.begin(),y2.end(),(m-y2[i]+1));
			if(a==y2.begin())continue;
			//printf("%d %d\n",y2[i],*(a-1));
			maxi= max(maxi,y2[i]+*(a-1));
		}
		printf("%d\n",maxi);
	}
}