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 <set>
#include <algorithm>
#include <iostream>
#define MAX(a,b) (((a)>(b))?(a):(b))
using namespace std;
typedef long long int lli;

set<lli> v;
lli m,n;
lli k;
int mx;

int dfs(int num,int score){
	mx=MAX(mx,score);
	if(num<4){
		for(set<lli>::iterator it=v.begin();it!=v.end()&&score+*it<=m;it++){
			dfs(num+1,score+*it);
		}
	}
	return mx;
}

int main(void){
	while(cin>>n>>m,(n||m)){
		v.clear();
		mx=0;
		for(lli i=0;i<n;i++){
			cin>>k;
			v.insert(k);
		}
		for(set<lli>::iterator itm=v.begin();itm!=v.end();itm++){
			dfs(1,*itm);
		}
		cout<<mx<<endl;
	}
	return 0;
}

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

#define MAX_N (1000)
#define MAX_M (2 * 1e8)

using namespace std;

int field[MAX_N + 1];
int fld[(MAX_N + 1) * (MAX_N + 1)];

int N,M;

int ans;

int bin_search(int tar,int *s,int *e){
	int result;
	int width = e - s;
	while(true){
		width = e - s;
		if(width <= 1){
			result = *s;
			break;
		}

		if(*(s + (width / 2)) <= tar){
			s = s + (width / 2);
		}
		else{
			e = s + (width / 2);
		}
	}
	
	return result;
}

void solve(){
	for(int i = 0;i <= N;i++){
		for(int j = 0;j <= N;j++){
			fld[(N + 1) * i + j] = field[i] + field[j];
		}
	}

	sort(fld,fld + (N + 1) * (N + 1));

	ans = 0;
	int rest;
	int sample;
	for(int i = 0;i <= N;i++){
		for(int j = 0;j <= N;j++){
			rest = M - (field[i] + field[j]);
			if(rest < 0) break;
			sample = bin_search(rest,fld,fld + (N + 1) * (N + 1));
			ans = max(ans,field[i] + field[j] + sample);
		}
	}

	return ;
}

int main(){

	while(true){
		scanf("%d%d",&N,&M);
		if(N == 0 && M == 0) break;
		for(int i = 1;i <= N;i++){	//fiest element is 0;
			scanf("%d",field + i);
		}

		solve();

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

	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
  int N, M;
  int P[10001];

  while(cin >> N >> M){
    if(N == 0 && M == 0) break;
    
    for(int i = 0 ; i < N ; i++){
      cin >> P[i];
    }
    
    sort(P, P+N, greater<int>());
    int ans = 0;
    
    for(int i = 0 ; i < N ; i++){
      if(M < 0) break;
      if(M >= P[i]){
	ans += (M / P[i]) * P[i];
	M -= (M / P[i]) * P[i];
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

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