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

int n,m;
int target[1000];
int sum = 0;

void chek(int cnt, int point,int now)
{
	int i;
	//int sum=0;
	sum = max(sum, point);
	if (cnt == 5)
	{
		return ;
	}
	if (sum == m)
	{
		return;
	}

	for (i = now; i <= n; i++)
	{
		if (point + target[i] <= m)
		{
			chek(cnt + 1, point + target[i],i);
		}
		else return;
	}

}

int main()
{
	//int sum;
	int i;

	while (scanf("%d %d",&n,&m), n != 0 || m != 0)
	{
		sum = 0;
		//printf("AA%d\n",m);
		for (i = 1; i <= n; i++)
		{
			scanf("%d", &target[i]);
		}

		sort(target + 1, target + n + 1);

		chek(1, 0,0);

		printf("%d\n", sum);
	}

	return 0;
}

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

using namespace std;

int main(){
    while(true){
        int N,M;
        cin >> N >> M;
        if(N == 0 and M == 0) break;
        const int C = 4;
        vector<set<int>> ps(C+1);
        ps[0].insert(0);
        for(int i=0;i<N;i++){
            int p;cin >> p;
            for(int j=0;j<C;j++){
                for(auto k : ps[j]){
                    if(k+p <= M){
                        ps[j+1].insert(k+p);
                    }
                }
            }
        }
        int ret = 0;
        for(int i=0;i<C+1;i++){
            ret = max(ret,*max_element(ps[i].begin(),
                                       ps[i].end()));
        }
        cout << ret << endl;
    }
    return 0;
}

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 = 1340788; T > 1000; 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 += 100000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*1000)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define ALL(v) (v).begin(), (v).end()

int main()
{
  int n, m;
  cin >> n >> m;
  //while (cin >> n >> m, n){
    int a[1010];
    for (int i = 0; i < n; i++){
      cin >> a[i];
    }

    vector<int> r;
    for (int i = 0; i < n; i++){
      for (int j = 0; j < n; j++){
        if (a[i] + a[j] > m) continue;
        r.push_back(a[i] + a[j]);
      }
    }

    sort(ALL(r));
    r.erase(unique(ALL(r)), r.end());

    r.push_back(r[r.size() - 1]);
    int maxs = 0;
    for (int i = 0; i < n; i++){
      if (a[i] <= m){
        maxs = max(maxs, a[i]);
      }
      maxs = max(maxs, a[i] + *(lower_bound(ALL(r), m - a[i]) - 1));
      for (int j = 0; j < n; j++){
        if (a[i] + a[j] <= m){
          maxs = max(maxs, a[i] + a[j]);
        }
        int n = *(lower_bound(ALL(r), m - (a[i] + a[j])) - 1);
        maxs = max(maxs, n + a[i] + a[j]);
      }
    }
    cout << maxs << endl;
  //}
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cstdio>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;

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

int n;
llong m;

int main(){
	while(1){
		scanf("%d%lld", &n, &m);
		if(n==0 && m==0) return 0;
		n++;
		vector<llong> p(n);
		p[0]=0;
		FOR(i, 1, n) scanf("%lld", &p[i]);
		VSORT(p);
		int size=0;
		vector<llong> v;
		REP(i, n){
			REP(j, n){
				v.push_back(p[i]+p[j]);
			}
		}
		VSORT(v);
		llong ans=0;
		REP(i, v.size()){
			int rem=m-v[i];
			if(rem < 0) continue;
			int l=0, r=v.size();

			while(l<r){
				int mid=(l+r)/2;
				if(rem>v[mid]){
					l=mid+1;
				}
				else if(rem<v[mid]){
					r=mid;
				}
				else {
					break;
				}
			}

			ans = max(ans, v[i]+v[r-1]);
		}

		printf("%d\n",ans);
	}
}

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

	int tsP[1001 * 1001];
	int main() {
		int N, M;
		int P[1001];
		int ans;
		int cnt;

		P[0] = 0;

		//cin >> N >> M;
		while (cin>>N>>M,N&&M) {
			ans = 0;
			cnt = 0;
			for (int i = 1; i <= N; i++) {
				cin >> P[i];
			}

			for (int i = 0; i <= N; i++) {
				for (int j = i; j <= N; j++) {
					if (P[i] + P[j] <= M) {
						tsP[cnt++] = P[i] + P[j];
					}

				}
			}

			for (int i = 0; i < cnt; i++) {
				for (int j = i; j < cnt; j++) {
					if (tsP[i] + tsP[j] <= M) {
						ans = max(ans, tsP[i] + tsP[j]);
					}
				}
			}
			cout << ans << endl;
		}
		return 0;
	}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long n;
	unsigned long long m;
	while(cin>>n>>m,n!=0||m!=0){
		int k=0;
	long long int s=0;
		vector<long long int> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0&&k!=4){
			if(s+y[n-1]<=m){
				s=s+y[n-1];
				k++;
			}
			else
				n--;
			
		}
		cout<<s<<endl;
	}
	
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
while(1){
int i,j,n,m,o=1,s=0;
static int p[1001]={0},up[1001*1001]={0};
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++){
scanf("%d",&p[i]);
for(j=i;j>0;j--){
up[o]=p[i]+p[j];
o++;
}
}
sort(up,up+o);
for(i=0;i<o;i++)s=max(s,up[i]+*(upper_bound(up,up+o,m-p[i])-1));
printf("%d\n",s);
}
return 0;
}