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 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 <vector>
#include <algorithm>

using namespace std;

int main() {
	long long int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector<long long int> data;
		long long int b;
		data.push_back(0);
		for (long long int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				bool hantei = true;
				for (long long int j = 0; j < data.size(); j++) {
					if (data[j] == b) {
						hantei = false;
						break;
					}
				}
				data.push_back(b);
			}
		}
		sort(data.begin(), data.end());
		vector<long long int> data2;
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		for (long long int i = 0; i < data2.size(); i++) {
			long long int a = data2[i];
			data.push_back(a);
			for (long long int j = i+1; j < data2.size(); j++) {
				if (data2[j] == a) {
					i++;
				} else {
					break;
				}
			}
		}
		data2.clear();
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		long long int sz = data2.size();
		cout << data2[sz-1] << 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 = 1000000; T > 100; 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 += 100000000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*10)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

int main() {
	int N, M; cin >> N >> M;
	static int dp[200000001];
	memset(dp, 5, sizeof(dp));
	dp[0] = 0;

	for (int i = 0; i < N; i++) {
		int P; cin >> P;
		for (int j = 0; j <= M; j++) {
			if (dp[j] >= 0)
				dp[j + P] = min(
					dp[j + P],
					dp[j] + 1
				);
		}
	}
	for (int j = M; j >= 0; j--) {
		if (dp[j] >= 0 && dp[j] <= 4) {
			cout << j<< endl;
			break;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD=1e9+7;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n,m;
  while(true){
      cin>>n>>m;
      if(n==0&&m==0) break;
      std::vector<ll> v(n),kouho;
      for(int i=0;i<n;i++) cin>>v[i];
      v.push_back(0);
      for(int i=0;i<=n;i++)for(int j=0;j<=n;j++){
          kouho.push_back(v[i]+v[j]);
      }
      ll ma=-1;
      sort(kouho.begin(),kouho.end());
      for(int i=0;i<(int)kouho.size();i++){
          if(i>0&&kouho[i]==kouho[i-1]) continue;
          ll res=m-kouho[i];
          if(res<0) break;
          auto itr=upper_bound(kouho.begin(),kouho.end(),res);
          itr--;
          ma=max(kouho[i]+*itr,ma);
          //cout<<ma<<endl;
      }
      cout<<ma<<endl;
  }
  return 0;
}

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

	int tsP[1001 * 1001];
	int main() {
		int N, M;
		int P[1001];
		int ans;
		int cnt;

		P[0] = 0;

		//cin >> N >> M;
		while (cin>>N>>M,N&&M) {
			ans = 0;
			cnt = 0;
			for (int i = 1; i <= N; i++) {
				cin >> P[i];
			}

			for (int i = 0; i <= N; i++) {
				for (int j = i; j <= N; j++) {
					if (P[i] + P[j] <= M) {
						tsP[cnt++] = P[i] + P[j];
					}

				}
			}
			sort(tsP, tsP + cnt);
			for (int i = 0; i < cnt; i++) {
				//for (int j = i; j < cnt; j++) {
					//if (tsP[i] + tsP[j] <= M) {
						ans = max(ans, /*tsP[i] + tsP[j]*/tsP[i] + *(upper_bound(tsP, tsP + cnt, M - tsP[i]) - 1));
					//}
				//}
			}
			cout << ans << endl;
		}
		return 0;
	}

C++: #include <iostream>
#include <algorithm>
#define N1 1001
using namespace std;
int N,M,P[N1],data[N1][N1][3];
int main(){
  int i,j,maxv,k;
  while(1){
    cin >> N >> M;
    if(N==0&&M==0) break;
    for(i=0;i<N;i++) cin >> P[i];
    for(i=0;i<N;i++){
      for(j=i;j<N;j++){
	data[i][j][0]=P[i]+P[j];
	data[i][j][1]=P[i]+P[j]*2;
	data[i][j][2]=P[i]+P[j]*3;
      }
    }
    maxv=0;
    for(i=0;i<N;i++){
      for(j=i;j<N;j++){
	for(k=0;k<3;k++){
	  if(data[i][j][k]<=M) maxv=max(maxv,data[i][j][k]);
	}
      }
    }
    cout << maxv << endl;
  }
  return 0;
}

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

int main()
{
  int N, M;
  while (cin >> N >> M && N != 0) {
    vector<int> ps(N);
    for (int i = 0; i < N; i++) {
      cin >> ps[i];
    }

    vector<int> v(1, 0);
    for (int i = 0; i < N; i++) {
      v.push_back(ps[i]);
      for (int j = i; j < N; j++) {
        if (ps[i] + ps[j] <= M) {
          v.push_back(ps[i] + ps[j]);
        }
      }
    }
    sort(v.begin(), v.end());

    int ans = 0;
    for (vector<int>::const_iterator it(v.begin()); it != v.end(); ++it) {
      vector<int>::const_iterator jt = lower_bound(v.begin(), v.end(), M - *it);
      if (jt != v.begin()) {
        --jt;
        ans = max(ans, *it + *jt);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main() {