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++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;

int n, m;
int nearabs(int x, int y) {
	if (x > m)return y;
	if (y > m)return x;

	if (m - x > m - y)return y;
	else return y;
}
signed main() {
	while (cin >> n >> m, n || m) {
		vector<vector<int>>dp(n + 1, vector<int>(n + 1, 0));
		vector<int>v(n + 1); rep(i, 1, n + 1)cin >> v[i];
		
		rep(i, 1, n + 1) {
			rep(j, 1, n + 1) {
				if (dp[i][j - 1] + v[i] < m)dp[i][j] = max(dp[i - 1][j], dp[i][j - 1] + v[i]);
				else {
					rep(k, 1, i + 1) {
						if (dp[i][j - 1] + v[k] < m)dp[i][j] = max(dp[i][j], dp[i][j - 1] + v[k]);
					}
				}
			}
		}
		cout << dp[n][n] << endl;

	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
	vector<int> sum;
	int n,m;
	int a[1000];
	while(cin>>n>>m){
		int ans=0;
		a[0]=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a,a+n+1);
		for(int i=0;i<=n;i++){
			for(int j=i;j<=n;j++){
				if(m>=a[i]+a[j])sum.push_back(a[i]+a[j]);
			}
		}
		sort(sum.begin(),sum.end());
		int s=sum.size()-1;
		for(int i=0;i<=s;i++){
			while(sum[i]+sum[s]>m)s--;
			if(ans<sum[i]+sum[s])ans=sum[i]+sum[s];
		}
		cout<<ans<<endl;
	}
}

C++: #include <iostream>
#include <stdlib.h>
using namespace std;
 
int main() {
	for(;;){
		/*入力*/
		int N,M,count=0;
		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;
			}
		}
 
		/*４投に拡張*/
		for(int i=0;i<=M;i++){
			if(array[i]==1)count++;
		}
		int k=0,p[count];
		for(int i=0;i<=M;i++){
			if(array[i]==1){
				p[k]=i;
				k++;
			}
		}
		for(int i=0;i<count;i++){
			for(int j=0;j<count;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<iostream>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<cstdlib>
#include<typeinfo>
#include<sstream>
#include<list>
#include<set>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define fori(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
int in()
{
	int x;cin>>x;
	return x;
}
int stringInt(string a)
{
	return atoi(a.c_str());
}
string intString(int a)
{
	stringstream ss;
	ss<<a;
	return ss.str();
}
vector<int> SieveOfEratosthenes(int x)
{
	vector<int> a;
	vector<int> b(x+1,0);
	for(int i=2;i<=x;i++)
	{
		if(b[i]==0)
		{
			a.push_back(i);
			for(int l=i;l<=x;l+=i)
			{
				b[l]=1;
			}
		}
	}
	return a;
}

int main ()
{
	int n,m;
	while(cin>>n>>m&&n&&m)
	{
		vector<long long > a,b;
		b.push_back(0);
		rep(i,n)
		{
			b.push_back(in());
		}
		rep(i,n+1)
		{
			rep(l,n+1)
			{
				a.push_back(b[i]+b[l]);
			}
		}
		long long ans = 0;
		sort(a.begin(),a.end());
		rep(i,a.size())
		{
			ans=max(ans,*(upper_bound(a.begin(),a.end(),m-a[i])-1)+a[i]);
		}
		cout<<ans<<endl;


	}

}

C++: #include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define mp(x,y) make_pair(x,y)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int,int>
#define INF 1<<29
#define MAX 1010
using namespace std;

int p[MAX];

void solve(){
 int n,m,ind;
 for(;;){
  cin>>n>m;
  if(!n) break;
  int ma=0;
  vector<int> v;
  FOR(i,n){
   cin>>p[i];
   v.pb(p[i]);
  }
  FOR(i,n){
   FORR(j,i,n){
    v.pb(a[i]+a[j]);
   }
  }
  sort(v.begin(),v.end());
  erase(unique(v.begin(),v.end()),v.end());
  ind=upper_bound(v.begin(),v.end(),m)-1;
  if(ind>=0) ma=v[ind];
  FOR(i,v.size()){
   ind=upper_bound(v.begin(),v.end(),m-v[i])-1;
   if(ind>=0) ma=max(ma,v[i]+v[ind]);
  }
  cout<<ma<<endl;
 }
}

int main(){
 solve();
 return 0;
}

C++: 
#include <stdio.h>
#include <algorithm>

using namespace std;

int n, m;
int p[1001];
int sp[1001*1001];

int main()
{
	for (;;)
	{
		scanf("%d %d", &n, &m);
		if (!n && !m)break;
		for (int i = 0; i < n; ++i)
		{
			scanf("%d", &p[i]);
		}
		p[n] = 0;
		int it = 0;
		for (int a = 0; a <= n; ++a)
		{
			for (int b = 0; b <= a; ++b)
			{
				sp[it++] = p[a] + p[b];
			}
		}
		sort(sp, sp + it);
		int *fr = sp;
		int *bk = sp + it - 1;
		int ans = 0;
		while(fr <= bk)
		{
			if ((*fr) + (*bk) <= m)
			{
				ans = max(ans, (*fr) + (*bk));
				++fr;
			}
			else
			{
				--bk;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

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

int main(){
  int n, m;
  while(1){
    scanf("%d%d", &n, &m);
    if(n == 0 && m == 0)return 0;
    int res = 0, tmp;
    vector<int> points, points2;
    for(int i = 0;i < n;i++){
      scanf("%d", &tmp);
      points.push_back(tmp);
    }
    set<int>::iterator it1,it2;
    for(it1 = points.begin();it1 != points.end();it1++){
      for(it2 = points.begin();it2 != points.end();it2++){
	points2.push_back(*it1 + *it2);
      }
    }
    sort(points2.begin(),points2.end());
    for(it1 = points2.begin();it1 != points2.end();it1++){
      it2 = upper_bound(points2.begin(), points2.end(), m - *it1);
      if(it2 == points2.begin())continue;
      it2--;
      res = max(res, *it1 + *it2);
    }
    printf("%d\n",res);
  }
}