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<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<map>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

vector<int> get;
int main(){
  while(1){
  int n,m;
  cin>>n>>m;
  if(n==0)break;
  get.clear();
  vector<int> dat;
  dat.push_back(0);
  rep(i,n){
    int a;
    cin>>a;
    dat.push_back(a);
  }
  n++;

  rep(j,dat.size()){
    for(int k=0;k<dat.size();k++){
      get.push_back(dat[j]+dat[k]);
    }
  }
  sort(get.begin(),get.end());

  int ans=0;
  /*  rep(j,get.size()){
    printf("%d\n",get[j]);
    }puts("");*/
  rep(i,get.size()){
    if(m-get[i]<0)continue;
    int t=*(upper_bound(get.begin(),get.end(),m-get[i])-1);
    ans=max(ans,t+get[i]);
  }
  printf("%d\n",ans);
}
}

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

#define MAX_N 1001

using namespace std;

class CNum{
public:
	vector<int> v;
};

bool Find(vector<int> &v, int search){
	for(unsigned int i = 0; i < v.size(); i++){
		if( v[i] == search ){
			return true;
		}
	}
	return false;
}

int main(void){
	while( 1 ){
		int n, m;
		int V[MAX_N] = {0,};
		CNum D[MAX_N][4];
		int Max = 0;

		cin >> n >> m;

		if( n == 0 && m == 0 ){
			break;
		}

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

		sort(V, V + n);
		reverse(V, V + n);

		if( m >= V[0] * 4 ){
			cout << V[0] * 4 << '\n';
			continue;
		}else if(m < V[n-1] ){
			cout << 0 << '\n';
			continue;
		}else{
			for(int i = 0; i < n; i++){
				for(int j = 0; j < 4; j++){
					if( j == 0 ){
						D[i][j].v.push_back( V[i] );
					}else if( j > 0 ){
						for(int k = 0; k <= i; k++){
							for(unsigned int l = 0; l < D[k][j - 1].v.size(); l++ ){
								int tmp = V[i] + D[k][j - 1].v[l];
								if( m - 4 * V[0] <= tmp && tmp <= m){
									if( !Find(D[i][j].v, tmp) ){
										D[i][j].v.push_back( tmp );
									}
								}
							}
						}
					}
				}
			}
		}

		for(int i = 0; i < n; i++){
			//cout << i << '\n';
			for(int j = 0; j < 4; j++){
				//cout << " " << j << ":" << '\n';
				for(unsigned int k = 0; k < D[i][j].v.size(); k++ ){
					//cout << D[i][j].v[k] << ' ';
					int tmp = D[i][j].v[k];
					if( tmp <= m && Max < tmp){
						Max = tmp;
					}
				}
				//cout << '\n';
			}
		}
		cout << Max << '\n';
	}
	return 0;
}

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

int main(){
	/*cout << "1000 200000000" << endl;
	for(int i=1;i<=1000;i++){
		cout << i << endl;
	}
	cout << "0 0" << endl;
	return 0;*/
	int n,m,t;
	while(cin >> n >> m, n){
		vector<int> bin;
		vector<int> data;
		for(int i=0;i<n;i++){
			cin >> t;
			data.push_back(t);
		}
		
		set<int> dp[4];
		
		for(int i=0;i<n;i++){
			dp[0].insert(data[i]);
		}
		
		for(int k=1;k<4;k++){
			for(int i=0;i<n;i++){
				for(set<int>::iterator it = dp[k-1].begin(); it != dp[k-1].end();it++){
					dp[k].insert((*it)+data[i]);
					bin.push_back((*it)+data[i]);
				}
			}
		}
		
		for(int k=0;k<4;k++){
			for(set<int>::iterator it = dp[k].begin(); it != dp[k].end();it++){
				bin.push_back((*it));
			}
		}
		sort(bin.begin(),bin.end());
		
		cout << *(lower_bound(bin.begin(), bin.end(), m)-1) << endl;
		
		
	}
}

C++: #include<stdio.h>
#include<math.h>
#include<vector>
#include<stack>
#include<set>
#include<string.h>
#include<iostream>
#include<algorithm>
#define MAXSIZE 1005
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int main()
{
    set<int,greater<int> >S;
    vector<int>v;
    int a[MAXSIZE];
    int n,m,ans;
    while(scanf("%d%d",&n,&m),n+m)
    {
        ans = 0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            v.push_back(a[i]),S.insert(a[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
                v.push_back(a[i]+a[j]),S.insert(a[i]+a[j]);
        }
        for(int i=0;i<(int)v.size();i++)
        {
            int num = *S.lower_bound(m-v[i]);
            if(num+v[i] < m)
                ans = max(ans,num+v[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

int main()
{
loop:
  int n, m, s = 0, p[1000], o = 4;
  cin >> n >> m;
  if(!n)
    return 0;
  for(int i = 0; i < n; i++)
    cin >> p[i];
  sort(p, p + n, greater<int>());
  for(int i = 0; i < n; i++)
  {
    int a = (m - s) / p[i];
    s += p[i] * min(a, o);
    o -= min(a, o);
  }
  cout << s << endl;
  goto loop;
}

C++: #include<bits/stdc++.h>
using namespace std;
long int n,m,a,ans;
vector <long int> ss1,aa;

int main(){

    while(1){

    cin >> n >> m;
    if(n==0&&m==0)break;
    ans=0;
    ss1.clear();
    aa.clear();

    aa.push_back(0);
    for(int i=0;i<n;i++)cin >> a ,aa.push_back(a);
    sort(aa.begin(),aa.end());

    for(int i=0;i<=n;i++){
        for(int j=i;j<=n;j++){
            if(aa[i]+aa[j]<=m)ss1.push_back(aa[i]+aa[j]);
        }
    }
    sort(ss1.begin(),ss1.end());

    int cnt=ss1.size()-1;
    for(int i=0;i<ss1.size();i++){
        while(ss1[i]+ss1[cnt]>m)cnt--;
        if(ss1[cnt]+ss1[i]<=m)ans=max(ans,ss1[cnt]+ss1[i]);
    }

    cout << ans << endl;

    }

return 0;
}

C++: //JOI 2008??´???3??? ?????????

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>

using namespace std;

#define MAX -210000000
#define MIN 210000000

int a,b;
int d[1000],c[1000*1000+2],hk=0,mcunt,low,high;
int x,pr=0/*,flg[200000000]*/,m,ff=0;

int main(){ 
	
	scanf("%d %d",&a,&b);
	while(a!=0 && b!=0){
		d[0]=0;
		hk=0;
		ff=0;
		pr=0;
		for(int i=1;i<=a;i++) scanf("%d",&d[i]);
		
		for(int i=0;i<=a;i++){
			for(int j=0;j<=a;j++){
				//if(flg[d[i]+d[j]]!=1){
					c[hk]=d[i]+d[j];
					//printf("%lld\n",c[cunt]);
					hk++;
				/*	flg[d[i]+d[j]]=1;
				
				}*/
			}
		}
		//printf("Sort???\n");
		sort(c,c+hk);
		/*printf("\n");
		for(int i=0;i<hk;i++) printf("%d\n",c[i]);
		printf("\n");
		
		printf("hk=%d\n",hk);
		printf("c[low]=%d c[high]=%d\n",c[hk],c[hk-1]);*/
										 
		for(int i=0;i<hk;i++){
			x=b-c[i];
			//printf("x=%d\n",x);
			low=0;
			high=hk-1;
			while(low<=high){
				m=(low+high)/2;
				if(x==c[m]){
					ff=1;
					break;
				}
				if(x>c[m]) low=m+1;
				else if(x<c[m]) high=m-1;
			}
			//printf("\n%d\n",low);
			if(ff==1){
				pr=c[i]+c[m];
				break;
			}
			if(b>c[i]+c[high] && pr<c[i]+c[high]){
				pr=c[i]+c[high];
			//	printf("%d %d\n",c[i],c[high]);
			}
		}
		printf("%d\n",pr);
		scanf("%d %d",&a,&b);
	}
	return 0;
}