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 <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <algorithm>
#include <array>
#include <numeric>
using namespace std;

const auto gcu = getchar_unlocked;
const auto pcu = putchar_unlocked;
#define _NE noexcept
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _sc _il bool scan
_T T in(int c)_NE{T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_il int in()_NE{return in<int>(gcu());}
_T T scan(T &n)_NE{int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c)_NE{c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s)_NE{int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t)_NE{return scan(h)&&scan(t...);}
#define _ot _il void out
#define _ol _il void outl
_ot(bool b)_NE{pcu('0'+b);}
_ot(const char *s)_NE{while(*s)pcu(*s++);}
_ot(char c)_NE{pcu(c);}
#ifdef _GLIBCXX_STRING
_ot(string s)_NE{for(char c:s)pcu(c);}
#endif
_T _ot(T n)_NE{static char b[20];char *p=b,m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=n%10*m+'0',n/=10;while(p!=b)pcu(*--p);}
_ol()_NE{out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _ot(vector<T> &v)_NE{for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _ot(H&& h, T&&... t)_NE{out(h);out(move(t)...);}
template <typename... T> _ol(T&&... t)_NE{out(move(t)...);outl();}

#define all(o) o.begin(), o.end()

void radix_sort(vector<int> &v) {
	vector<int> t(v.size());
	array<int, 256> c, s;
	for (int i = 0; i < 32; i += 8) {
		fill(all(c), 0);
		for_each(all(v), [&](int j) {c[(j >> i) & 255]++;});
		s.front() = 0;
		partial_sum(all(c) - 1, s.begin() + 1);
		for_each(all(v), [&](int j) {t[s[(j >> i) & 255]++] = j;});
		swap(v, t);
	}
}

int main() {
	for (int N, M; N = in(), M = in();) {
		vector<int> t(N), u;
		for (int &p: t)
			p = in();
		t.push_back(0);
		for (auto i = t.begin(); i != t.end(); i++)
			for (auto j = i; j != t.end(); j++)
				if (*i + *j <= M)
					u.push_back(*i + *j);
		radix_sort(u);
		int r = 0;
		for (auto i = u.begin(), e = u.end() - 1; i <= e; i++) {
			for (; *i + *e >= M; e--)
				;
			r = max(r, *i + *e);
		}
		outl(r);
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
    while(true){
        int n,m;
        cin >> n >> m;
        if(n==0)break;
        vector<int> list(n);
        for(int i=0;i<n;i++){
            cin >> list[i];
        }
        sort(list.begin(),list.end());
        vector<vector<int> > map(n,vector<int>(5,0));
        for(int i=1;i<=4;i++){
           if(list[0]*i<=m)map[0][i]=list[0]*i;
           else map[0][i]=map[0][i-1];
        }
        for(int i=1;i<n;i++){
            if(list[i]<=m)map[i][1]=max(list[i],list[i-1]);
            else list[i-1];
        }
        for(int i=1;i<n;i++){
            for(int j=2;j<=4;j++){
                map[i][j]=map[i][j-1];
                for(int k=0;k<=i;k++){
                for(int l=0;l<=i;l++){
                    if(map[k][j-1]+list[l]<=m){
                        map[i][j]=max(map[k][j-1]+list[l],map[i][j]);
                    }
                }
                }
            }
        }
        cout << map[n-1][4] << endl;
    }
    return 0;
}

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

long long int n, m;
long long int p[1000];
long long int s;
vector<long long int> a;
vector<long long int> b;

void f(long long int x){
	if(x <= m) s = max(s, x);
}

int main(){
	while(1){
		scanf("%lld %lld", &n, &m);
		if(n == 0 && m == 0) break;
		memset(p, 0, sizeof(p));
		for(int i=0; i<n; i++) scanf("%lld", &p[i]);
		a = b;
		s = 0;

		for(int i=0; i<n; i++){
			f(p[i]);
		}
		for(int i=0; i<n; i++){
			for(int j=0; j<=i; j++){
				if(p[i] + p[j] <= m){
					a.push_back(p[i] + p[j]);
				}
				f(p[i] + p[j]);
			}
		}
		for(int i=0; i<n; i++){
			for(int j=0; j<a.size(); j++){
				f(p[i] + a[j]);
			}
		}
		for(int i=0; i<a.size(); i++){
			for(int j=0; j<a.size(); j++){
				f(a[i] + a[j]);
			}
		}
		printf("%lld\n", s);

	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int a,s,d[1000],f[210000000],g,h,i,j,z,x,c,v;
int main(void)
{
	int high,low,ba,naka,nokori,kore;
	while(1){
		kore=0;
		scanf("%d %d",&a,&s);
		if(a==0&&s==0)
			break;
		d[0]=0;
		for(i=1;i<=a;i++){
			scanf("%d",&d[i]);
		}
		g=0;
		for(i=0;i<=a;i++){
			for(j=i;j<=a;j++){
				f[g]=d[i]+d[j];
				g++;
			}
		}
		sort(f,f+g);
		/*for(i=0;i<g;i++)
			printf("%d\n",f[i]);*/
		for(i=0;i<g;i++){
			v=0;
			low=0;
			high=g-1;
			nokori=s-f[i];
		//	printf("%d %d\n",i,nokori);
			if(nokori>=0){
				while(1){
					if(low>high)
						break;
					naka=(low+high)/2;
				//	printf("%d = %d %d %d %d %d\n",i,f[high],f[naka],f[low],f[i],nokori);
					if(nokori>f[naka]){
						low=naka+1;
					//	printf("%d %d %d low\n",i,nokori,f[naka]);
					}
					if(nokori<f[naka]){
						high=naka-1;
					//	printf("%d %d %d high\n",i,nokori,f[naka]);
					}
					if(nokori==f[naka]){
						ba=f[naka];
						v=1;
						break;
					}
				}
				if(v==0)
					ba=f[high];
			//	printf("%d\n",f[i]+ba);
				if(kore<f[i]+ba)
					kore=f[i]+ba;
			}
		}
		printf("%d\n",kore);
	}
	return 0;
}

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

using namespace std;
using lint = long long;

int main() {
    while (true) {
        int N, M;
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        vector<int> P(N);
        for (int i = 0; i < N; i++) {
            cin >> P[i];
        }
        P.emplace_back(0);
        ++N;
        vector<int> P2(N*N);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                P2[i*N+j] = P[i] + P[j];
            }
        }
        sort(P2.begin(), P2.end());
        int ans = 0;
        for (int i = 0; i < N*N; i++) {
            const int index = upper_bound(P2.begin(), P2.end(), M - P2[i]) - P2.begin();
            if (index == 0) continue;
            ans = max(ans, P2[i] + P2[index-1]);
        }
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

int main(void) {
  int N,M;
  while (cin >> N >> M , N) {
    vector<int> p(N);
    for (int i = 0; i < N; ++i) {
      cin >> p[i];
    }
    sort(p.begin(),p.end());
    int res = 0; 
    for (int i = N-1; i >= 0; --i) {
      while (res + p[i] <= M) {
        res += p[i];
      }
    }
    cout << res << endl;
  }
  return 0;
}