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<vector>
#include<map>
#include<utility>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<cstdlib>
#include<typeinfo>
#include<sstream>
#include<list>
#include<set>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define fori(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
int in()
{
	int x;cin>>x;
	return x;
}
int stringInt(string a)
{
	return atoi(a.c_str());
}
string intString(int a)
{
	stringstream ss;
	ss<<a;
	return ss.str();
}
vector<int> SieveOfEratosthenes(int x)
{
	vector<int> a;
	vector<int> b(x+1,0);
	for(int i=2;i<=x;i++)
	{
		if(b[i]==0)
		{
			a.push_back(i);
			for(int l=i;l<=x;l+=i)
			{
				b[l]=1;
			}
		}
	}
	return a;
}

int main ()
{
	int n,m;
	while(cin>>n>>m&&n&&m)
	{
		vector<int> a,b,c;
		b.push_back(0);
		rep(i,n)
		{
			b.push_back(in());
		}
		rep(i,n+1)
		{
			rep(l,n+1)
			{
				a.push_back(b[i]+b[l]);
			}
		}
		rep(i,a.size())
		{
			rep(l,a.size())
			{
				c.push_back(a[i]+a[l]);
			}
		}
		sort(c.begin(),c.end());
		cout<<*(upper_bound(c.begin(),c.end(),m)-1)<<endl;
	}

}

C++: #include "algorithm"
#include "array"
#include "cmath"
#include "complex"
#include "cstdio"
#include "deque"
#include "initializer_list"
#include "map"
#include "numeric"
#include "queue"
#include "regex"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "vector"

using std::accumulate;
using std::all_of;
using std::any_of;
using std::array;
using std::binary_search;
using std::copy;
using std::count_if;
using std::deque;
using std::fill;
using std::find_if;
using std::for_each;
using std::inner_product;
using std::lower_bound;
using std::map;
using std::max;
using std::min;
using std::multimap;
using std::multiset;
using std::next_permutation;
using std::none_of;
using std::priority_queue;
using std::queue;
using std::regex;
using std::search;
using std::set;
using std::sort;
using std::sregex_token_iterator;
using std::stable_sort;
using std::stack;
using std::string;
using std::swap;
using std::to_string;
using std::transform;
using std::unordered_map;
using std::upper_bound;
using std::vector;

#define INF 1000000000 // 10^10
#define FOR(i, n) for ((i) = 0; (i) < (n); ++(i))
#define FOR_FROM_NOT_ZERO(i, s, n) for ((i) = (s); (i) < (n); ++(i))
#define FOR_dec(i, n) for ((i) = (n); (i) >= 0; --(i))
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define d2_arr(height, width) array<array<int, (width)>, (height)>
#define d2_vec vector<vector<int>>
#define in_int(x) scanf(" %d/n", &(x));
#define in_str(x) scanf(" %[a-zA-Z 0-9_]/n", (x));
#define ch(x) printf("debag %d\n", (x))

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dx_ar[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy_ar[] = {1, 0, -1, 1, -1, 1, 0, -1};

template <typename type> void print_array_vertical(type d1_arr);
template <typename type> void print_array_horizon(type d1_arr);
template <typename type> void print_2darray(type d2_arr);
vector<vector<int>> d2_zeros(int height, int width);
vector<vector<int>> padding(vector<vector<int>> arr, int pad);
inline vector<int> str_int_split(char *s);
inline std::vector<std::vector<int>> in_d2_arr(int N, int size = 1000);
inline std::vector<int> in_d1_arr(int size = 1000);
inline double d2_distance(int x1, int y1, int x2, int y2);
inline int d2_manhattan(int x1, int y1, int x2, int y2);

int main() {
  int i, N, M, temp;
  std::vector<int> v = in_d1_arr();
  N = v[0];
  M = v[1];
  std::vector<int> tar;

  FOR(i, N) {
    in_int(temp);
    tar.push_back(temp);
  }

  sort(tar.begin(), tar.end(), std::greater<int>());
  int point = 0;

  FOR(i, 4) {
    FOR(i, N) {
      if (point + tar[i] <= M) {
        point = point + tar[i];
        break;
      }
    }
  }

  printf("%d\n", point);

  return 0;
}

template <typename type> void print_array_vertical(type d1_arr) {
  int i;
  string s;
  FOR(i, d1_arr.size()) {
    printf("%d\n", d1_arr[i]);
    fflush(stdout);
  }
}

template <typename type> void print_array_horizon(type d1_arr) {
  int i;
  FOR(i, d1_arr.size() - 1) { printf("%d ", d1_arr[i]); }
  printf("%d\n", d1_arr.back());
  fflush(stdout);
}

template <typename type> void print_2darray(type d2_arr) {
  int i, j;
  FOR(i, d2_arr.size()) {
    FOR(j, d2_arr[i].size() - 1) { printf("%d ", d2_arr[i][j]); }
    printf("%d\n", d2_arr[i].back());
    fflush(stdout);
  }
}

vector<vector<int>> d2_zeros(int height, int width) {
  vector<vector<int>> temp;
  temp.resize(height);
  int i, j;
  FOR(i, height) {
    temp[i].resize(width);
    FOR(j, width) { temp[i][j] = 0; }
  }
  return temp;
}

vector<vector<int>> padding(vector<vector<int>> arr, int pad) {
  vector<vector<int>> arr_c = arr;
  int height = arr_c.size();
  int width = arr_c[0].size();
  std::vector<int> v;
  v.resize(width);
  fill(v.begin(), v.end(), pad);
  arr_c.insert(arr_c.begin(), v);
  arr_c.push_back(v);
  int i;
  FOR(i, height + 2) {
    arr_c[i].insert(arr_c[i].begin(), pad);
    arr_c[i].resize(width + 2);
  }
  arr_c.shrink_to_fit();
  return arr_c;
}

inline vector<int> str_int_split(char *s) {
  char *tok = strtok(s, " ");
  std::vector<int> v;
  while (tok != NULL) {
    v.push_back(atoi(tok));
    tok = strtok(NULL, " "); /* 2回目以降 */
  }
  return v;
}

inline std::vector<std::vector<int>> in_d2_arr(int N, int size) {
  int i;
  std::vector<std::vector<int>> v;
  FOR(i, N) {
    char *lines;
    lines = new char[size];
    in_str(lines);
    v.push_back(str_int_split(lines));
    delete[] lines;
  }
  return v;
}

inline std::vector<int> in_d1_arr(int size) {
  std::vector<int> v;
  char *lines;
  lines = new char[size];
  in_str(lines);
  v = str_int_split(lines);
  delete[] lines;
  return v;
}

inline double d2_distance(int x1, int y1, int x2, int y2) {
  return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
}

inline int d2_manhattan(int x1, int y1, int x2, int y2) {
  return (abs(x2 - x1) + abs(y2 - y1));
}