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<iostream>
#include<algorithm>
using namespace std;
bool ok(int *q,int m,int h,int count){
  for(int i=0;i<count;i++){
    if(q[i]>m) continue;
    for(int j=0;j<count;j++){
      if(q[i]+q[j]<=m&&q[i]+q[j]>h) return true;
    }
  }
  return false;
}
int main(){
  int n,m;
  while(cin>>n>>m){
    if(n==0&&m==0) return 0;
    int data[n+1];
    int q[(n+1)*(n+1)/2];
    for(int i=0;i<n;i++) cin >> data[i];
    data[n] = 0;
    int count = 0;
    for(int i=0;i<=n;i++){
      for(int j=i;j<=n;j++){
	q[count] = data[i] + data[j];
	count++;
      }
    }
    sort(q,q+count,greater<int>());
    int min=0,max=m;
    while(min!=max){
      int h = (min+max)/2;
      if(ok(q,m,h,count)) min = h+1;
      else max = h;
    }
    cout << max << endl;
  }
}

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

using namespace std;

int main(void)
{
	int n, m;
	int s;
	
	while (1){
		
		vector<int> p;
	vector<int> s2;
	
	scanf("%d %d", &n, &m);
	
	if (n == 0 && m == 0){
		break;
	}
	
	for (int i = 0; i < n; i++){
		int k;
		scanf("%d", &k);
		p.push_back(k);
	}
	
	p.push_back(0);
	for (int i = 0; i < p.size(); i++){
		for (int j = 0; j < p.size(); j++){
			s2.push_back(p[i] + p[j]);
		}
	}
	
	sort(s2.begin(), s2.end());
	int max = -1;
	for (int i = 0; i < s2.size(); i++){
		vector<int>::iterator it = upper_bound(s2.begin(), s2.end(), m - s2[i]);
		//printf("%d\n", *(it - 1));
		
		if (it != s2.begin() && max < s2[i] + *(it - 1)){
			max = s2[i] + *(it - 1);
		}
		else if (max < s2[i]) {
			max = s2[i];
		}
	}
	
	printf("%d\n", max);
	
	}
	
	return (0);
}

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

using namespace std;

typedef pair<int,int> P;
typedef long long ll;
const int MAX = 2;
const ll INF = (1LL<<60);

int N,M;
vector<ll> V[MAX];
ll ans;
void init(){
  for(int i = 0; i < MAX; i++)
    V[i].clear();
  ans = 0;
}


void solve(){

  sort(V[0].begin(),V[0].end());

  for(int i = 0; i < V[0].size(); i++)
    for(int j = 0; j < V[0].size(); j++)
      V[1].push_back(V[0][i]+V[0][j]);

  V[1].push_back(INF);
  sort(V[1].begin(),V[1].end());

  vector<ll> :: iterator ite1,ite2;

  for(int i = 0; i < V[0].size(); i++)
    for(ite1 = V[1].begin(); ite1 != V[1].end(); ite1 = upper_bound(V[1].begin(),V[1].end(),*ite1))
      if(*ite1 + V[0][i] <= M) ans = max(ans,*ite1+V[0][i]);
      else break;

  for(ite1 = V[1].begin(); ite1 != V[1].end(); ite1 = upper_bound(V[1].begin(),V[1].end(),*ite1))
    for(ite2 = V[1].begin(); ite2 != V[1].end(); ite2 = upper_bound(V[1].begin(),V[1].end(),*ite2))
      if(*ite1 + *ite2 <= M) ans = max(ans,*ite1+*ite2);
      else break;
  

  for(int i = 0; i < MAX; i++){
    vector<ll> :: iterator ite;
    ite = upper_bound(V[i].begin(),V[i].end(),M);
    ans = max(ans,*(ite-1));
  }
  cout << ans << endl;
}

int main(){
  while(cin >> N >> M && N+M){
    init();
    for(int i = 0; i < N; i++){
      ll in;
      cin >> in;
      V[0].push_back(in);
    }
    solve();
  }
  return 0;
}

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

int main(){
  long n,m,p[1001];
  while(cin>>n>>m,n||m){
    for(int i=1;i<=n;++i) cin>>p[i];
    p[0]=0;
    vector<long> s(0);
    for(int i=0;i<=n;++i){
      for(int j=0;j<=n;++j) s.push_back(p[i]+p[j]);
    }
    sort(s.begin(),s.end());
    long a=0;
    for(vector<long>::iterator it=s.begin();*it<m/2;++it){
      a=max(a,*it+*(upper_bound(s.begin(),s.end(),m-*it)-1));
    }
    cout<<a<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, p[1010];
int dp[5][1010];

int main(){
	while (cin >> n >> m&&n + m){
		REP(i, 5)REP(j, 1010)dp[i][j] = 0;
		p[0] = 0;
		FOR(i, 1, n + 1)cin >> p[i];
		REP(i, 4){
			REP(j, n + 1){
				int buf = -1;
				REP(k, j + 1){
					if (dp[i][j] + p[k] <= m){
						buf = max(buf, p[k]);
					}
				}
				dp[i + 1][j] = dp[i][j] + buf;
			}
		}
		//REP(i, 5){
		//	REP(j, n + 1){
		//		cout << dp[i][j] << " ";
		//	}
		//	cout << endl;
		//}
		int ans = -1;
		REP(i, n + 1)ans = max(ans, dp[4][i]);
		cout << ans << endl;
	}
}

C++: #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>

using namespace std;

struct Info{
	Info(){
		number = 0;
	}
	int number;
};

void func(int N,int M){
	Info info[N];

	for(int i = 0; i < N; i++){
		scanf("%d",&info[i].number);
	}

	for(int i = 1; i <= N-1; i++){
		for(int k = N-1; k >= i; k--){
			if(info[k].number < info[k-1].number)swap(info[k],info[k-1]);
		}
	}

	int sum = 0,left,right,m,add;

	for(int loop = 0; loop < 4; loop++){
		left = 0; right = N-1,add=0;
		m = (left+right)/2;
		while(left < right){
			if(sum + info[m].number <= M){ //????????§????????????????¶??????????M????¶????????????°?????¢???
				add = info[m].number;
				left = m+1;
			}else{
				right = m-1;
			}
			m = (left+right)/2;
		}
		sum += add;
	}

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

}

int main(){

	int N,M;

	while(true){
		scanf("%d %d",&N,&M);
		if(N == 0 && M == 0)break;

		func(N,M);
	}

	return 0;
}

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

using namespace std;

int main(void)
{
   int n,m,input,s;
   vector<int>a;
   while(scanf("%d%d",&n,&m) ,n,m)
   {
      a.clear();
      a.push_back(0);
      for(int i = 0;i < n;i++)
      {
         scanf("%d",&input);
         a.push_back(input);
      }

      s = a.size();
      for(int i = 0;i < s;i++)
         for(int j = 0;j < s;j++)
            if(a[i] + a[j] <= m) a.push_back(a[i]+a[j]);

      s = a.size();
      for(int i = 0;i < s;i++)
         for(int j = 0;j < s;j++)
            if(a[i]+a[j] < m) a.push_back(a[i]+a[j]);

      s = a.size();
      int most = 0;
      for(int i = 0;i < s;i++)
         if(most < a[i]) most = a[i];

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


   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++;
				}
			}
		}
		cout << maxsc << endl;
	}
	return 0;
}