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 <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 = 100;
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);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

vector<vector<ll>> dp;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n >> m && n && m){
    vary(ll,s,n,0);
    dp = vector<vector<ll>>(n+2,vector<ll>(m+2,inf));
    rep(i,n){
      cin >> s[i];
    }
    sort(all(s));
    rep(i,m+1){
      dp[0][i] = 0;
    }
    rep(i,n+1){
      dp[i][0] = 0;
    }
    REP(i,0,n){
      for(ll j = 0; j < m+1 + s[i]; j += s[i]){
        if(j - s[i] >= 0){
          dp[i+1][j] = min(dp[i+1][j],dp[i][j-s[i]]+1);
        }
      }
      if(i > 0){
        rep(j,m+1){
          dp[i+1][j] = min(dp[i][j],dp[i+1][j]);
        }
      }
    }
    ll ans = 0;
    for(ll j = n; j >= 0; --j){
      for(ll i = m; i >= 0; --i){
        if(dp[j][i] != inf && dp[j][i] != 0 && dp[j][i] <= m){
          ans = i;
          j = -1;
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define MAX_N 1010
#define pb push_back

int main(){
  int N, M;
  
  while(cin >> N >> M){
    if(N == 0 && M == 0) break;
    vector<int> P(N), P2;

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

    P2.pb(0);
    for(int i = 0 ; i < N ; i++){
      for(int j = 0 ; j < N ; j++){
	P2.pb(P[i]+P[j]);
      }
    }

    sort(P2.begin(), P2.end());
    
    int ans = 0;
    for(int i = 0 ; i < P2.size() ; i++){
      if(M >= P2[i]) ans = max(ans, P2[i]+upper_bound(P2.begin(), P2.end(), M-P2[i])[-1]);
    }
    cout << ans << endl;
  }
  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(n);
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, xxx[i] + xxx[idx]);
}
printf("%d\n", ans);
}
return 0;
}

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

using namespace std;

int main() {
	long long int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector<long long int> data;
		long long int b;
		data.push_back(0);
		for (long long int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				bool hantei = true;
				for (long long int j = 0; j < data.size(); j++) {
					if (data[j] == b) {
						hantei = false;
						break;
					}
				}
				data.push_back(b);
			}
		}
		sort(data.begin(), data.end());
		vector<long long int> data2;
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		for (long long int i = 0; i < data2.size(); i++) {
			long long int a = data2[i];
			data.push_back(a);
			for (long long int j = i+1; j < data2.size(); j++) {
				if (data2[j] == a) {
					i++;
				} else {
					break;
				}
			}
		}
		data2.clear();
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		long long int sz = data2.size();
		cout << data2[sz-1] << endl;
	}
	return 0;
}

C++: 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	int ans[4];
	int temp;
	double energy,energy_min,energy_now;
	int M, N;
	int* p;
	int i, j;
	double T=100;
	int ret;
	while (1) {
		scanf("%d%d", &N, &M);
		p = (int*)malloc((N+1) * sizeof(int));
		for (i = 0; i < N; i++) {
			scanf("%d", &p[i]);
		}
		p[N] = 0;
		if (N == M && N == 0) {
			break;
		}
		energy_min = pow(10, 300);
		for (j = 0; j < 4; j++) {
			ans[j] = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
		}
		
		for (T = 1000000; T > 100; T--) {
			for (j = 0; j < 4; j++) {
				temp = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
				for (j = 0; j < 4; j++) {
					ret = ans[0] + ans[1] + ans[2] + ans[3];
					ret += temp - ans[j];
					energy = M - ret;
					if (M < ret)energy += 100000000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*10)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

int main() {
	int N, M; cin >> N >> M;
	static int dp[200000001];
	memset(dp, 5, sizeof(dp));
	dp[0] = 0;

	for (int i = 0; i < N; i++) {
		int P; cin >> P;
		for (int j = 0; j <= M; j++) {
			if (dp[j] >= 0)
				dp[j + P] = min(
					dp[j + P],
					dp[j] + 1
				);
		}
	}
	for (int j = M; j >= 0; j--) {
		if (dp[j] >= 0 && dp[j] <= 4) {
			cout << j<< endl;
			break;
		}
	}
	return 0;
}