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 <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);
		set<int> s;
		for(int i=0;i<n+1;i++){
			for(int j=0;j<n+1;j++)	s.insert(p[i]+p[j]);
		}
		int ans=0;
		for(auto it=s.begin();it!=s.end();it++){
			if(*it>=m)	break;
			auto it2=s.lower_bound(m-*it);
			while(it2!=s.begin()&&*it2+*it>=m)	it2--;
			if(*it+*it2<m)	ans=max(ans,*it+*it2);
		}
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
/*bool ok(int *q,int m,int h,int count){
  for(int i=0;i<count;i++){
    if(q[i]>m) continue;
    for(int j=0;j<count;j++){
      if(q[i]+q[j]<=m&&q[i]+q[j]>h) return true;
    }
  }
  return false;
  }*/
int main(){
  int n,m;
  while(cin>>n>>m){
    if(n==0&&m==0) return 0;
    int data[n+1];
    int q[(n+1)*(n+1)/2];
    int ans = 0;
    for(int i=0;i<n;i++) cin >> data[i];
    data[n] = 0;
    int count = 0;
    for(int i=0;i<=n;i++){
      for(int j=i;j<=n;j++){
	if(data[i]+data[j]>m) continue; 
	  q[count] = data[i] + data[j];
	  count++;
      }
    }
    for(int i=0;i<count;i++){
      for(int j=0;j<count;j++){
	if(q[i]+q[j]>m) continue;
	ans = max(ans,q[i]+q[j]);
      }
    }
      //sort(q,q+count);
    cout << ans << endl;
  }
}

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

using namespace std;
using LL = long long;

int main() {
    int N, M, P[1002];

    while (cin >> N >> M) {
        if ((N | M) == 0) break;

        P[0] = 0;
        for (int i = 1; i <= N; i++) cin >> P[i];

        vector<LL> S;
        for (int i = 0; i <= N; i++) {
            for (int j = 0; j <= N; j++) {
                S.push_back(P[i] + P[j]);
            }
        }
        sort(S.begin(), S.end());

        LL ans = 0;
        for (auto s : S) {
            if (M - s > 0) {
                ans = max(ans, s + *(lower_bound(S.begin(), S.end(), M - s) - 1));
            }
        }

        cout << ans << endl;
    }
}

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

using namespace std;

typedef long long  ll;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    ll m;

    while (cin >> n >> m, n != 0 && m != 0) {
        ++n;
        vector<int> p(n);
        p[0] = 0;
        for (int i = 1; i < n; ++i)
            cin >> p[i];

        vector<int> dp(n * n);
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                dp[i * n + j] = p[i] + p[j];

        sort(dp.begin(), dp.end());
        dp.erase(unique(dp.begin(), dp.end()), dp.end());

        n = dp.size();
        ll ans = 0;
        for (int i = 0; i < n; ++i) {
            if (m < dp[i])
                break;
            ll tmp = *(upper_bound(dp.begin(), dp.end(), m - dp[i]) - 1);
            if (tmp + dp[i] <= m)
                ans = max(ans, tmp + dp[i]);
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int p[1001];
int main(){
	int n,m;
	while(true){
	cin>>n>>m;
	if(n+m==0)break;
	for(int i=0;i<n;i++)cin>>p[i];
	p[n]=0;sort(p,p+n+1);
	int ans=0;
	for(int i=0;i<=n;i++){
		for(int j=0;j<=i;j++){
			for(int k=0;k<=j;k++){
				int key=m-p[i]-p[j]-p[k];
				if(key>=0){
				int left=0,right=k+1,mid,cnt=0;
				while(true){
					if(left>=right-1)break;
					mid=(left+right)/2;
					if(p[mid]<=key){cnt=max(cnt,mid);left=mid;}
					else right=mid;
				}
				ans=max(ans,m-key+p[cnt]);
				}
			}
		}
	}
	cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int sum[1000100];

int main(){
  int n,d;
  int m[1111];
  while(cin >> n >> d , n){
    memset(sum,0,sizeof(sum));
    for(int i=0;i<n;i++){
      cin >> m[i];
    }
    m[n] = 0;
    n++;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	sum[i*n+j] = m[i] + m[j];
      }
    }
    sort(sum,sum+n*n);
    
    int res = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	int ab = m[i] + m[j];
	if(m[i] + m[j] <= d){
	  int h = d - (m[i] + m[j]);
	  int tmp = sum[upper_bound(sum,sum+n*n,h)-sum-1] + ab;
	  if(tmp <= d){
	    res = max(res,tmp);
	  }
	}
      }
    }
    cout << res << endl;
  }
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<functional>

long long kazu[1002001];//1001^2
long long n,m,Max,data[1001];

int main(){
	while(true){
		int s=0;
		std::cin>>n>>m;
		if(n==0&&m==0)break;
		data[0]=0;
		for(int i=0;i<n;i++){
			std::cin>>data[i+1];
		}
		for(int c=0;c<n+1;c++){
			for(int d=0;d<n+1;d++){
				kazu[c*n+d]=data[c]+data[d];
			}
		}
		std::sort(kazu,kazu+(n+1)*(n+1));
		for(int a=0;a<n+1;a++){
			for(int b=0;b<n+1;b++){
				while(s<m&&Max<m-s){
					if(std::binary_search(kazu,kazu+((n+1)*(n+1)),m-s-data[a]-data[b])){
						Max=std::max(m-s,Max);
						break;
					}
					s++;
				}
				s=0;
			}
		}
		std::cout<<Max<<std::endl;
		Max=0;
	}
	return 0;
}

C++: #include<iostream>
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 max=0;
  sort(t,t+n);
  for(int i=n-1;i>=0;){
    if(ans+t[i]>=m){
      i--;
      continue;
    }
    ans+=t[i];
    //cout<<"ans "<<ans<<endl;
  }
  cout<<ans<<endl;
  return 0;
}

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

using namespace std;

int main() {
  
  int n, m;
  
  while(cin >> n >> m && (n|m)) {
    int data[1002];
    int Q[1000005];
    
    n++;
    data[0] = 0;
    for(int i=1; i<n; i++) {
      cin >> data[i];
    }
    
    int T = 0;
    
    for(int i=0; i<n; i++) {
      Q[T++] = data[i];
    }
    
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
	Q[T++] = data[i] + data[j];
      }
    }
    
    sort(Q, Q+T);

    // for(int i=0; i<T; i++) cout << Q[i] << endl;
    
    int ans = 0;
    
    for(int i=0; i<T; i++) {
      int M = m - Q[i];
      if(M < 0) break;
      
      int lb = 0, ub = T;
      
      while(ub - lb > 1) {
	int mid = (lb + ub) / 2;
	
	if(Q[mid] <= M) {
	  lb = mid;
	} else {
	  ub = mid;
	}
      }
      ans = max(ans, Q[i]+Q[lb]);
    }
    
    cout << ans << endl;
  }
  
  return 0;
}