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 <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int N, M;
vector<int> P;

int solve(){
    vector<int> sums(N*N+N+2);
    for(int i=0; i<N; ++i){
        for(int j=0; j<N; ++j){
            sums[i*N+j]=P[i]+P[j];
        }
    }
    for(int i=0; i<N; ++i){
        sums[N*N+i]=P[i];
    }
    sums[N*N+N]=0;
    sums[N*N+N+1]=INT_MAX;
    sort(sums.begin(), sums.end());

    int ans;
    auto p=lower_bound(sums.begin(), sums.end(), M);
    if(*p==M) return M;
    else ans=*(p-1);

    for(int i=0; i<N; ++i){
        for(int j=0; j<N; ++j){
            int rest=M-P[i]-P[j];
            if(rest==0) return M;
            else if(rest<0);
            else{
                auto p=lower_bound(sums.begin(), sums.end(), rest);
                if(*p==rest) return M;
                else ans=max(ans, *(p-1)+P[i]+P[j]);
            }
        }
    }
    return ans;
}

int main(){
    while(true){
        cin >> N >> M;
        if(N==0 && M==0) break;
        P.resize(N);
        for(int i=0; i<N; ++i){
            cin >> P[i];
        }
        cout << solve() << endl;
    }
    return 0;
}

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

using namespace std;

int main(){
  for(int n,m;cin>>n>>m,n|m;){
    int p[1001];
    for(int i=0;i<n;i++){
      cin>>p[i];
    }
    p[n]=0;
    static int pd[1001*1001];
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	pd[i*(n+1)+j]=p[i]+p[j];
      }
    }
    sort(pd,pd+(n+1)*(n+1));
    int r=0;
    for(int i=0;i<(n+1)*(n+1);i++){
      int x=lower_bound(pd,pd+(n+1)*(n+1),m-pd[i])-pd;
      if(pd[i]+pd[x]==m){
	r=m;
      }else if(x>0){
	r=max(r,pd[i]+pd[x-1]);
      }
    }
    cout<<r<<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 > 102; 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++: #define	_USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <set>
#include <complex>
using namespace std;
int main(){
	
	int n;
	long long int m;
	while(cin>>n>>m,n!=0&&m!=0){
	
	vector<long long> v(n+1,0);
	vector<long long> s;

	
	for(int i=1;i<=n;i++)
		cin>>v[i];
		
		for(int i=0;i<=n;i++){
			for(int j=0;j<=n;j++){
				s.push_back(v[i]+v[j]);
			}
		}
	
	
	
	sort(s.begin(),s.end());
	long long ans=0;
		for(int i=0;i<s.size();i++){
	vector<long long>::iterator it=upper_bound(s.begin(),s.end(),m-s[i]);
			if(it!=s.begin()){
				it--;
			ans=max(ans,s[i]+s[it-s.begin()]);
			}
		}
	cout<<ans<<endl;
	}

	
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,m;
	while(true){
		cin>>n>>m;
		if(n==0)	return 0;
		vector<int> p(n);
		for(int i=0;i<n;i++)	cin>>p[i];
		p.push_back(0);	n++;
		set<int> s;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++)	if(p[i]+p[j]<m)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(auto it=s.begin();it!=s.end();it++){
			auto it2=s.lower_bound(*it);
			if(it2!=s.begin())	it2--;
			if(*it+*it2<=m)	ans=max(ans,*it+*it2);
			else 	break;
		}
		cout<<ans<<endl;
	}
}

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

int main(){
	int n, m;
	while(scanf("%d%d", &n, &m), n){
		vector<int> p(n + 1), v;
		v.reserve(1000000);
		for(int i = 1; i <= n; ++i){
			scanf("%d", &p[i]);
			for(int j = 0; j <= i; ++j){
				int s = p[i] + p[j];
				if(s <= m){	v.push_back(s); }
			}
		}
		sort(v.begin(), v.end());

		int ans = 0;
		for(int i = 0; i < v.size(); ++i){
			ans = max(ans, v[i] + upper_bound(v.begin(), v.end(), m - v[i])[-1]);
		}
		printf("%d\n", ans);
	}
}

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

int main(int argc, char *argv[]){
  int n, m;
  while(cin >> n >> m, n){
    int point[n + 1];
    point[0] = 0;
    set<int> a;
    for (int i = 1; i <= n; i++) {
      cin >> point[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a.insert(- point[i] - point[j]);
      }
    }
    int ans = 0;
    for (int i: a){
      ans = max(ans, - i - *(a.upper_bound(- m - i)));
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

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

int main(){
  int i,j,k,l,n,m,a[1000],ans;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    for(i=0;i<n;i++) cin >> a[i];

    sort(a,a+n);
    ans = 0;

    for(i=n-1;i>=0;i--){
      if(a[i] <= m) ans = max(ans,a[i]);

      if(a[i] < m){
	for(j=n-1;j>=0;j--){
	  if(a[i] + a[j] <= m) ans = max(ans,a[i] + a[j]);
	  
	  if(a[i] + a[j] < m){
	    for(k=n-1;k>=0;k--){
	      if(a[i] + a[j] + a[k] <= m) ans = max(ans,a[i] + a[j] + a[k]);
	      
	      if(a[i] + a[j] + a[k] < m){
		for(l=n-1;l>=0;l--){
		  if(a[i] + a[j] + a[k] + a[l] <= m){
		    ans = max(ans,a[i] + a[j] + a[k] + a[l]);
		    break;
		  }
		}
	      }
	    }
	  }
	}
      }
    }

    cout << ans << endl;

  }
  return 0;
}

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 int n,m;
	while(cin>>n>>m,n!=0||m!=0){
	long long int s=0;
		vector<long long int> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
			}
			else
				n--;
		}
		cout<<s<<endl;
	}
	
}