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<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long int ll;

int main()
{
	const int MAX_N=105;
    int n,m,p[MAX_N];
	int x[MAX_N*MAX_N];
	while(true){
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)return 0;
		for(int i=0;i<n;i++)scanf("%d",&p[i]);
		p[n]=0;
		n++;

		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				x[i*n+j]=p[i]+p[j];
			}
		}
		sort(x,x+n*n);

		int ans=0;
		for(int i=0;i<n*n;i++){
			int mp=m-x[i];
			if(mp<0)continue;
			int l=0,r=n*n-1;
			int y=0;
			while(r-l>=1){
				int c=(r+l)/2;
				if(x[c]==mp){
					y=x[c];
					break;
				}
				else if(x[c]<mp){
					y=x[c];
					l=c+1;
				}
				else r=c;
			}
			ans=max(ans,y+x[i]);
		}
		printf("%d\n",ans);
	}
    return 0;
}

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

int main(){
  int n, m;
  while(1){
    scanf("%d%d", &n, &m);
    if(n == 0 && m == 0)return 0;
    int res = 0, tmp;
    vector<int> points, points2;
    for(int i = 0;i < n;i++){
      scanf("%d", &tmp);
      if(tmp < m)points.push_back(tmp);
    }
    vector<int>::iterator it1,it2;
    for(it1 = points.begin();it1 != points.end();it1++){
      points2.push_back(*it1);
      for(it2 = points.begin();it2 != points.end();it2++){
	if(*it1 + *it2 <= m)points2.push_back(*it1 + *it2);
      }
    }
    sort(points2.begin(),points2.end());
    for(it1 = points2.begin();it1 != points2.end();it1++){
      it2 = upper_bound(points2.begin(), points2.end(), m - *it1);
      res = max(res, *it1);
      if(it2 == points2.begin())continue;
      it2--;
      res = max(res, *it1 + *it2);
    }
    printf("%d\n",res);
  }
}

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

using namespace std;

int main ( void )
{
	int n, m;
	int p[4];
	set<int> S;
	
	while ( cin >> n >> m, (n | m) )
	{
		int ans = 0;
		S.clear();
		S.insert(0);
		for ( int i = 0; i < n; ++i )
		{
			cin >> p[i];
			if ( p[i] < m )
			{
				S.insert(p[i]);
				ans = max( ans, p[i] );
			}
		}
		
		for ( int i = 0; i < n; ++i )
		{
			for ( int j = i; j < n; ++j )
			{
				int x = p[i] + p[j];
				if ( x <= m )
				{
					S.insert(x);
					ans = max( ans, p[i] );
				}
			}
		}
		
		if ( S.count(m) )
		{
			cout << m << endl;
			continue;
		}
		
		for ( int i = 0; i < n; ++i )
		{
			for ( int j = i; j < n; ++j )
			{
				int x = p[i] + p[j];
				if ( x <= m )
				{
					int y = m - x;
					while ( !S.count(y) && ( ans < (x + y) ) )
					{
						--y;
					}
					ans = max( ans, (x + y) );
				}
			}
		}
		
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
	int n,m,in,MAX;
	vector<int> P;
	vector<int>::iterator it;
	
	while(scanf("%d%d",&n,&m)&&(n!=0||m!=0)){
		P.clear();
		
		for(int i = 0; i < n; i++){
			scanf("%d",&in);
			P.push_back(in);
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				P.push_back(P[i]+P[j]);
			}
		}
	
		P.push_back(0);
		sort( P.begin(),P.end() );
		MAX = 0;
	
		for(int i = 0; i < P.size(); i++){
			if(P[i] >= m){
				break;
			}
			it = upper_bound(P.begin(),P.end(),m-P[i]);
			it--;
			MAX = max(MAX,P[i] + *it);
		}
	
		printf("%d\n",MAX);
	}
	
	return 0;
}

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

#define N 1000
#define M 200000000

typedef long long ll;

#define max(a,b) (a>b?a:b)

int main() {
	ll dp[N+1][5], ans;
	int n, m, a[N+1];
	while(1) {
		cin >> n >> m;
		if(!n&&!m) break;
		for(int i=1; i<=n; ++i) cin >> a[i];
		for(int i=0; i<=n; ++i) {
			for(int j=0; j<5; ++j) {
				dp[i][j] = 0;
			}
		}
		ans = 0;
		for(int i=1; i<=n; ++i) {
			for(int j=1; j<5; ++j) {
				dp[i][j] = dp[i-1][j];
				for(int k=1; k<=j; ++k) {
					if(dp[i-1][j-k]+a[i]*k<=m) {
						dp[i][j] = max(dp[i][j], dp[i-1][j-k]+a[i]*k);
					}
				}
			}
		}
		for(int i=1; i<5; ++i) ans = max(ans, dp[n][i]);
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define INF 200000000
using namespace std;


int point2[500500];

int search(int a[], int n, int key) {
	int left = 0;
	int right = n;
	int mid;
	while (left < right) {
		mid = (left + right) / 2;
		if (a[mid] == key) return a[mid];
		else if (a[mid] < key) left = mid + 1;
		else right = mid;
	}
	return a[left-1];
}

int main() {
	while (true) {
		int n, m;
		cin >> n >> m;
		if (n == 0 && m == 0) return 0;
		int point[n+1];
		for (int i = 0; i < n; i++) cin >> point[i];
		point[n] = 0;
		int k = 0;
		for (int i = 0; i < n + 1; i++) {
			for (int j = i; j < n + 1; j++) {
				point2[k] = point[i] + point[j];
				k++;	
			}
		}
		int np = k;
		sort(point2, point2 + np);

		int ans = 0;
		for (int i = 0; i < np; i++) {
			if (point2[i] > m) break;
			int s = search(point2, np, m - point2[i]);
			int t = s + point2[i];
			if (t > ans) ans = t;
		}
		cout << ans << endl;
	}
}

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

using namespace std;

#define MAX(a,b) ((a)>(b)?(a):(b))

int main()
{
    int N;
    long long M;

    cin >> N >> M;

    while(N!=0 && M!=0) {
        vector<int> p(N);
        set<long long> st[2];

        long long ans=0;

        for(int i=0; i<N; ++i) {
            cin >> p[i];
        }

        sort(p.begin(),p.begin()+N);

        for(int i=0; i<N; ++i) {
            if(p[i]<M) st[0].insert(p[i]);
            ans=MAX(ans,p[i]);
        }

        for(int k=1; k<4; ++k) {
            for(auto itr=st[(k-1)%2].begin(); itr!=st[(k-1)%2].end(); ++itr) {
                auto p_itr=upper_bound(p.begin(),p.end(),M-*itr);
                if(p_itr==p.begin()) {
                    st[(k-1)%2].clear();
                    break;
                } else {
                    --p_itr;
                }
                ans=MAX(ans,*itr+*p_itr);
                st[k%2].insert(*itr+*p_itr);
            }
        }

        cout << ans << endl;


        cin >> N >> M;
    }
    return 0;
}

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

typedef long long int ll;
using namespace std;

int main() {
	int N, M;
	cin >> N >> M;
	vector<ll> P(N);
	vector<ll> Ps;
	Ps.push_back(0);
	for(int i = 0; i < N; i++) {
		cin >> P[i];
		Ps.push_back(P[i]);
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			Ps.push_back(P[i] + P[j]);
		}
	}
	sort(Ps.begin(), Ps.end());

	ll ans = 0;
	for (int i = 0; i < Ps.size(); i++) {
		if (Ps[i] > M) break;
		int k = std::upper_bound(Ps.begin(), Ps.end(), M - Ps[i]) - 1 - Ps.begin();
		ans = max(ans, Ps[i] + Ps[k]);
	}
	cout << ans << endl;



	return 0;
}