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<iostream>
#include<set>
using namespace std;

int main(int argc, char *argv[]){
  int n, m;
  while(scanf("%d %d\n", &n, &m)){
    if(n == 0 && m == 0)break;
    int point[n + 1];
    point[0] = 0;
    set<int> a;
    for (int i = 1; i <= n; i++) {
      scanf("%d\n", &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){
      int tmp = *(--a.upper_bound(m - i));
      if(i + tmp <= m)
        ans = max(ans, i + tmp);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

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

int main()
{
	vector<int> AB,P;
	vector<int>::iterator it;
	int maxpoint = 0,n,m,in,key,c;

	cin >> n >> m;
	while(n != 0 && m != 0){

	for(int i = 0; i < n; i++){
		cin >> in;
		P.push_back(in);
	}

	P.push_back(0);
	sort( P.begin(),P.end() );

	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= n; j++){
			AB.push_back(P[i]+P[j]);
		}
	}

	AB.push_back(m+1);
	sort( AB.begin(),AB.end() );

	for(int i = 0; i < AB.size() ; i++){
		key = m - AB[i];
		if(key < 0)	break;
		else	c = upper_bound(AB.begin(),AB.end(),key) - AB.begin() - 1;
		maxpoint = max(maxpoint,AB[i] + AB[c]);
	}

	cout << maxpoint << endl;
	
	maxpoint = 0;	
	AB.clear();
	P.clear();

	cin >> n >> m;
	}
	return 0;
}

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

#define sint(i) scanf("%d",&i);
#define sintt(i,j) scanf("%d%d",&i,&j);
#define sinttt(i,j,k) scanf("%d%d%d",&i,&j,&k);
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int mato[501501];

int main(){
	while(1){
		int n,m;
		sintt(n,m);
		
		if(n==0)break;
		
		int mat[1001];
		mat[0]=0;
		rep(i,n){
			sint(mat[i+1]);
		}
		n++;
		int cn=0;
		rep(i,n){
			rep(j,n-i){
				mato[cn]=mat[j]+mat[j+i];
				cn++;
			}
		}
		
		sort(mato,mato+cn);
		
		/*
		rep(i,cn){
			printf("%d ",mato[i]);
		}puts("");
		*/
		int maxi=0;
		rep(i,cn){
			int *nn=lower_bound(mato,mato+cn,m-mato[i])-1;
			if(*nn+mato[i]>m){
				break;
			}
			maxi=max(maxi,mato[i]+*nn);
		}
		printf("%d\n",maxi);
	}
}

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);
			if(it2!=s.begin())	it2--;
			ans=max(ans,*it+*it2);
		}
		cout<<ans<<endl;
	}
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)


typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vvi;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007



const int MAX_N = 1000;
const int MAX_W = 200000000;

int dp[MAX_N + 1][MAX_W + 1][5];
int v[MAX_N], w[MAX_N];
int N,W;

// int solve(int i, int j, int k){
// 	if(dp[i][j][k] != -1) return dp[i][j][k];
// 	int ret = 0;
// 	if(i == N) ret = 0;
// 	else if(j < w[i] || k == 4) ret =dp[i][j][k];
// 	else ret =max(max(solve(i,j,k),solve(i,j-w[i],k+1) + v[i]),solve(i+1,j-w[i],k + 1) + v[i]);
// 	return dp[i][j][k] = ret;
// }

int solve() {
	for(int i = 0; i < N; i++) {
		for(int j = 0; j <= W; j++){
			for(int k = 0; k < 5; k++){
				if(j < w[i] || k <1) {
					dp[i + 1][j][k] = dp[i][j][k];
				}
				else{
					dp[i + 1][j][k] = max(max(dp[i][j][k],dp[i][j-w[i]][k-1] + v[i]), dp[i + 1][j - w[i]][k-1] + v[i]);
				}
			}
		}
	}
	return dp[N][W][4];
}

int main(){
	int n, m;
	while(cin>>n>>m){
		N = n;
		W = m;
		// for(int i = 0; i<= N; i++){
		// 	for(int j = 0; j <=W; j++){
		// 		for(int k = 0; k < 4; k++){
		// 			dp[i][j][k] = -1;
		// 		}
		// 	}
		// }
		for(int i = 0; i < n; i++){
			cin >> v[i];
			w[i] = v[i];
		}
		cout<<solve()<<endl;
	}
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;

long modpow(long a, long n, long mod) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) {
    return modpow(a, mod - 2, mod);
}

int main()
{
    int P[1005];
    int sum1[1002005], sum2[1002005];
    int N;
    int M;

    for(;;){
        cin >> N >> M;
        //cout << N << " " << M << endl;
        if(N == 0 && M == 0)
            break;
        P[0] = 0;
        REP(i, N)
            cin >> P[i + 1];
        sort(P, P + N + 1);
        REP(i, N + 1)
            REP(j, N + 1){
                sum1[i * (N + 1) + j] = P[i] + P[j];
                sum2[i * (N + 1) + j] = M - P[i] - P[j];
            }
        sort(sum1, sum1 + (N + 1) * (N + 1));
        sort(sum2, sum2 + (N + 1) * (N + 1));
        long ans = 0;
        REP(i, (N + 1) * (N + 1)){
            int temp = (int)(lower_bound(sum2, sum2 + (N + 1) * (N + 1), sum1[i]) - sum2);
            //cout << N << " " << M << " " << sum1[i] << " " << sum2[temp] << endl; 
            if(temp != (N + 1) * (N + 1) && ans < (long)sum1[i] + (long)M - (long)sum2[temp] && M >= (long)sum1[i] + (long)M - (long)sum2[temp])
                ans = (long)sum1[i] + (long)M - (long)sum2[temp];
        }
        cout << ans << endl;
    }

    return 0;
}

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

using namespace std;

int main(){
	while(true){
		int n, m;
		cin >> n >> m;
		if(n == 0 && m == 0){ break; }
		vector<int> p(1, 0);
		for(int i = 0; i < n; i++){
			int val;
			cin >> val;
			p.push_back(val);
		}
		vector<int> p2;
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= i; j++){
				if(p[i] + p[j] <= m){ p2.push_back(p[i] + p[j]); }
			}
		}
		sort(p2.begin(), p2.end());
		int answer = 0;
		for(int i = 0; i < p2.size(); i++){
			vector<int>::iterator it = upper_bound(p2.begin(), p2.end(), m - p2[i]);
			int v = p2[i] + *(--it);
			answer = max(answer, v);
		}
		cout << answer << endl;
	}
	return 0;
}