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 <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

int N, M, arr[1000];

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    while (true) {
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        arr[0] = 0;
        for (int j = 1; j <= N; ++j) {
            cin >> arr[j];
        }
        ++N;
        int arr2[1001*1001];
        for (int j = 0; j < N; ++j) for (int k = 0; k < N; ++k) {
            arr2[N*j+k] = arr[j] + arr[k];
        }
        sort(arr2, arr2+N*N);

        int ans = -1, l = 0, r = 0;
        while (l < N*N-1 || r < N*N-1) {
            int cand = arr2[l] + arr2[r];
            if (cand <= M) {
                ans = max(ans, cand);
            }
            if (l == r || (r < N*N-1 && arr2[l] + arr2[r+1] <= M)) {
                ++r;
            } else {
                ++l;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

typedef long long ll;

ll dp[5];
ll dat[1001];

int main() {
  int n, m;
  
  while(cin >> n >> m && (n|m)) {
    for(int i=0; i<n; i++) cin >> dat[i];
    
    fill(dp, dp+5, 0);
    
    ll ans = 0;
    
    for(int i=0; i<4; i++) {
      for(int j=0; j<n; j++) {
	if(dp[i]+dat[j] <= m) {
	  dp[i+1] = max(dp[i]+dat[j], ans);
	  ans = max(dp[i+1], ans);
	}
      }
    }
    
    cout << ans << endl;
  }
  
  return 0;
}

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

using namespace std;

int m,n;
int a[1002];
int aa[1002001];

int ans,Max;

int main(){
	
	while (1) {
		scanf("%d%d",&n,&m);
		ans=0;
		if (n==0&&m==0) {
			break;
		}
		for (int i=0; i<n; i++) {
			scanf("%d",&a[i]);
		}
		a[n++]=0;
		int size=0;
		for(int c=0;c<n;c++){
			for (int d=0; d<n; d++) {
				aa[size++]=a[c]+a[d];
			}
		}
		sort(aa,aa+size);
		
		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) {
				if (m-a[i]-a[i]>=0) {
					int l=0,r=size;
					Max=0;
					while (l<=r) {
						if (aa[size-1]+a[i]+a[j]<ans) {
							break;
						}
						int k=(l+r)/2;
						if (aa[k]<(m-a[i]-a[j])&&aa[k]>Max) {
							Max=aa[k];
						}
						if(aa[k]<(m-a[i]-a[j]))l=k+1;
						else r=k-1;
					}
					if (Max+a[i]+a[j]>ans) {
						ans=Max+a[i]+a[j];
					}
				}
			}
		}
		
		printf("%d\n",ans);
	}
	
	
}

C++: 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	int ans[4];
	int temp;
	double energy,energy_min,energy_now;
	int M, N;
	int* p;
	int i, j;
	double T=100;
	int ret;
	while (1) {
		scanf("%d%d", &N, &M);
		p = (int*)malloc((N+1) * sizeof(int));
		for (i = 0; i < N; i++) {
			scanf("%d", &p[i]);
		}
		p[N] = 0;
		if (N == M && N == 0) {
			break;
		}
		energy_min = pow(10, 300);
		for (j = 0; j < 4; j++) {
			ans[j] = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
		}
		
		for (T = 1000788; T > 1000; T--) {
			for (j = 0; j < 4; j++) {
				temp = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
				for (j = 0; j < 4; j++) {
					ret = ans[0] + ans[1] + ans[2] + ans[3];
					ret += temp - ans[j];
					energy = M - ret;
					if (M < ret)energy += 100000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*1000)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}

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

int n, m;
vector<int> p;
int t;
vector<int> q;
vector<int> o;
int ans;

int b(int x){
	int l = 0;
	int r = q.size();
	while(r - l > 1){
		int i = (l + r) / 2;
//		printf("l=%d, r=%d, i=%d, q[i]=%d, x=%d\n", l, r, i, q[i], x);
		if(q[i] < x){
			l = i;
		}else{
			r = i;
		}
	}
//	printf("%d\n", q[l]);
	return q[l];
}

int main(){
	while(1){
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;
		p = o;
		p.push_back(0);
		for(int i=0; i<n; i++){
			scanf("%d", &t);
			p.push_back(t);
		}
		q = o;

		for(int i=0; i<p.size(); i++){
			for(int j=0; j<p.size(); j++){
				q.push_back(p[i] + p[j]);
			}
		}
		sort(q.begin(), q.end());
/*
		for(int i=0; i<q.size(); i++)
			printf("%d\n", q[i]);
*/
		for(int i=0; i<q.size(); i++){
			int a = q[i];
			if(m - a < 0) continue;
			ans = max(ans, b(m - a) + a);
//			printf("%d\n", b(m - a));
		}

		printf("%d\n", ans);
	}

	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 = 1000000000;
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);
}

ll n,m;
vector<vector<ll>> dp(5,vector<ll>(1001,0));
vary(ll,s,1001,0);
ll dfs(ll cnt, ll sum){
  if(dp[cnt][sum] != inf || cnt == 4) return sum;
  dp[cnt][sum] = 1;
  ll res = sum;
  rep(i,n){
    if(sum + s[i] <= m){
      res = max(res,dfs(cnt+1,sum + s[i]));
    }
  }
  return res;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(cin >> n >> m && n && m){
    rep(i,n){
      cin >> s[i];
    }
    sort(s.begin(),s.begin()+n);
    rep(i,5){
      fill(all(dp[i]),inf);
    }
    cout << dfs(0,0) << endl;
  }
  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 cnt=0;
  sort(t,t+n);
  for(int i=n-1;i>=0;){
    if(cnt>3){
      break;
    }
    if(ans+t[i]>=m){
      i--;
      continue;
    }
    cnt++;
    ans+=t[i];
    //cout<<cnt<<endl;
    //cout<<"ans "<<ans<<endl;
  }
  cout<<ans<<endl;
  return 0;
}