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+j] = arr[i] + arr[j]
			}
		}
		sort.Slice(sums, func(i, j int) bool { return sums[i] < sums[j] })

		var m int64 = 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<vector>
#include<algorithm>
#define REP(i,a,n) for(int i = a;i < n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;
int in(){int x; scanf("%d",&x); return x;}
int main(void){
   int n,m;
   while(scanf("%d%d",&n,&m),n+m){
      vector<int>_point(0);
      vector<int>point(0);
      point.push_back(0);
      _point.push_back(0);
      rep(i,n){
         int x; scanf("%d",&x);
         _point.push_back(x);
         for(int j = _point.size()-1;j >= 0;j--){
            point.push_back(x+_point[j]);
         }
      }

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

      int ma = 0;

      for(int i = 0;point[i] <= m;i++){
         if(i >= point.size()) break;

         vector<int>::iterator idx = upper_bound(point.begin(),point.end(),m-point[i]);
         if(*idx == 0) continue;
         idx -= 1;
         int num = *idx;
         ma = max(ma,point[i]+num);
      }
      printf("%d\n",ma);
   }
   return 0;
}

C++: #include<iostream>
#include<algorithm>
#define MAXN 1000

using namespace std;

int N,M;
int pi[MAXN];

bool comp(int i,int j)
{
	return i>j;
}

void solve()
{
	int temp=4;
	int res=0;
	int grade=M;
	sort(pi,pi+N,comp);
	for(int i=0;i<N;i++)
	{
		while(grade>=pi[i]&&temp>0)
		{
			grade-=pi[i];
			temp--;
			res+=pi[i];
		}
		if(temp==0)break;
	}
	cout<<res<<endl;
}

void input()
{
	while(scanf("%d%d",&N,&M))
	{
		if(N==0&&M==0)break;
		for(int i=0;i<N;i++)
		{
			scanf("%d",&pi[i]);
		}
		solve();
	}
}

int main()
{
	input();
	return 0;
}

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

int binary(vector<int> &a, int tar){
  int begin = 0, end = a.size() - 1, mid = (begin + end)/2;
  while(begin < mid){
    if(a[mid] < tar){
      begin = mid;
    }else{
      end = mid;
    }
    mid = (begin + end)/2;
  }
  return a[mid];
}


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;
    vector<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.push_back(point[i] + point[j]);
      }
    }
    sort(a.begin(), a.end());
    int ans = 0;
    for (int i: a){
      int tmp = binary(a, m - i);
      if(i + tmp <= m)
        ans = max(ans, i + tmp);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n, m, c[1009], s[510009], t[510009], cnt[32777], sum[32777];
int main() {
	while (scanf("%d%d", &n, &m), n) {
		for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
		sort(c, c + n);
		int b = 0;
		for (int i = 0; i <= n; i++) {
			for (int j = i; j <= n && c[i] + c[j] <= m; j++) {
				s[b++] = c[i] + c[j];
			}
		}
		for (int i = 0; i < 30; i += 15) {
			fill(cnt, cnt + 32768, 0); sum[0] = 0;
			for (int j = 0; j < b; j++) cnt[(s[j] >> i) & 32767]++;
			for (int j = 0; j < 32768; j++) sum[j + 1] = sum[j] + cnt[j];
			for (int j = 0; j < b; j++) t[sum[(s[j] >> i) & 32767]++] = s[j];
			for (int j = 0; j < b; j++) s[j] = t[j];
		}
		int r = b - 1, ret = 0;
		for (int i = 0; i < b && i <= r; i++) {
			while (r > 0 && s[i] + s[r] > m) r--;
			ret = max(ret, s[i] + s[r]);
		}
		printf("%d\n", ret);
	}
	return 0;
}

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		long long int b;
		set<long long int> ss;
		ss.insert(0);
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				ss.insert(b);
			}
		}
		set<long long int> ss2;
		for (set<long long int>::iterator ite = ss.begin(); ite != ss.end(); ite++) {
			ss2.insert(*(ite));
			for (set<long long int>::iterator ite2 = ite; ite2 != ss.end(); ite2++) {
				if (*(ite)+*(ite2) <= m) {
					ss2.insert(*(ite)+*(ite2));
				} else {
					break;
				}
			}
		}
		ss.clear();
		long long int a = m;
		for (long long int i = 0; i < 4; i++) {
			set<long long int>::iterator ite = upper_bound(ss2.begin(), ss2.end(), a);
			ite--;
			a -= *(ite);
		}
		cout << m-a << endl;
	}
	return 0;
}

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

const auto gcu = getchar_unlocked;
const auto pcu = putchar_unlocked;
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _sc _il bool scan
_T T in(int c){T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_il int in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _ot _il void out
#define _ol _il void outl
_ot(bool b) {pcu('0'+b);}
_ot(const char *s){while(*s)pcu(*s++);}
_ot(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_ot(string s){for(char c:s)pcu(c);}
#endif
_T _ot(T n){static char b[20];char *p=b,m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=n%10*m+'0',n/=10;while(p!=b)pcu(*--p);}
_ol(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _ot(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _ot(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _ol(T&&... t){out(move(t)...);outl();}

int main() {
	for (int N, M; N = in(), M = in();) {
		vector<int> t(N), u;
		for (int &p: t)
			p = in();
		t.push_back(0);
		sort(t.begin(), t.end());
		for (auto i = t.begin(); i != t.end(); i++)
			for (auto j = i; j != t.end(); j++)
				if (*i + *j <= M)
					u.push_back(*i + *j);
		sort(u.begin(), u.end());
		u.erase(unique(u.begin(), u.end()), u.end());
		int r = 0;
		for (auto i = u.begin(), e = u.end() - 1; i <= e; i++) {
			for (; *i + *e >= M; e--)
				;
			if (*i + *e > r)
				r = *i + *e;
		}
		outl(r);
	}
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
ll n, m;
ll dp[1001][4], p[1001];
int main() {
    while (scanf("%lld%lld",&n,&m)) {
        if (!n&&!m) break;
        for (int i=0; i<n; i++) scanf("%lld",&p[i]);
        for (int i=0; i<4; i++) {
            if ((i+1)*p[0]>m&&i) {
                dp[0][i]=dp[0][i-1];
            } else if ((i+1)*p[0]>m) {
                dp[0][i]=0;
            } else {
                dp[0][i]=(i+1)*p[0];
            }
        }
        for (int i=1; i<n; i++) {
            for (int j=0; j<4; j++) {
                dp[i][j]=0;
                for (int k=0; k<=j; k++) {
                    if (dp[i-1][j-k]+k*p[i]<=m) {
                        dp[i][j]=max(dp[i][j],dp[i-1][j-k]+k*p[i]);
                    }
                }
            }
        }
        printf("%lld\n",dp[n-1][3]);
    }
}