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<cstdio>
#include<algorithm>
#include<functional>

long long kazu[1002001];//1001^2
long long n,m,Max,data[1001];

int main(){
	while(true){
		int s=0;
		std::cin>>n>>m;
		if(n==0&&m==0)break;
		data[0]=0;
		for(int i=0;i<n;i++){
			std::cin>>data[i+1];
		}
		for(int c=0;c<n+1;c++){
			for(int d=0;d<n+1;d++){
				kazu[c*n+d]=data[c]+data[d];
			}
		}
		std::sort(kazu,kazu+(n+1)*(n+1));
		for(int a=0;a<n+1;a++){
			for(int b=0;b<n+1;b++){
				while(s<m&&Max<m-s){
					if(std::binary_search(kazu,kazu+((n+1)*(n+1)),m-s-data[a]-data[b])){
						Max=std::max(m-s,Max);
						break;
					}
					s++;
				}
				s=0;
			}
		}
		std::cout<<Max<<std::endl;
		Max=0;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  int t[n];
  for(int i=0;i<n;i++){
    cin>>t[i];
  }
  int ans=0;
  int max=0;
  sort(t,t+n);
  for(int i=n-1;i>=0;){
    if(ans+t[i]>=m){
      i--;
      continue;
    }
    ans+=t[i];
    //cout<<"ans "<<ans<<endl;
  }
  cout<<ans<<endl;
  return 0;
}

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

using namespace std;

int main() {
  
  int n, m;
  
  while(cin >> n >> m && (n|m)) {
    int data[1002];
    int Q[1000005];
    
    n++;
    data[0] = 0;
    for(int i=1; i<n; i++) {
      cin >> data[i];
    }
    
    int T = 0;
    
    for(int i=0; i<n; i++) {
      Q[T++] = data[i];
    }
    
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
	Q[T++] = data[i] + data[j];
      }
    }
    
    sort(Q, Q+T);

    // for(int i=0; i<T; i++) cout << Q[i] << endl;
    
    int ans = 0;
    
    for(int i=0; i<T; i++) {
      int M = m - Q[i];
      if(M < 0) break;
      
      int lb = 0, ub = T;
      
      while(ub - lb > 1) {
	int mid = (lb + ub) / 2;
	
	if(Q[mid] <= M) {
	  lb = mid;
	} else {
	  ub = mid;
	}
      }
      ans = max(ans, Q[i]+Q[lb]);
    }
    
    cout << ans << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <cstring>
#include <cstdlib>

using namespace std;

int array_sum( int ary[4][1001], int idx, int size )
{
	int sum = 0;
	for(int i=0; i < size; ++i) sum += ary[i][idx];
	return sum;
}
void map_sort( int ary[4][1001], int idx, int size )
{
	for(int i=0; i < size; ++i)
		for(int j=i+1; j < size; ++j) {
			if(ary[i][idx] < ary[j][idx])
				swap(ary[i][idx], ary[j][idx]);
		}
}

int main( void )
{
	int n=0, m, input[1002], map[4][1001];
	while(cin >> n >> m, n | m) {
		for(int i=0; i < n; ++i) {
			cin >> input[i];
			for(int j=0; j < 4; ++j) map[j][i] = 0;
		}
		for(int i=0; i < 4; ++i) map[i][n] = 0;
		sort(input, input+n, greater<int>());
		int sum = 0, count = 0;
		while(sum < m && count < 4) {
			sum += input[0];
			++count;
		}
		int mmm = m, cc = 0;
		for(int i=0; i < n; ++i) {
			while(input[i] <= mmm && cc < 4) {
				mmm -= input[i];
				++cc;
			}
			if(cc >= 4) break;
		}
		sort(input+1, input+n);
		for(int i=0; i < count; ++i) map[i][0] = input[0];
		int sub = sum - m;
		if(!sub) { cout << m << endl; continue; }
		else if(sub < 0) { cout << sum << endl; continue; }
		int Min = min(m, mmm);
		//cout << Min << endl;
		input[n] = 0;
		for(int i=0; i < n; ++i) {
			//cout << input[i] << endl;
			sum = array_sum(map, i, count);
			if(sum == m) { Min = 0; break; }
			int next = i + 1, ii = i + 1;
			for(int j=0; j < count; ++j) {
				int tsum = sum - map[j][i] + input[ii];
				int tsub = tsum - m;
				if(tsub > 0) {
					if(sum > tsum) {
						sum = tsum;
						map[j][next] = input[ii];
					}
					else
						map[j][next] = map[j][i];
				}
				else if(!tsub) {
					Min = 0; break;
				}
				else { // minus
					Min = min(Min, -tsub);
					int tcount = 4 - count;
					int tmul = tcount * input[ii];
					if(tmul >= -tsub) {
						for(int k=0; k <= j; ++k) map[k][next] = input[ii];
						for(int k=j+1; k < count; ++k) map[k][next] = map[k][i];
						for(int k=count; k < 4; ++k) {
							if(sum < array_sum(map, next, k) + input[ii]) break;
							map[k][next] = input[ii];
							++count;
						}
						//input[ii] = 0;
						//i -= 2;
						break;
					}
					else {
						map[j][next] = map[j][i];
					}
				}
			}
			if(!Min) break;
			map_sort(map, next, 4);
			//cout << "--------------------------------------------" << endl;
			//for(int y=0; y < 4; ++y, cout << endl)
			//	for(int x=0; x < n+1; ++x) cout << map[y][x] << ' ';
			//cout << "--------------------------------------------" << endl;
		}
		if(!Min) { cout << m << endl; continue; }
		for(int i=0; i < count; ++i) map[i][0] = input[0];
		for(int i=0; i < n; ++i) {
			sum = array_sum(map, i, count);
			if(sum == m) { Min = 0; break; }
			int next = i + 1, ii = i + 1;
			for(int j=0; j < count; ++j) {
				int tsum = sum - map[j][i] + input[ii];
				int tsub = tsum - m;
				if(tsub > 0) {
					if(sum > tsum) {
						sum = tsum;
						map[j][next] = input[ii];
					}
					else
						map[j][next] = map[j][i];
				}
				else if(!tsub) {
					Min = 0; break;
				}
				else { // minus
					Min = min(Min, -tsub);
					map[j][next] = map[j][i];
				}
			}
			if(!Min) break;
			map_sort(map, next, 4);
		}
		cout << m - Min << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
int main(){
while(1){
int i,n,j,m,s=0,l,k;
static int p[1001]={0};
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++)scanf("%d",&p[i]);
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
for(l=0;l<=n;l++){
for(k=0;k<=n;k++){
if(p[i]+p[j]+p[l]+p[k]<=m&&p[i]+p[k]+p[j]+p[l]>s)s=p[i]+p[j]+p[l]+p[k];
}
}
}
}
printf("%d\n",s);
}
return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define mp(x,y) make_pair(x,y)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int,int>
#define INF 1<<29
#define MAX 1010
using namespace std;

int p[MAX];

void solve(){
 int n,m,ind;
 for(;;){
  cin>>n>>m;
  if(!n) break;
  int ma=0;
  vector<int> v;
  FOR(i,n){
   cin>>p[i];
   v.pb(p[i]);
  }
  FOR(i,n){
   FORR(j,i,n){
    v.pb(a[i]+a[j]);
   }
  }
  sort(v.begin(),v.end());
  erase(unique(v.begin(),v.end()),v.end());
  ind=upper_bound(v.begin(),v.end(),m)-1;
  if(ind>=0) ma=v[ind];
  FOR(i,v.size()){
   ind=upper_bound(v.begin(),v.end(),m-v[i])-1;
   if(ind>=0) ma=max(ma,v[i]+v[ind]);
  }
  cout<<ma<<endl;
 }
}

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