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 <vector>
#include <algorithm>
using namespace std;
typedef long long ll;

int main(){
	int n;
	ll m;
	while( cin >> n >> m, n || m ){
		vector<ll> vp(n+1);	// 入力用
		vp[0] = 0;
		for( int i = 0; i < n; i++ ){
			cin >> vp[i+1];
		}

		vector<ll> points;	// 2本投げた時のすべての得点の組み合わせを格納
		for( int i = 0; i <= n; i++ ){
			for( int j = i; j <= n; j++ ){
				points.push_back(vp[i]+vp[j]);
			}
		}
		sort(points.begin(), points.end());

		ll ans = 0;
		// 残りの2本を投げたときの最高得点を求めていく
		for( int i = 1; i < points.size(); i++ ){
			ll diff = m - points[i];
			if( diff < 0 || points[i] == points[i-1] ) continue;	// 初めの2本で既にm以上の場合と同点のものを無視する
			// 二分探索
			int left = 0, right = points.size()-1;
			while( 1 ){
				ll next = (left + right) / 2;
				if( next == left || next == right ){
					if( points[next] <= diff ) ans = max(ans, points[i] + points[next] );
					break;
				}
				if( points[next] == diff ){
					ans = points[i] + points[next];
					break;
				}else if( points[next] < diff ){
					left = next;
				}else{
					right = next;
				}
			}
		}
		cout << ans << endl;
	}
}

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

int max(int x, int y);
int add(int x, int y, int m);

int main(){
  int n, m;
  vector<int> ans;
  while(cin >> n >> m, n&&m){
    int dp[4][n];
    for(int i = 0; i < 4; i++){
      for(int j = 0; j < n; j++){
	dp[i][j]  = 0;
      }
    }
    for(int i = 0; i < n; i++){
      cin >> dp[0][i];
    }

    for(int i = 1; i < 4; i++){
      for(int j = 0; j < n; j++){
	dp[i][j] = dp[i-1][j];
	for(int k = 0; k < n; k++){
	  dp[i][j] = max(add(dp[i-1][j], dp[0][k], m), dp[i][j]);
	}
      }
    }
    int max = 0;
    for(int i = 0; i < n; i++){
      if(dp[3][i] > max) max = dp[3][i];
    }
    ans.push_back(max);
  }
  vector<int>::iterator it;
  for(it = ans.begin(); it != ans.end(); it++) cout << *it << endl;
}


int max(int x, int y){
  return x < y ? y : x;
}

int add(int x, int y, int m){
  return x+y < m ? x+y : 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
int main() {
	ll a[1000], n, m;
	while (cin >> n >> m&&n != 0 && m != 0) {
		for (int i = 0; i < n; i++) cin >> a[i];
		sort(a, a + n);
		int t = 0;
		for (t; t < n; t++) {
			if (a[t] > m) break;
		}
		long long sum = 0;
		for (int i = 0; i < t; i++) {
			sum = max(sum, a[i]);
			for (int j = i; j < t; j++) {
				if (a[i] + a[j] <= m) {
					sum = max(sum, a[i] + a[j]);
					for (int x = j; x < t; x++) {
						if (a[i] + a[j] + a[x] <= m) {
							sum = max(a[i] + a[j] + a[x], sum);
							for (int y = x; y < t; y++) {
								if (a[i] + a[j] + a[x] + a[y] <= m) {
									sum = max(a[i] + a[j] + a[x] + a[y], sum);
								}
								else break;
							}
						}
						else break;
					}
				}
				else break;
			}
		}
		cout << sum << endl;
	}
}

C++: //Solution for AOJ 0529: Darts 
#include <iostream>
#include <queue>
using namespace std;
const int MAX_M = 100000000;
int n, m, p[1000], save;
bool  dp[MAX_M+1];
queue <int> que;
int main() {
	while (cin >> n >> m){
		for (int i = 0; i<n; i++){
			cin >> p[i];
		}
		for(int i=0;i<m;i++){
			dp[i]=0;
		}
		dp[0] = 1;
		que.push(0);
		for (int i = 0; i<n; i++){
			dp[p[i]] = 1;
			que.push(p[i]);
		}
		for (int i = 1; i<4; i++){
			save=que.size();
			for (int k=0;k<save;k++){
				for (int j = 0; j < n; j++){
					if (que.front() + p[j] < m){
						dp[que.front() + p[j]] = 1;
						que.push(que.front() + p[j]);
					}
				}
				que.pop();
			}
		}
		for (int i = m - 1; i >= 0; i--){
			if (dp[i] != 0){
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(int i=a; i<int(b); i++)
#define rep(i,b) loop(i,0,b)
#define each(e,c) for(auto& e:c)
#define iter(it,c) for(auto it=c.begin(); it!=c.end(); ++it)

ll const inf = 1<<28;
double const pi = acos(-1);

int a[1024];
int d[1024*1024];

// 整数二分探索,
// int res=l;
// for(int d=(inf); d; d>>=1) if(res+d <= r && ok(res+d)) res += d;
// すると間違えない. (n回目)
int solve(int n, int k){
    int c=0;
    rep(i,n)loop(j,i,n){
        int s=a[i]+a[j];
        if(s<=k){
            d[c++]=s;
        }
    }

    sort(d,d+c);
    int ans=0;
    rep(i,c){
        int s1=d[i];
        int l=0, r=c;
        int res=l;
        for(int w=1<<22;w;w>>=1){
            bool ok;
            if(res+w>=r) ok=false;
            else{
                int s2=d[res+w];
                ok=(s1+s2<=k);
            }
            if(res+w<r && ok) res+=w;
        }
        ans=max(ans, s1+d[res]);
    }
    return ans;
}

int main(){
    int n,k;
    while(cin>>n>>k && n|k){
        a[0]=0;
        rep(i,n)cin>>a[i+1];
        n++;
        cout << solve(n,k) << endl;
    }
}

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


int n,m,d[1003],t[1010005],ans;

int main(){
	while(1){
	scanf("%d%d",&n,&m);
	if(n + m == 0)break;
	ans = 0;
	for(int i = 1;i <= n;i++){
		scanf("%d",&d[i]);
	}

	int co = 0;

	for(int i = 0;i <= n;i++){
		for(int j = 0;j <= n;j++){
			t[co++] = d[i] + d[j];
		}
	}
	sort(t,t + co);

	for(int i = 0;i <= n;i++){
		for(int j = i;j <= n;j++){
			int tmp = d[i] + d[j];
			if(tmp > m)continue;
			int lb = 0,ub = co;
			while(ub - lb > 1){
				int mid = (ub + lb)/2;
				if(t[mid] + tmp <= m)lb = mid;
				else ub = mid;
			}
			ans = max(ans,tmp+t[lb]);
		}
	}

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

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

#define INF 100000000
int sum[1000*1000];
int v[1000];
int n, m;

int main(){
    int n, m;
    while(cin >> n >> m, n||m){
        
        for(int i=0; i<n; ++i){
            int a; cin >> a;
            v[i] = a;
        }
        sort(v, v + n);
        
        int maxV=0;
        
        for(int i=0; i<n; ++i){
            for(int j=0; j<n; ++j){
                sum[i*n+j] = v[i]+v[j];
                //cout << sum[i*n+j] << end;
            }
        }
        sort(sum, sum+n*n);
        for(int i=0; i<n; ++i){
            for(int j=0; j<n; ++j){
                for(int i=0; i<=m-v[i]-v[j]; i++){
                    auto t = sum[upper_bound(sum,sum + n*n, m-v[i]-v[j])-sum-1];
                    if(v[i]+v[j]+t<=m) {
                        maxV=max(maxV,v[i]+v[j]+t);
                    }
                }
            }
        }
        cout << maxV << endl;
    }
}