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 <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n, m;
vector<int> p;
int t;
vector<int> q;
vector<int> o;
int ans;

int b(int x){
	int l = 0;
	int r = q.size();
	while(r - l > 1){
		int i = (l + r) / 2;
//		printf("l=%d, r=%d, i=%d, q[i]=%d, x=%d\n", l, r, i, q[i], x);
		if(q[i] < x){
			l = i;
		}else{
			r = i;
		}
	}
//	printf("%d\n", q[l]);
	return q[l];
}

int main(){
	while(1){
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;
		p = o;
		p.push_back(0);
		for(int i=0; i<n; i++){
			scanf("%d", &t);
			p.push_back(t);
		}
		q = o;

		for(int i=0; i<p.size(); i++){
			for(int j=0; j<p.size(); j++){
				q.push_back(p[i] + p[j]);
			}
		}
		sort(q.begin(), q.end());
/*
		for(int i=0; i<q.size(); i++)
			printf("%d\n", q[i]);
*/
		for(int i=0; i<q.size(); i++){
			int a = q[i];
			if(m - a < 0) continue;
			ans = max(ans, b(m - a) + a);
//			printf("%d\n", b(m - a));
		}

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

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 1000000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

ll n,m;
vector<vector<ll>> dp(5,vector<ll>(1001,0));
vary(ll,s,1001,0);
ll dfs(ll cnt, ll sum){
  if(dp[cnt][sum] != inf || cnt == 4) return sum;
  dp[cnt][sum] = 1;
  ll res = sum;
  rep(i,n){
    if(sum + s[i] <= m){
      res = max(res,dfs(cnt+1,sum + s[i]));
    }
  }
  return res;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(cin >> n >> m && n && m){
    rep(i,n){
      cin >> s[i];
    }
    sort(s.begin(),s.begin()+n);
    rep(i,5){
      fill(all(dp[i]),inf);
    }
    cout << dfs(0,0) << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  int t[n];
  for(int i=0;i<n;i++){
    cin>>t[i];
  }
  int ans=0;
  int cnt=0;
  sort(t,t+n);
  for(int i=n-1;i>=0;){
    if(cnt>3){
      break;
    }
    if(ans+t[i]>=m){
      i--;
      continue;
    }
    cnt++;
    ans+=t[i];
    //cout<<cnt<<endl;
    //cout<<"ans "<<ans<<endl;
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>

using namespace std;
int main(){
	int n,m,ans;
	int dp[1001][4];
	for(;;){
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)break;
		for(int i=0;i<1001;i++)for(int j=0;j<4;j++)dp[i][j]=0;
		for(int i=0;i<n;i++)scanf("%d",&dp[i][0]);
		//sort(p,p+n);
		//reverse(p,p+n);
		ans = 0;
		for(int i=0;i<n;i++){
			for(int j=1;j<4;j++){
				for(int k=0;k<=i;k++)
					for(int l=0;l<j;l++)
						if(dp[k][l]+dp[i][0] <= m)
							dp[i][j]=max(dp[i][j],dp[k][l]+dp[i][0]);
				ans = max(ans,dp[i][j]);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

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 < n;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] < dp[j-p[i]-1])dp[j] = dp[j-p[i]]-1;
				if(dp[j])ans = max(ans, j);
				//if(dp[j] && ans < j)ans = j;
			}
		}
		//for(int i = 1;i <= 50;i++)cout << dp[i] << endl;
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	while(true){
		int n,m;
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)return 0;
		int p[1001];
		for(int i=0;i<n;i++)scanf("%d",&p[i]);
		p[n]=0;
		n++;

		int ans=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				for(int k=0;k<n;k++){
					for(int l=0;l<n;l++){
						if(p[i]+p[j]+p[k]+p[l]<=m)ans=max(ans,p[i]+p[j]+p[k]+p[l]);
					}
				}
			}
		}

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

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;

int n, m;
int nearabs(int x, int y) {
	if (x > m)return y;
	if (y > m)return x;

	if (m - x > m - y)return y;
	else return y;
}
signed main() {
	while (cin >> n >> m, n || m) {
		vector<vector<int>>dp(n + 1, vector<int>(n + 1, 0));
		vector<int>v(n + 1); rep(i, 1, n + 1)cin >> v[i];
		
		rep(i, 1, n + 1) {
			rep(j, 1, n + 1) {
				if (dp[i][j - 1] + v[i] < m)dp[i][j] = max(dp[i - 1][j], dp[i][j - 1] + v[i]);
				else {
					rep(k, 1, i + 1) {
						if (dp[i][j - 1] + v[k] < m)dp[i][j] = max(dp[i][j], dp[i][j - 1] + v[k]);
					}
				}
			}
		}
		cout << dp[n][n] << endl;

	}
	return 0;
}