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 <stdio.h>
#include <algorithm>

using namespace std;

int n, m;
int p[1001];
int sp[1001*1001];

int main()
{
	for (;;)
	{
		scanf("%d %d", &n, &m);
		if (!n && !m)break;
		for (int i = 0; i < n; ++i)
		{
			scanf("%d", &p[i]);
		}
		p[n] = 0;
		int it = 0;
		for (int a = 0; a <= n; ++a)
		{
			for (int b = 0; b <= a; ++b)
			{
				sp[it++] = p[a] + p[b];
			}
		}
		sort(sp, sp + it);
		int *fr = sp;
		int *bk = sp + it - 1;
		int ans = 0;
		while(fr <= bk)
		{
			if ((*fr) + (*bk) <= m)
			{
				ans = max(ans, (*fr) + (*bk));
				++fr;
			}
			else
			{
				--bk;
			}
		}
		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);
      points.push_back(tmp);
    }
    set<int>::iterator it1,it2;
    for(it1 = points.begin();it1 != points.end();it1++){
      for(it2 = points.begin();it2 != points.end();it2++){
	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);
      if(it2 == points2.begin())continue;
      it2--;
      res = max(res, *it1 + *it2);
    }
    printf("%d\n",res);
  }
}

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;
}