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<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()){
			int *a = (lower_bound(y2.begin(),y2.end(),(m-y2[i]+1))-1);
			maxi= max(maxi,y2[i]+*a);
		}
		printf("%d\n",maxi);
	}
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long n;
	unsigned long long m;
	while(cin>>n>>m,n!=0||m!=0){
		int k=0;
	unsigned long long  s=0;
		vector<unsigned long long> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0&&k!=4){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
				k++;
			}
			else
				n--;
			
		}
		cout<<s<<endl;
	}
	
}

C++: #include<bits/stdc++.h>
using namespace std;
vector<long int> po;
long int n,a,low,m,lastans,memo[1005];
void solve(int dp,long int sum);

int main(){
    while(1){
        lastans=0;
        po.clear();
        low=100000000;

        cin >> n >> m;
        if(n==0&&m==0)break;
        for(int i=0;i<n;i++){
            cin >> a;
            po.push_back(a);
            low=min(low,a);
        }
        solve(0,0);
        cout << lastans << endl;
    }

    return 0;
}

void solve(int dp,long int sum){
    //int ans=0;
    if(dp==n)return ;
    if(low+sum>m)return;
    else{
     for(int i=0;i<n;i++){
        if(sum+po[i]<m){
            lastans=max(lastans,sum+po[i]);
           // ans=max(ans,sum+po[i]);
            solve(dp+1,sum+po[i]);
            }
        }
    }
    return;
}

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

int dp[100000000];

int main(){
	/*int N,M;
	while(cin>>N>>M,N||M){
		int dp[200000001]={},P,ans=0;
		dp[0]=1;
		for(int i=0;i<N;i++){
			cin>>P;
			for(int j=M;j>=0;j--){
				
			}
		}
	}*/
	return 0;
}

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

inline long long f(long long M, long long s)
{
  return s > M ? 0LL : s;
}


int main()
{
  int N;
  long long M;
  while (cin >> N >> M && N != 0) {
    vector<long long> ps(N);
    for (int i = 0; i < N; i++) {
      cin >> ps[i];
    }

    vector<vector<long long> > dp(N, vector<long long>(5, 0));
    for (int j = 0; j < 5; j++) {
      dp[0][j] = f(M, ps[0]*j);
    }

    for (int i = 1; i < N; i++) {
      for (int j = 0; j < 5; j++) {
        for (int k = 0; k <= j; k++) {
          dp[i][j] = max(dp[i][j], f(M, dp[i-1][k] + ps[i]*(j-k)));
        }
      }
    }
    long long ans = 0;
    for (int j = 0; j < 5; j++) {
      ans = max(ans, dp[N-1][j]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>

#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repm(a,b,c) for(int a=(b-1);a>=c;a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d",&a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s){double ans=0;rep(i,0,s.length()){ans+=(s[i]-'0')*pow(10,s.length()-i-1);}return (int)ans;}
inline string numstr(int m){str s="";while(m>0){char c;int a=m/10;if(a>0)a=m%(a*10);else a=m;c=(char)('0'+a);s+=c;m/=10;}str st="";for(int i=s.size()-1;i>=0;i--){st+=s[i];}return st;}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
int cost[1002001];
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m,d[1001];
    while(1){
    cin >> n >> m;
    if(n==0&&m==0)break;
    int ans=0;
    rep(i,0,n){
        cin >> d[i];
        if(ans<d[i]&&d[i]<=m)ans=d[i];
    }
    d[n]=0;
    int cou=0;
    rep(i,0,n+1){
        rep(j,0,n+1){
            cost[cou]=d[i]+d[j];
            if(cost[cou]>ans&&cost[cou]<=m)ans=cost[cou];
            cou++;
        }
    }
    sort(cost,cost+cou);
    int left=0;
    int right=cou;
    rep(i,0,cou){
        while((cost[i]+cost[right-1])>m)right--;
        int a=cost[i]+cost[right-1];
        if(right<i)break;
        //cout << right << " " << a << endl;
        if(a<=m)ans=max(a,ans);
    }
    cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int N, M;
int P[1001];
int PP[1002001];
int ans=0;
int main()
{
	while (true){
		ans = 0;
		scanf("%d%d", &N, &M);
		if (N == 0 && M == 0)break;
		for (int i = 0; i < N; i++)scanf("%d", &P[i]);
		P[N] = 0;
		for (int i = 0; i <= N; i++)for (int j = 0; j <= N; j++)PP[i*(N + 1) + j] = P[i] + P[j];
		sort(PP, PP + (N + 1)*(N + 1));
		int l = 0, r = (N + 1)*(N + 1) - 1;
		while (r >= l){
			int sum = PP[l] + PP[r];
			if (sum <= M){
				ans = max(ans, sum);
				l++;
			}
			else r--;

		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 1002

int p[MAX], dp[MAX];
int main(){
	int n, m, ans;
	while(cin >> n >> m, n){
		fill(dp, dp+MAX, 0);
		for(int i = 0;i < n;i++)cin >> p[i];
		ans = 0;
		
		for(int i = 0;i < 3;i++){
			dp[0] = 5;
			for(int j = p[i];j <= m;j++){
				dp[j] = max(dp[j], dp[j-p[i]]-1);
				if(dp[j])ans = max(ans, j);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: //Solution for AOJ 0529: Darts
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 1000, MAX_M = 200000000;
int n, m;
int p[MAX_N + 1], p2[MAX_N*MAX_N], ans, roop, s, mid;
void search(int end, int start){
	if ((end + start) % 2)
		mid = (end + start) / 2 + 1;
	else
		mid = (end + start) / 2;
	if (p2[mid] == s)
		return;
	if (end == start){
		if (p2[mid] > s)
			mid--;
		return;
	}
	if (p2[mid] < s){
		if (end - mid == 1){
			if (p2[end]<s)
				mid++;
			return;
		}
		search(end, mid);
	}
	else{
		if (mid - start == 1){
			mid--;
			return;
		}
		search(mid, start);
	}
}
int main(){
	while (cin >> n >> m){
		ans = 0;
		for (int i = 1; i <= n + 1; i++){
			cin >> p[i];
		}
		roop = 0;
		for (int i = 0; i <= n + 1; i++){
			for (int j = i; j <= n + 1; j++){
				if (p[i] + p[j] <= m){
					p2[roop] = p[i] + p[j];
					roop++;
				}
			}
		}
		sort(p2, p2 + roop);
		for (int i = 0; i < roop; i++){
			s = m - p2[i];
			search(roop - 1, 0);
			ans = max(ans, p2[i] + p2[mid]);
		}
		cout << ans << endl;
		return 0;
	}
}