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++: #include<cstdio>
#include<vector>
#include<algorithm>
#define REP(i,a,n) for(int i = a;i < n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;
int in(){int x; scanf("%d",&x); return x;}
int main(void){
   int n,m;
   while(scanf("%d%d",&n,&m),n+m){
      vector<int>_point(0);
      vector<int>point(0);
      point.push_back(0);
      _point.push_back(0);
      rep(i,n){
         int x; scanf("%d",&x);
         _point.push_back(x);
         for(int j = _point.size()-1;j >= 0;j--){
            point.push_back(x+_point[j]);
         }
      }

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

      int ma = 0;

      for(int i = 0;point[i] <= m;i++){
         vector<int>::iterator idx = upper_bound(point.begin(),point.end(),m-point[i]);

         idx -= 1;
         int num = *idx;
         ma = max(ma,point[i]+num);
      }
      printf("%d\n",ma);
   }
   return 0;
}

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

vector<long long>F1;
vector<long long>F2;

long long n,m;
long long a[10000];
long long maxn;
long long l,r,M;

int main(){
	while(true){
		maxn=0;
		cin>>n>>m;
		if(n==0 && m==0){break;}
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		F1.clear();
		F2.clear();
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				F1.push_back(a[i]+a[j]);
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				F2.push_back(a[i]+a[j]);
			}
		}
		sort(F2.begin(),F2.end());
		for(int i=0;i<F1.size();i++){
			if(F1[i]<=m){
				l=0;r=F2.size();
				for(int j=0;j<50;j++){
					M=(l+r)/2;
					if(F1[i]+F2[M]>m){
						r=M;
					}
					else if(M==F1.size()-1){
						l=M;
					}
					else if(F1[i]+F2[M+1]<=m){
						l=M;
					}
				}
				maxn=max(maxn,F1[i]+F2[M]);
			}
		}
		cout<<maxn<<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 <iostream>
#include <vector>
#include <algorithm>

constexpr int MAX_N = 1001;

int n, m;
int p[MAX_N];
std::vector<int> sums;

int main() {
	while (1) {
		std::cin >> n >> m;
		if (n + m == 0)
			break;
		sums.clear();
		for (int i = 0; i < n; ++i)
			std::cin >> p[i];
		++n;
		p[n - 1] = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = i; j < n; ++j)
				sums.push_back(p[i] + p[j]);
		}
		std::sort(sums.begin(), sums.end());
		sums.erase(std::unique(sums.begin(), sums.end()), sums.end());
		int res = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				auto it = std::upper_bound(sums.begin(), sums.end(), m - p[i] - p[j]);
				if (it != sums.begin()) {
					--it;
					res = std::max(res, (*it) + p[i] + p[j]);
				}
			}
		}
		std::cout << res << std::endl;
	}
	return 0;
}

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

typedef long long lint;
typedef pair<int, int> P;
#define REP(i, x, n) for(lint i = x ; i < n ; i++)
#define rep(i, n) REP(i, 0, n)
#define repr(i, n) for(lint i = n - 1 ; i >= 0 ; i--)
#define SORT(c) sort((c).begin(), (c).end())

const int IINF = 1e9 + 10;
const lint LLINF = (lint)1e18 + 10;
const lint MOD = (lint)1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

lint n, m;
vector<lint> p;

int main(){
    while(cin >> n >> m, n){
        p.resize(n);
        rep(i, n){
            cin >> p[i];
        }
        p.push_back(0);
        vector<lint> pts;
        rep(i, n+1){
            rep(j, n+1){
                if(p[i] + p[j] <= m){
                    pts.push_back(p[i] + p[j]);
                }
            }
        }
        SORT(pts);
        lint ans = 0;
        rep(i, pts.size()){
            int k = (upper_bound(pts.begin(), pts.end(), m - pts[i]) - pts.begin());
            k--;
            ans = max(ans, pts[i] + pts[k]);
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
using namespace std;

int main(){
  while(true){
    ll n,m;
    cin>>n>>m;
    if(n==0&&m==0){
      break;
    }
    vector<ll> p(n);
    rep(i,n)cin>>p[i];
    vector<ll> ps;
    rep(i,n){
      ps.push_back(p[i]);
      for(ll j=i;j<n;j++){
        ps.push_back(p[i]+p[j]);
      }
    }
    sort(ps.begin(),ps.end());
    ll maxi=0;
    for(ll i=0;i<ps.size();i++){
      auto itr=upper_bound(ps.begin(),ps.end(),m-ps[i]);
      if(itr!=ps.begin()){
        itr--;
        maxi=max(maxi,ps[i]+*itr);
      }
    }
    cout<<maxi<<endl;
  }
}

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


int max_sum(int a[],int as,int b[],int bs,int m){
	int j=bs-1;
	int res=0;
	for(int i=0;i<as;i++){
		while(0<=j && m<a[i]+b[j])j--;
		if(j==-1)break;
		res=max(res,a[i]+b[j]);
	}
	return res;
}

int p[1000],p2[1000*1000];
int main(){
	int n,m;
	while(cin>>n>>m,n||m){
		int ans=0;
		for(int i=0;i<n;i++){
			cin>>p[i];
			if(p[i]<=m)ans=max(ans,p[i]);
		}
		sort(p,p+n);
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				int a=p[i]+p[j];
				p2[i*n+j]=a;
				if(a<=m)ans=max(ans,a);
			}
		}
		sort(p2,p2+n*n);
		ans=max(ans,max_sum(p,n,p2,n*n,m));
		ans=max(ans,max_sum(p2,n*n,p2,n*n,m));
		cout<<ans<<endl;
	}
	return 0;
}