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+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++: #define _USE_MATH_DEFINES
#include<stdio.h>
#include<string>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<vector>
#include <algorithm>
#include<math.h>
#include<set>
#include<map>
#include<iomanip>
using namespace std;


int main(){
	
	int n,m;
	while(1){
		cin>>n>>m;
		if(!n&&!m)break;
		int g[1009]={};
		int w=2000000009;
		for(int i=0;i<n;i++)
		{cin>>g[i];w=min(w,g[i]);}
		sort(g,g+n);
		reverse(g,g+n);
		long long int ans=0,H=0;;
		int y=0;
		int r=3;
		int i=0;
		while(i<4){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
		}
		y=0;
		H=max(H,ans);
		r=2;i=0;ans=0;
		while(i<3){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
		}
		
		y=0;
		H=max(H,ans);
		r=1;i=0;ans=0;
		while(i<2){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
		}
		
		y=0;
		H=max(H,ans);
		r=0;i=0;ans=0;
		while(i<1){
			if(ans+g[y]+(r-i)*w<=m){ans+=g[y];i++;}
			else y++;
		}
		H=max(H,ans);


		cout<<H<<endl;
	}
	


	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=a;i<b;++i)
#define for_rev(i,a,b) for(int i=a;i>=b;--i)
#define allof(a) a.begin(),a.end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) (int)a.size()

int N, M;
int p[1010];

int main() {
	while (cin >> N >> M, N) {
		for_(i,0,N) cin >> p[i];
	
		vector<int> vp;
		vp.push_back(0);
		for_(i,0,N) vp.push_back(p[i]);
	
		vector<int> sum2;
		for_(i,0,N + 1) for_(j,0,N + 1) sum2.push_back(vp[i] + vp[j]);
	
		sort(allof(sum2));
		int s_size = size_of(sum2);
		int ans = 0;
		for_(i,0,s_size) {
			int sum = sum2[i];
			if (sum <= M) {
				int sub = M - sum;
				int lb = *(upper_bound(allof(sum2), sub) - 1);
				//cout << sub << " " << lb << endl;
				sum += lb;
				ans = max(ans, sum);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <set>
#include <algorithm>
#include <iostream>
#define MAX(a,b) (((a)>(b))?(a):(b))
using namespace std;
typedef long long int lli;

set<lli> v;
lli m,n;
lli k;
int mx;

int dfs(int num,int score,int z){
	mx=MAX(mx,score);
	if(num==z)return 1;
	if(num<z){
		for(set<lli>::reverse_iterator it=v.rbegin();it!=v.rend();it++){
			if(score+*it<=m){
				if(dfs(num+1,score+*it,z)){
					return 1;
				}
			}
		}
	}
	return 0;
}

int main(void){
	while(cin>>n>>m,(n||m)){
		v.clear();
		mx=0;
		for(lli i=0;i<n;i++){
			cin>>k;
			v.insert(k);
		}
//				cout<<"a:";
//				for(set<lli>::reverse_iterator it=v.rbegin();it!=v.rend();it++) cout<<*it<<" ";
//				cout<<endl;

		for(int f=1;f<5;f++){
			for(set<lli>::reverse_iterator itm=v.rbegin();itm!=v.rend();itm++){
				if(*itm<=m) dfs(1,*itm,f);
			}
		}
		cout<<mx<<endl;
	}
	return 0;
}

C++: #include<iostream>

#define NMAX 1000
#define MMAX 200000
using namespace std;

int main(void){

	std::ios_base::sync_with_stdio();

	int N,M;
	int max=-1;
	int dp[4][MMAX]={-1};
	int sc[NMAX];

	while(1){
		cin>>N>>M;
		if(N==0&&M==0) break;
		for(int i=0;i<N;i++){
			cin>>sc[i];
		}
		for(int i=0;i<4;i++){
			for(int j=0;j<MMAX;j++){
				dp[i][j]=-1;
			}
		}
		dp[0][0]=1;
		for(int j=0;j<=M;j++){
			for(int k=0;k<4;k++){
				if(dp[k][j]==1){
					for(int l=0;l<N;l++){
						if(j+sc[l]<=M){
							dp[k+1][j+sc[l]]=1;
							if(max<j+sc[l]) max=j+sc[l];
						}
					}
				}
			}
		}
		cout<<max<<endl;
	}


	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n >> m && n && m){
    vary(ll,s,n,0);
    rep(i,n){
      cin >> s[i];
    }
    sort(all(s));
    queue<ll> q;
    q.push(0);
    ll k = 4;
    ll ma = 0;
    while(k){
      auto S = q.size();
      rep(l,S){
        ll a = q.front();q.pop();
        ma = max(a,ma);
        rep(i,n){
          if(a + s[i] <= m)
            q.push(a+s[i]);
        }
      }
      --k;
    }
    while(q.size()){
      ma = max(ma,q.front());
      q.pop();
    }
    cout << ma <<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m,p[1001],ans;
  while(cin >> n >> m){
    if(n == 0 && m == 0) break;
    vector<int> vec1,vec2;
    for(int i=0;i<n;i++) cin >> p[i];
    p[n] = 0;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=n;j++){
	vec1.push_back(p[i]+p[j]);
	vec2.push_back(p[i]+p[j]);
      }
    }
    ans = 0;
    for(int i=0;i<vec1.size();i++){
      for(int j=0;j<vec2.size();j++){
	if(m >= vec1[i] + vec2[j]) ans = max(ans,vec1[i]+vec2[j]);
      }
    }
    cout << ans << endl;
  }
  return (0);
}

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

int main() {
    while(true) {
        int n,m; scanf("%d%d", &n, &m);
        if(n==0) break;
        static int ps[1001];
        static int pps[1002001];
        for(int i = 0; i < n; i++) scanf("%d", ps+i);
        ps[n++] = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                pps[i*n+j] = ps[i]+ps[j];
        sort(pps, pps+n*n);
        int max_val = 0;
        int x = 0;
        int y = n*n-1;
        while(x<n*n && y>=0) {
            if(pps[x]+pps[y]<=m) {
                max_val = max(max_val, pps[x]+pps[y]);
                x++;
            } else {
                y--;
            }
        }
        printf("%d\n", max_val);
    }
    return 0;
}