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

long long int solve(long long int a, vector<long long int>& side_b, long long int m){
  int left = 0, right = side_b.size() - 1;
  while(right - left > 1){
    int middle = (right + left) / 2;
    if(side_b[middle] + a <= m){ left = middle; }
    else{ right = middle; }
  }
  return a + side_b[left];
}


int main(void){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){ break; }

    vector<long long int> input(n);
    for(int i = 0; i < n; i++){ cin >> input[i]; }

    vector<long long int> side_a;
    for(int i = 0; i < n; i++){
      side_a.push_back(input[i]);
      for(int j = 0; j < n; j++){
        side_a.push_back(input[i] + input[j]);
      }
    }
    sort(side_a.begin(), side_a.end());
    side_a.push_back(side_a[side_a.size() - 1]);

    long long int ans = 0;
    for(size_t i = 0; i < side_a.size(); i++){
      long long int a = side_a[i];
      long long int tmp = solve(a, side_a, m);
      if(tmp <= m){ ans = max(ans, tmp); }
    }
    
    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

#define MAX(a,b) ((a)>(b)?(a):(b))

int main()
{
    int N;
    long long M;

    cin >> N >> M;

    while(N!=0 && M!=0) {
        vector<int> p(N);
        set<long long> st[2];

        long long ans=0;

        for(int i=0; i<N; ++i) {
            cin >> p[i];
        }

        sort(p.begin(),p.begin()+N);

        for(int i=0; i<N; ++i) {
            if(p[i]<M) st[0].insert(p[i]);
            ans=MAX(ans,p[i]);
        }

        for(int k=1; k<4; ++k) {
            for(auto itr=st[(k-1)%2].begin(); itr!=st[(k-1)%2].end(); ++itr) {
                auto p_itr=upper_bound(p.begin(),p.end(),M-*itr);
                if(p_itr==p.begin()) {
                    st[(k-1)%2].clear();
                    break;
                } else {
                    --p_itr;
                }
                ans=MAX(ans,*itr+*p_itr);
                st[k%2].insert(*itr+*p_itr);
            }
        }

        cout << ans << endl;


        cin >> N >> M;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int N;
	ll M;
	cin >> N >> M;
	if (N == 0 && M == 0) return 0;
	vector<ll> P(N + 1);
	REP(i, N) cin >> P[i];
	P[N] = 0;
	vector<ll> sum((N + 1) * (N + 1));
	REP(i, N + 1)REP(j, N + 1) sum[(N + 1) * i + j] = P[i] + P[j];
	sort(sum.begin(),sum.end());
	ll res = 0;
	REP(i, N + 1)REP(j, N + 1) {
		if (sum[(N + 1) * i + j] <= M) {
			res = max(res,sum[(N + 1) * i + j]);
			int n = upper_bound(sum.begin(), sum.end(), M - sum[(N + 1) * i + j]) - sum.begin() - 1;
			if (n >= 0) res = max(res, sum[(N + 1) * i + j] + sum[n]);
		}
	}
	cout << res << "\n";

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 1002

int p[MAX], dp[MAX];
int main(){
	int n, m, ans;
	while(cin >> n >> m, n){
		fill(dp, dp+MAX, 0);
		for(int i = 0;i < n;i++)cin >> p[i];
		ans = 0;
		
		for(int i = 0;i < 3;i++){
			dp[0] = 5;
			for(int j = p[i];j <= m;j++){
				dp[j] = max(dp[j], dp[j-p[i]]-1);
				//if(dp[j])ans = max(ans, j);
				if(dp[j] && ans < j)ans = j;
			}
		}
		//for(int i = 1;i <= 50;i++)cout << dp[i] << endl;
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int N, M;
vector<int> P;

int solve(){
    vector<int> sums(N*N+N+2);
    for(int i=0; i<N; ++i){
        for(int j=0; j<N; ++j){
            sums[i*N+j]=P[i]+P[j];
        }
    }
    for(int i=0; i<N; ++i){
        sums[N*N+i]=P[i];
    }
    sums[N*N+N]=0;
    sums[N*N+N+1]=INT_MAX;
    sort(sums.begin(), sums.end());

    int ans;
    auto p=lower_bound(sums.begin(), sums.end(), M);
    if(*p==M) return M;
    else ans=*(p-1);

    for(int i=0; i<N; ++i){
        for(int j=0; j<N; ++j){
            int rest=M-P[i]-P[j];
            if(rest==0) return M;
            else if(rest<0);
            else{
                auto p=lower_bound(sums.begin(), sums.end(), rest);
                if(*p==rest) return M;
                else ans=max(ans, *(p-1)+P[i]+P[j]);
            }
        }
    }
    return ans;
}

int main(){
    while(true){
        cin >> N >> M;
        if(N==0 && M==0) break;
        P.resize(N);
        for(int i=0; i<N; ++i){
            cin >> P[i];
        }
        cout << solve() << endl;
    }
    return 0;
}

C++: //12
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
  for(int n,m;cin>>n>>m,n|m;){
    int p[1001];
    for(int i=0;i<n;i++){
      cin>>p[i];
    }
    p[n]=0;
    static int pd[1001*1001];
    for(int i=0;i<n+1;i++){
      for(int j=0;j<n+1;j++){
	pd[i*(n+1)+j]=p[i]+p[j];
      }
    }
    sort(pd,pd+(n+1)*(n+1));
    int r=0;
    for(int i=0;i<(n+1)*(n+1);i++){
      int x=lower_bound(pd,pd+(n+1)*(n+1),m-pd[i])-pd;
      if(pd[i]+pd[x]==m){
	r=m;
      }else if(x>0){
	r=max(r,pd[i]+pd[x-1]);
      }
    }
    cout<<r<<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 > 102; 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;
}