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

int main() {
  while(true) {
    int N, M; cin >> N >> M;
    if(N == 0 && M == 0) break;
    vector<int> A(N);
    for(int i = 0; i < N; i++) {
      cin >> A[i];
    }

    A.push_back(0);
    vector<int> B(N*N);
    for(int a : A) {
      for(int b : A) {
        B.push_back(a+b);
      }
    }
    sort(B.begin(), B.end());
    B.erase(unique(B.begin(), B.end()), B.end());

    int ans = 0;
    for(int i = 0; i < (int)B.size(); i++) {
      if(B[i] > M) continue;
      if(B[i] == M) { ans = M; continue; }
      ans = max(ans, B[i] + *(lower_bound(B.begin(), B.end(), M - B[i]) - 1));
    }
    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>

using namespace std;

int n,m;
int p[1001];

int solve(){
  int pp[(n+1)*(n+1)];
  
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      pp[n*i+j] = p[i] + p[j];
      // cout<<"pp:"<<pp[n*i+j]<<endl;
    }
  }
  /*
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      cout<<"ij:"<<n*i+j<<"\t"<<pp[i*n+j]<<endl;
    }
  }
  */
  int ans = 0;
  
  sort(pp,pp+n*n+n+1);
  /*
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      cout<<"ij:"<<n*i+j<<"\t"<<pp[i*n+j]<<endl;
    }
  }
  */
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      int* it = upper_bound(pp,pp+n*n+n+1,m-p[i]-p[j]);
      --it;
      // cout<<"it"<<*it<<endl;
      // cout<<"ij:"<<p[i]<<"\t"<<p[j]<<endl;
      // cout<<"ans:"<<p[i]+p[j]+(*it)<<endl;
      if(p[i] + p[j] > m)continue;
      ans = max(ans,p[i]+p[j]+(*it));
    }
  }
  return ans;
}

int main(){
  while(cin>>n>>m){
    if(n==0&&m==0)break;
    p[0] = 0;
    for(int i = 1;i<=n;i++){
      cin>>p[i];
    }
    
    cout<<solve()<<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;
}

vector<vector<ll>> dp;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n >> m && n && m){
    vary(ll,s,n,0);
    dp = vector<vector<ll>>(n+2,vector<ll>(m+2,inf));
    rep(i,n){
      cin >> s[i];
    }
    sort(all(s));
    rep(i,m+1){
      dp[0][i] = 0;
    }
    rep(i,n+1){
      dp[i][0] = 0;
    }
    REP(i,0,n){
      for(ll j = 0; j < m+1 + s[i]; j += s[i]){
        if(j - s[i] >= 0){
          dp[i+1][j] = min(dp[i+1][j],dp[i][j-s[i]]+1);
        }
      }
      if(i > 0){
        rep(j,m+1){
          dp[i+1][j] = min(dp[i][j],dp[i+1][j]);
        }
      }
    }
    ll ans = 0;
    for(ll j = n; j >= 0; --j){
      for(ll i = m; i >= 0; --i){
        if(dp[j][i] != inf && dp[j][i] != 0 && dp[j][i] <= m){
          ans = i;
          j = -1;
          break;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define MAX_N 1010
#define pb push_back

int main(){
  int N, M;
  
  while(cin >> N >> M){
    if(N == 0 && M == 0) break;
    vector<int> P(N), P2;

    for(int i = 0 ; i < N ; i++){
      cin >> P[i];
      P2.pb(P[i]);
    }

    P2.pb(0);
    for(int i = 0 ; i < N ; i++){
      for(int j = 0 ; j < N ; j++){
	P2.pb(P[i]+P[j]);
      }
    }

    sort(P2.begin(), P2.end());
    
    int ans = 0;
    for(int i = 0 ; i < P2.size() ; i++){
      if(M >= P2[i]) ans = max(ans, P2[i]+upper_bound(P2.begin(), P2.end(), M-P2[i])[-1]);
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main()
{
while(true) {
int n, m;
vector<int> x, xxx;
scanf("%d%d", &n, &m);
if(n == 0)
break;
x.resize(n);
for(int i = 0; i < n; ++i)
scanf("%d", &x[i]);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j)
xxx.push_back(x[i] + x[j]);
}
xxx.push_back(0);
sort(xxx.begin(), xxx.end());
int ans = 0;
for(int i = 0; i < xxx.size(); ++i) {
int r = m - xxx[i];
if(r < 0)
continue;
int idx = upper_bound(xxx.begin(), xxx.end(), r) - xxx.begin() - 1;
if(idx < 0)
continue;
ans = max(ans, xxx[i] + xxx[idx]);
}
printf("%d\n", ans);
}
return 0;
}

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

using namespace std;

int main() {
	long long int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector<long long int> data;
		long long int b;
		data.push_back(0);
		for (long long int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				bool hantei = true;
				for (long long int j = 0; j < data.size(); j++) {
					if (data[j] == b) {
						hantei = false;
						break;
					}
				}
				data.push_back(b);
			}
		}
		sort(data.begin(), data.end());
		vector<long long int> data2;
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		for (long long int i = 0; i < data2.size(); i++) {
			long long int a = data2[i];
			data.push_back(a);
			for (long long int j = i+1; j < data2.size(); j++) {
				if (data2[j] == a) {
					i++;
				} else {
					break;
				}
			}
		}
		data2.clear();
		for (long long int i = 0; i < data.size(); i++) {
			data2.push_back(data[i]);
			for (long long int j = i; j < data.size(); j++) {
				if (data[i]+data[j] <= m) {
					data2.push_back(data[i]+data[j]);
				} else {
					break;
				}
			}
		}
		sort(data2.begin(), data2.end());
		data.clear();
		long long int sz = data2.size();
		cout << data2[sz-1] << endl;
	}
	return 0;
}