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<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

//#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#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 all(a) (a).begin(),(a).end()
#define dump(o) if(DEBUG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DEBUG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
const bool DEBUG = false;

signed main() {
	int P[1010] = {};
	static int a[1000010] = {}; //1000010
	for (int N, M; cin >> N >> M&&N;) {
		memset(a, 0x3f, sizeof(a));
		rep(i, 0, N)cin >> P[i];
		P[N] = 0;
		N++;
		rep(i, 0, N)rep(j, 0, N) {
			a[i*N + j] = P[i] + P[j];
		}
		dumpc(a);
		sort(a, a + N*N);
		dumpc(a);
		int ans = 0;
		rep(i, 0, N*N) {
			if (a[i] > M)break;
			int add = a[upper_bound(a, a + N*N, M - a[i]) - a - 1];
			ans = max(ans, a[i] + add);
		}
		cout << ans << endl;
	}
	return 0;
}

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

int n, m;
int p[1001], dp[1001][5];
int main()
{
  while(true){
    //chap1
    cin >> n >> m;
    if(!n && !m) break;
    
    memset(dp, 0, sizeof dp);
    for(int i = 1; i <= n; i++)
      cin >> p[i];

    //chap2
    for(int i = 1; i <= n; i++)
      for(int j = 0; j <= 4; j++)
	for(int k = 0; j + k <= 4; k++)
	  if(dp[i - 1][j] + p[i] * k <= m)
	    dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] + p[i] * k);

    cout << dp[n][4] << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define EQ(a,b) (abs((a)-(b))<EPS)
int p[1001];
int ans;
vector<int> ab,cd;
int n,m;
int main()
{
	while(1)
	{
		memset(p,0,sizeof(p));
		ab.clear();cd.clear();
		cin >> n >> m;
		ans=0;
		if(n==0&&m==0)break;
		for(int i=0;i<n;i++)
		{
			cin >> p[i];
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				ab.pb(p[i]+p[j]);
				cd.pb(m-(p[i]+p[j]));
			}
		}
		sort(cd.begin(),cd.end());
		for(int i=0;i<sz(ab);i++)
		{
			vector<int>::iterator it=lower_bound(cd.begin(),cd.end(),ab[i]);
			ans=max((*it)*-1+m+ab[i],ans);
		}
		cout << ans << endl;
	}
	return 0;
}

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 <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 1005
#define LSIZE 1000005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;
ll M;
ll T[SIZE];
set<ll> S;
ll K;

ll solve()
{
	scanf("%d %lld",&n,&M);
	if(n+M ==0)return -1;
	for(int i=1;i<=n;i++)scanf("%lld",&T[i]);
	T[0]=0;
	S.clear();
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			S.insert(T[i]+T[j]);
		}
	}
	ll maxv=0;
	set<ll>::iterator ii;
	for(set<ll>::iterator it=S.begin();it!=S.end();it++){
		if((*it)<=M){
			ii = upper_bound(S.begin(),S.end(),M-(*it));
			ii--;
			maxv = max(maxv,(*it)+(*ii));
		}
	}
	return maxv;
}

int main()
{
	while(1){
		ll ans = solve();
		if(ans==-1)break;
		else printf("%lld\n",ans);
	}
	return 0;
}

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

#define ll int

void solve(){
    ll n,m;
    ll ans;

    while(cin>>n>>m,n|m){
        vector<ll> v,sum_v;

        for(ll i=0;i<n;i++){
            ll t;
            cin >> t;
            v.push_back(t);
        }

        v.push_back(0);

        for( auto t:v ){
            for( auto s:v ){
                sum_v.push_back(t+s);
            }
        }

        sort(sum_v.begin(),sum_v.end());

        ans=-1;
        for( auto t:sum_v ){
            if( binary_search(sum_v.begin(),sum_v.end(),m-t) ){
                ans=m;
                break;
            }else{
                auto it=upper_bound(sum_v.begin(),sum_v.end(),m-t);
                if( it != sum_v.begin() ){
                    ans=max(ans,t + *(--it));
                }
            }
        }

        cout<<ans<<endl;
    }
}

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

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

typedef long long ll;
int main() {
    while(true) {
        ll n, m;
        cin >> n >> m;
        if(!n && !m) break;
        vector<ll> v(n+1, 0);
        vector<ll> v2;
        for(int i=1;i<n+1;i++) cin >> v[i];
        for(int i=0;i<n+1;i++) {
            for(int j=i;j<n+1;j++) {
                v2.push_back(v[i]+v[j]);
            }
        }

        sort(v2.begin(), v2.end());
        ll ma = 0;
        for(int i=0;i<v2.size();i++) {
            vector<ll>::iterator itr = upper_bound(v2.begin(), v2.end(), m-v2[i]);
            int d = distance(v2.begin(), itr);
            if(d > 0) {
                ma = max<ll>(ma, v2[i]+v2[d-1]);
            }
        }
        cout << ma << endl;
    }
}