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<bits/stdc++.h>

#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repm(a,b,c) for(int a=(b-1);a>=c;a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d",&a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s){double ans=0;rep(i,0,s.length()){ans+=(s[i]-'0')*pow(10,s.length()-i-1);}return (int)ans;}
inline string numstr(int m){str s="";while(m>0){char c;int a=m/10;if(a>0)a=m%(a*10);else a=m;c=(char)('0'+a);s+=c;m/=10;}str st="";for(int i=s.size()-1;i>=0;i--){st+=s[i];}return st;}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
vi cost;
int d[1010];
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,k;
    while(1){
    cost.clear();
    cin >> n >> k;
    if(n==0&&k==0)break;
    rep(i,0,n){
        cin >> d[i];
    }
    d[n]=0;
    int cou=0;
    rep(i,0,n+1){
        rep(j,0,n+1){
            int u=d[i]+d[j];
            cost.pb(u);
        }
    }
    sort(ALL(cost));
    int ans=0;
    for(int v : cost){
           // cout << v << endl;
        int an=k-v;
        if(an<0)break;
        int smal=0,larg=cost.size()-1;
        while(larg>smal){
            int cen=(smal+larg)/2;
            //cout << cen << endl;
            if(cost[cen]>an){
                larg=cen;
            }else if(cost[cen]<an){
                smal=cen+1;
            }else{
                ans=k;
                break;
            }
            if(larg<=smal){
                int a=cost[larg-1];
                if(ans<(v+a))ans=v+a;
                break;
            }
        }
        if(ans<(v+cost[larg-1]))ans=(v+cost[larg]);
    }
    cout << ans << endl;
    }
    return 0;
}

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

int n,m;
int target[1000];

int chek(int cnt, int point)
{
	int i;
	int sum=0;

	if (cnt == 5)
	{
		return point;
	}

	for (i = 0; i <= n; i++)
	{
		if (point + target[i] <= m)
		{
			sum = max(sum, chek(cnt + 1, point + target[i]));

			/*
			num = chek(cnt + 1, point + target[i]);
			if (num <= m)
			{
				max = (max < num) ? max : num;
			}
			*/
		}
	}

	return sum;
}

int main()
{
	int sum;
	int i;

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

		sum = chek(1, 0);

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

	return 0;
}

C++: #include <iostream>
#include <map>
#include <algorithm>
#define INF (2LL << 58)
using namespace std;

typedef long long ll;

map<int, ll> dp;
int n, m, p[1111];

ll rec(int sum, int cnt){
    
    if(sum < 0) return -INF;
    if(cnt == 4) return 0;
    if(dp[sum]) return dp[sum];
    
    ll ret = -INF;
    for(int i = 0; i < n; i++){
        ret = max(ret, max(rec(sum - p[i], cnt + 1) + p[i], rec(sum, cnt + 1)));
    }
    return dp[sum] = ret;
}

int main(){
    
    while(cin >> n >> m, n||m){
        dp.clear();
        for(int i = 0; i < n; i++) cin >> p[i];
        sort(p, p + n, greater<int>());
        cout << rec(m, 0) << endl;
    }
}

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

int main() {
	for(;;){
		/*入力*/
		int N,M;
		cin >> N >> M;
		if(N==0 && M==0)exit(0);
		int P[N+1],array[M+1]={0};
		P[0]=0;
		for(int i=1;i<=N;i++)cin >> P[i];
		for(int i=0;i<=N;i++)array[P[i]]=1;
		
		
		/*２投(０投１投を含む)で1からMまでが可能か判定*/
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				if(P[i]+P[j] <= M)array[P[i]+P[j]]=1;
			}
		}
		
		/*４投に拡張*/
		int p[M+1]={0};
		for(int i=1;i<=M;i++){
				if(array[i]==1)p[i]=i;
		}
		for(int i=0;i<=M;i++){
			for(int j=0;j<=M;j++){
				if(p[i]+p[j] <= M)array[(p[i]+p[j])]=1;
			}
		}
		
		/*判定出力*/
		for(int i=M;i>0;i--){
			if(array[i]==1){
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int kk[1100001];
int n, m;

int binary_search(int x) {
    if (x <= 0) return 0;
    int l = 0, r = n * n;
    //dump(r - l);
    int i = 0;
    while(r - l >= 1) {
        i = (l + r) / 2;
        if (kk[i] == x) return i;
        else if (kk[i] < x) l = i + 1;
        else r = i;
    }
    while(kk[i] > x) i--;
    return i;
}

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true){
        cin >> n >> m;
        if(n == 0 && m == 0) return 0;
        int k[n+1];
        k[0] = 0;
        rep(i, n) cin >> k[i+1];

        rep(c, n+1) {
            rep(d, n+1) {
                kk[c * n + d] = k[c] + k[d];
            }
        }

        sort(kk, kk + (n+1) * (n+1));
        int ans = 0;
        rep(a, n+1) {
            rep(b, n+1) {
                int index = binary_search(m - k[a] - k[b]);
                int tmp = kk[index] + k[a] + k[b];
                if(tmp <= m) ans = max(ans, tmp);
                tmp = kk[index+1] + k[a] + k[b];
                if(tmp <= m) ans = max(ans, tmp);

            }
        }
        cout << ans << endl;
    }

    return 0;
}

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

int n,m;
int target[1000];

int chek(int cnt, int point,int now)
{
	int i;
	int sum=0;

	if (cnt == 5)
	{
		return point;
	}

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

			/*
			num = chek(cnt + 1, point + target[i]);
			if (num <= m)
			{
				max = (max < num) ? max : num;
			}
			*/
		}
	}

	return sum;
}

int main()
{
	int sum;
	int i;

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

		sum = chek(1, 0,0);

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

	return 0;
}

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

int main() {
	long n,maxsc;
	long m,p[100000000];
	while(1){
		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>());
		maxsc = 0;
		int cn = 0;
		for(int i=0;i<n||cn<=4;i++){
			for(int j=cn;j<=4;j++){
				if(maxsc+p[i] <= m){
					maxsc = max(maxsc+p[i],maxsc);
					cn++;
				}
			}
			if(maxsc+p[i+7] >= m) break;
		}
		cout << maxsc << endl;
	}
	return 0;
}