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<stdio.h>
#include<algorithm>

using namespace std;
int main(){
	int n,m,ans;
	int dp[1001][4];
	for(;;){
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)break;
		for(int i=0;i<1001;i++)for(int j=0;j<4;j++)dp[i][j]=0;
		for(int i=0;i<n;i++)scanf("%d",&dp[i][0]);
		//sort(p,p+n);
		//reverse(p,p+n);
		ans = 0;
		for(int i=0;i<n;i++){
			for(int j=1;j<4;j++){
				for(int k=0;k<=i;k++)
					for(int l=0;l<j;l++)
						if(dp[k][l]+dp[i][0] <= m)
							dp[i][j]=max(dp[i][j],dp[k][l]+dp[i][0]);
				ans = max(ans,dp[i][j]);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 100000000

int p[MAX], dp[MAX];
int main(){
	int n, m, ans;
	while(cin >> n >> m, n){
		fill(dp, dp+MAX, 0);
		for(int i = 0;i < n;i++)cin >> p[i];
		ans = 0;
		
		for(int i = 0;i < n;i++){
			dp[0] = 5;
			for(int j = p[i];j <= m;j++){
				dp[j] = max(dp[j], dp[j-p[i]]-1);
				//if(dp[j] < dp[j-p[i]-1])dp[j] = dp[j-p[i]]-1;
				if(dp[j])ans = max(ans, j);
				//if(dp[j] && ans < j)ans = j;
			}
		}
		//for(int i = 1;i <= 50;i++)cout << dp[i] << endl;
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	while(true){
		int n,m;
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)return 0;
		int p[1001];
		for(int i=0;i<n;i++)scanf("%d",&p[i]);
		p[n]=0;
		n++;

		int ans=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				for(int k=0;k<n;k++){
					for(int l=0;l<n;l++){
						if(p[i]+p[j]+p[k]+p[l]<=m)ans=max(ans,p[i]+p[j]+p[k]+p[l]);
					}
				}
			}
		}

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

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;

int n, m;
int nearabs(int x, int y) {
	if (x > m)return y;
	if (y > m)return x;

	if (m - x > m - y)return y;
	else return y;
}
signed main() {
	while (cin >> n >> m, n || m) {
		vector<vector<int>>dp(n + 1, vector<int>(n + 1, 0));
		vector<int>v(n + 1); rep(i, 1, n + 1)cin >> v[i];
		
		rep(i, 1, n + 1) {
			rep(j, 1, n + 1) {
				if (dp[i][j - 1] + v[i] < m)dp[i][j] = max(dp[i - 1][j], dp[i][j - 1] + v[i]);
				else {
					rep(k, 1, i + 1) {
						if (dp[i][j - 1] + v[k] < m)dp[i][j] = max(dp[i][j], dp[i][j - 1] + v[k]);
					}
				}
			}
		}
		cout << dp[n][n] << endl;

	}
	return 0;
}

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

int n, m, point1[1001], sum, ans;
vector<int> point2;

int solve(int start, int end, int border){
    int mid;
    while(1){
        if((end + start) % 2){
            mid = (end + start) / 2 + 1;
        } else{
            mid = (end + start) / 2;
        }

        if(point2[mid] > border){
            end = mid;
        } else{
            start = mid;
        }

        if(end - start == 1){
            if(point2[end] < border){
                return point2[end];
            } else{
                return point2[start];
            }
        }
    }
}

int main(){
    while(cin >> n >> m && n && m){
        for(int i = 0; i < n; i++){
            cin >> point1[i];
        }
        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < n + 1; j++){
                point2.push_back(point1[i] + point1[j]);
            }
        }

        sort(point1, point1 + n + 1);
        sort(point2.begin(), point2.end());

        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < n + 1; j++){
                sum = point1[i] + point1[j];
                if(sum > m){
                    continue;
                }
                ans = max(ans, sum + solve(0, point2.size() - 1, m - sum));
            }
        }

        cout << ans << endl;

        memset(point1, 0, sizeof(point1));
        point2.clear();
        ans = 0;
    }

    return 0;
}

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

using namespace std;

int main(void){
    int c[1001001],p[1010],n,m,cnt,i,j,tmp,ans,pnt;
    int *point;
    cin >> n >> m;
    while(n!=0 && m!=0){
        cnt = 0;
        ans = 0;
        p[0]=0;
        for(i=1; i<=n; i++){
            cin >> p[i];
        }
        m++;
        for(i=0; i<=n; i++){
            for(j=i; j<=n; j++){
                if(p[i]+p[j]<m){
                    c[cnt] = p[i]+p[j];
                    cnt++;
                }
            }
        }
        sort(c,c+cnt);
        for(i=0; i<cnt; i++){
            point = lower_bound(c,c+cnt,m-c[i]);
            pnt = (int)(point-c);
            if(pnt>0){
                pnt--;
                tmp = c[i]+c[pnt];
                ans = max(ans,tmp);
            }
        }
        cout << ans <<endl;
        cin >> n >> m;
    }
    return 0;
}

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector<long long int> data(n);
		for (int i = 0; i < n; i++) {
			cin >> data[i];
		}
		sort(data.begin(), data.end());
		long long int a = m;
		for (int i = 0; i < 4; i++) {
			vector<long long int>::iterator ite = upper_bound(data.begin(), data.end(), a);
			if (ite != data.begin()) {
				a -= *(ite-1);
			}
		}
		cout << m-a << endl;
	}
	return 0;
}

C++: 4 50
3
14
15
9
3 21
16
11
2