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<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);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;
int n,m;
vi a;
int dp[200000001];
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
    cin >> n >> m;
    if(n==0&&m==0)break;
    rep(i,0,20000001){dp[i]=0;}
    rep(i,0,n){
        int x;
        cin >> x;
        a.pb(x);
    }
    sort(ALL(a));
    dp[0]=1;
    rep(i,0,a.size()){
        for(int j=0;j<m;j++){
            rep(k,1,5){
                if((j+a[i]*k)<=m){
                    if(dp[a[i]*k+j]>0&&dp[j]>0)dp[a[i]*k+j]=min(dp[a[i]+j],dp[j]+1);
                    if(dp[j]>0)dp[a[i]*k+j]=dp[j]+1;
                }
            }
        }
    }
    int ans=0;
    rep(i,0,m+1){
        if(dp[i]!=0&&dp[i]<=4){
            ans=i;
            //cout << i << " " << dp[i] <<endl;
        }
    }
    //cout << dp[m-2] << endl;
    cout << ans << endl;
    }
    return 0;
}

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

class Solve {
private:

public:
	bool is_last_query{};
	Solve()
	{
		int N, M;
		scanf("%d%d", &N, &M);
		if (N == 0)
		{
			is_last_query = true;
			return;
		}
		std::vector<int> P(N);
		for (auto& e: P) scanf("%d", &e);
		std::vector<int> former;
		former.reserve((N + 1) * (N + 1));
		former.push_back(0);
		for (auto& e: P) former.push_back(e);
		for (int i{}; i < N; i++)
			for (int j{}; j < N; j++)
				if (P[i] + P[j] <= M)
					former.push_back(P[i] + P[j]);
		std::sort(former.begin(), former.end());
		former.erase(std::unique(former.begin(), former.end()), former.end());
		int max{};
		for (int i{}, pair{(int)former.size() - 1}; i < (int)former.size(); i++)
		{
			while (former[i] + former[pair] > M) pair--;
			max = std::max(max, former[i] + former[pair]);
		}
		printf("%d\n", max);
	}
};

int main()
{
	while (!Solve().is_last_query);

	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 = 1000001; T > 103; 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 <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#define INF 2000000000
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
#define ll long long
#define PL pair<long long, long long>
#define P pair<int,int>
#define mk make_pair
#define en endl;
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0;
#define HERE cout<<"here"<<en
#define _out(value) cout<<"      "<<value<<en
#define out(value) cout<<value<<en

#define MAX 100005
int half_1[MAX],half_2[MAX];

int main(){
  int n,m;
  while(cin>>n>>m&&n&&m){
    int p[n];
    vector<ll> v;
    v.push_back(0);
    for(int i=0; i<n; i++){
      cin>>p[i];
      v.push_back(p[i]);
    }
    
    for(int i=0; i<n; i++){
      for(int j=0; j<n; j++){
        if(p[i]+p[j]<=m){
          v.push_back(p[i]+p[j]);
        }
      }
    }
    
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()),v.end());
    
    ll  ans=0;
    for(int i=0; i<v.size(); i++){
      ll lb=lower_bound(v.begin(), v.end(), (ll)m-v[i])-v.begin()-1;
      if(v[i]+v[lb]<=m) ans=max(ans,v[i]+v[lb]);
    }
    
    out(ans);
  }
}

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

int main(){
  int i,j,k,l,n,m,a[1000],ans;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    for(i=0;i<n;i++) cin >> a[i];

    sort(a,a+n);
    ans = 0;

    for(i=n-1;i>=0;i--){
      if(a[i] <= m) ans = max(ans,a[i]);

      if(a[i] + a[0] < m && a[i]*4 > ans){
	for(j=i;j>=0;j--){
	  if(a[i] + a[j] <= m) ans = max(ans,a[i] + a[j]);
	  
	  if(a[i] + a[j] + a[0] < m && a[i] + a[j]*3 > ans){
	    for(k=j;k>=0;k--){
	      if(a[i] + a[j] + a[k] <= m) ans = max(ans,a[i] + a[j] + a[k]);
	      
	      if(a[i] + a[j] + a[k] + a[0] < m && a[i] + a[j] + a[k]*2 > ans){
		for(l=k;l>=0;l--){
		  if(a[i] + a[j] + a[k] + a[l] <= m){
		    ans = max(ans,a[i] + a[j] + a[k] + a[l]);
		    break;
		  }
		}
	      }
	    }
	  }
	}
      }
    }

    cout << ans << endl;

  }
  return 0;
}