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<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 <cstdio>
#include <algorithm>
#include <functional>
#include <cstring>
 
long long kazu[1002001];//1001^2
long long n,m,Max = 0,data[1001];
 
int main(){
    while(std::cin>>n>>m, n * m){
        if(n==0&&m==0)break;
 
        memset(kazu, 0, sizeof(kazu));
        memset(data, 0, sizeof(data));
 
        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));
 
        long long* ptr;
        for(int i = 0; i < (n + 1) * (n + 1);++i){
            if(kazu[i] < m){
                ptr = std::upper_bound(kazu, kazu + (n + 1) * (n + 1), m - kazu[i]) - 1;
                Max = std::max(Max, *ptr + kazu[i]);
            }
        }
 
 
        std::cout << Max << std::endl;
        Max = 0;
    }

    return 0;
}

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

using namespace std;

typedef pair<int,int> P;
typedef long long ll;
const int MAX = 2*100000000+1;
const ll INF = (1LL<<60);

int N,M;
vector<ll> V[4];
ll ans;
void init(){
  for(int i = 0; i < 4; i++)
    V[i].clear();
  ans = 0;
}


void solve(){

  sort(V[0].begin(),V[0].end());

  for(int i = 0; i < V[0].size(); i++)
    for(int j = 0; j < V[0].size(); j++)
      V[1].push_back(V[0][i]+V[0][j]);

  V[1].push_back(INF);
  sort(V[1].begin(),V[1].end());

  vector<ll> :: iterator ite1,ite2;
  
  for(int i = 0; i < V[0].size(); i++)
    for(ite1 = V[1].begin(); ite1 != V[1].end(); ite1 = upper_bound(V[1].begin(),V[1].end(),*ite1))
      if(*ite1 != INF) V[2].push_back(V[0][i]+*ite1);
      else break;

  V[2].push_back(INF);
  sort(V[2].begin(),V[2].end());

  for(ite1 = V[1].begin(); ite1 != V[1].end(); ite1 = upper_bound(V[1].begin(),V[1].end(),*ite1))
    for(ite2 = V[1].begin(); ite2 != V[1].end(); ite2 = upper_bound(V[1].begin(),V[1].end(),*ite2))
      if(*ite1 != INF && *ite2 != INF) V[3].push_back(*ite1+*ite2);
      else break;

  V[3].push_back(INF);
  sort(V[3].begin(),V[3].end());

  for(int i = 0; i < 4; i++){
    vector<ll> :: iterator ite;
    ite = upper_bound(V[i].begin(),V[i].end(),M);
 
    ans = max(ans,*(ite-1));
  }
  cout << ans << endl;
}

int main(){
  while(cin >> N >> M && N+M){
    init();
    for(int i = 0; i < N; i++){
      ll in;
      cin >> in;
      V[0].push_back(in);
    }
    solve();
  }
  return 0;
}

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

int main(){
  int n,m;
  while(cin >> n >> m,n&&m){
    int ans=0;
  vector<int> v1(n),v2;
  r(i,n)cin >> v1[i];
  v1.push_back(0);
  n++;
  
  r(i,n)r(j,n)v2.push_back(v1[i]+v1[j]);
  
  sort(v2.begin(),v2.end());
   
  int j=n*n-1;
  r(i,n*n){
    while(j>i&&v2[i]+v2[j]>m)j--;
    if(v2[i]+v2[j]<=m)ans=max(ans,v2[i]+v2[j]);
  }
  cout<<ans<<endl;
  }
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
int n, m, c[1009], s[1000009];
int main() {
	while (scanf("%d%d", &n, &m), n) {
		for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
		int b = 0;
		for (int i = 0; i <= n; i++) {
			for (int j = i; j <= n; j++) {
				if (c[i] + c[j] <= m) s[b++] = c[i] + c[j];
			}
		}
		sort(s, s + b);
		int r = b - 1, ret = 0;
		for (int i = 0; i < b && i <= r; i++) {
			while (r > 0 && s[i] + s[r] > m) r--;
			ret = max(ret, s[i] + s[r]);
		}
		printf("%d\n", ret);
	}
	return 0;
}

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

int main() {
	int N, M;
	while(cin >> N >> M && (N || M)){
		int  P[1001] = {0}, ans = 0;
		for(int i = 0; i < N; ++i){
			scanf("%d", &P[i]);
		}
		vector<int> v;
		for(int i = 0; i <= N; ++i){
			for(int j = 0; j <= N; ++j){
				if(P[i] + P[j] <= M)
					v.push_back(P[i] + P[j]);
			}
		}
		sort(v.begin(), v.end());
		v.erase(unique(v.begin(), v.end()), v.end());
		for(unsigned int i = 0; i < v.size(); ++i){
			if(v[i] > M)
				continue;
			vector<int>::iterator it = lower_bound(v.begin(), v.end(), M - v[i]);
			if(it == v.end())
				--it;
			if(*it > M - v[i])
				--it;
					
			ans = max(ans, v[i] + *it);
		}
		/*for(int i = 0; i <= N; ++i){
			for(int j = 0; j <= N; ++j){
				if(P[i] + P[j] > M)
					continue;
				set<int>::iterator it = darts.lower_bound(M - (P[i] + P[j]));
				if(it == darts.end())
					--it;
				if(*it > M - (P[i] + P[j]))
					--it;
					
				ans = max(ans, P[i] + P[j] + *it);
			}
		}*/
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1002002];
int main(void){
	while(1){
		ll n,m,i,j,p[1001],hi,lo,mi,pp,x,mx;
		cin>>n>>m;
		if(n==0)break;
		p[0]=0;
		for(i=1;i<=n;i++) cin>>p[i];
		for(i=0;i<=n;i++) for(j=0;j<=n;j++) a[i*n+j]=p[i]+p[j];
		pp=pow(n+1,2);
		sort(a,a+(pp+2));
//		for(i=0;i<=pp+1;i++) cout<<a[i]<<endl;
		mx=0;
		for(i=0;i<=pp+1;i++){
			hi=pp+1; lo=0;
			while(hi>=lo){
				mi=(hi+lo)/2;
				x=a[i]+a[mi];
				if(x>m) hi=mi-1;
				else if(x<m) lo=mi+1;
			}
			x=a[i]+a[hi];
			if(mx<x&&m>=x) mx=x;
		}
		cout<<mx<<endl;
	}
	return 0;
}