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*len(arr)+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++: //Solution for AOJ 0529: Darts
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 1000, MAX_M = 200000000;
int n, m, ans, roop, s, mid;
int p[MAX_N + 1], p2[MAX_N*MAX_N];
void search(int end, int start){
	if ((end + start) % 2)
		mid = (end + start) / 2 + 1;
	else
		mid = (end + start) / 2;
	if (p2[mid] == s)
		return;
	if (p2[mid] < s){
		if (end - mid == 1){
			if (p2[end]<s)
				mid++;
			return;
		}
		search(end, mid);
	}
	else{
		if (mid - start == 1){
			mid--;
			return;
		}
		search(mid, start);
	}
}
int main(){
	while (cin >> n >> m){
		ans = 0;
		for (int i = 1; i <= n; i++){
			cin >> p[i];
		}
		roop = 0;
		for (int i = 0; i <= n; i++){
			for (int j = i; j <= n; j++){
				if (p[i] + p[j] <= m){
					p2[roop] = p[i] + p[j];
					roop++;
				}
			}
		}
		sort(p2, p2 + roop);
		for (int i = 0; i < roop; i++){
			s = m - p2[i];
			search(roop - 1, 0);
			ans = max(ans, p2[i] + p2[mid]);
			if (ans == m)
				break;
		}
		cout << ans << endl;
		return 0;
	}
}

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 = 1000000; T > 100; 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 += 100000000;

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

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

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

using namespace std;

int main()
{
	for(int n,m;cin>>n>>m,n;){
		int p[1001];	p[0]=0;
		for(int i=1;i<=n;i++)	cin>>p[i];

		int nn=0;
		static int xp[1001*1001];
		for(int i=0;i<=n;i++)for(int j=i;j<=n;j++)	xp[nn++]=p[i]+p[j];
		n=nn;
		sort(xp,xp+n);

		int maxscore=0;
		for(int i=0;i<n;i++){
			if(m-xp[i]>=0)
				maxscore=max(maxscore,xp[i]+upper_bound(xp,xp+n,m-xp[i])[-1]);
		}
		cout<<maxscore<<endl;
	}

	return 0;
}

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

using namespace std;

int m,n;
int a[1002];

int ans,Max;

void bin_search(int *aa,int x){
	int l=0,r=n*n;
	Max=0;
	
	while (l<=r) {
		int i=(l+r)/2;
		if (aa[i]<x&&aa[i]>Max) {
			Max=aa[i];
		}
		if(aa[i]<x)l=i+1;
		else r=i-1;
	}
}

void solve(){
	int aa[1002*1002]={0};
	for(int c=0;c<n;c++){
		for (int d=0; d<n; d++) {
			aa[c*n+d]=a[c]+a[d];
		}
	}
	sort(aa,aa+n*n);
	
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			if (m-a[i]-a[i]>=0) {
				bin_search(aa,m-a[i]-a[j]);
				if (Max+a[i]+a[j]>ans) {
					ans=Max+a[i]+a[j];
				}
			}
		}
	}
	
}

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;
		n++;
		solve();
		printf("%d\n",ans);
	}
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define MP make_pair

typedef vector<int> vi;

int main() {
	while (true) {
		int n, m, score[1001];
		vi v;
		cin >> n >> m;
		if (n == 0 && m == 0) break;
		REP(i, n) scanf("%d", &score[i]);
		score[n++] = 0;

		REP(i, n) FOR(j, i, n) v.push_back(score[i] + score[j]);
		sort(v.begin(), v.end(), greater<int>());

		int ans = 0;
		REP(i, n) FOR(j, i, n) {
			vi::iterator it = lower_bound(v.begin(), v.end(), m - score[i] - score[j], greater<int>());
			if (it == v.end()) continue;
			ans = max(ans, *it + score[i] + score[j]);
		}

		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define MP make_pair

typedef vector<int> vi;

int main() {
	while (true) {
		int n, m, score[1001];
		vi v;
		cin >> n >> m;
		if (n == 0 && m == 0) break;
		REP(i, n) scanf("%d", &score[i]);
		score[n++] = 0;

		REP(i, n) FOR(j, i, n) v.push_back(score[i] + score[j]);
		sort(v.begin(), v.end());
		unique(v.begin(), v.end());

		int ans = 0;
		REP(i, v.size()) {
			vi::iterator it = upper_bound(v.begin(), v.end(), m - v[i]);
			if (it != v.begin())
				ans = max(ans, *(it - 1) + v[i]);
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
int t[1001*1001];
using namespace std;
int main(void)
{	
	int  n,p[1001],s,m,i,j,k,x,high,low,mid,max,suu,flg,mx;
	while(1) {
		scanf("%d %d",&n,&m);
		if(n==0 && m==0) break;
		p[0]=0;
		for(i=1;i<=n;i++) scanf("%d",&p[i]);
		x=0;
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++) {
				t[x]=p[i]+p[j];
				x++;
			}
		}
		sort(t,t+x);
		max=-1;
		mx=-1;
		for(i=0;i<x;i++) {
			flg=0;
			low=0;
			high=x;
			while(low<=high) {
				mid=(low+high)/2;
				if(t[mid]==m-t[i]) {
					flg=1;
					break;
				}
				if(t[mid]<m-t[i]) low=mid+1;
				else if(t[mid]>m-t[i]) high=mid-1;
			}
			if(flg==1) break; 
			else {
				if(t[high]+t[i]>m){}
				else max=t[high]+t[i];
			}
		}
		if(flg==1) printf("%d\n",m);
		else printf("%d\n",max);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long n;
	unsigned long long m;
	while(cin>>n>>m,n!=0||m!=0){
		int k=0;
	unsigned long long  s=0;
		vector<unsigned long long> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0&&k!=4){
			if(s+y[n-1]<m){
				s=s+y[n-1];
				k++;
			}
			else
				n--;
			
		}
		cout<<s<<endl;
	}
	
}

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

#define ef(e, a, b) ((e) ? (a) : (b))

using namespace std;

int main ( void )
{
	int n, m;
	int p[4][1000];

	while ( cin >> n >> m, (n | m) )
	{
		int ans = 0;
		
		for (int i = 0; i < n; ++i)
		{
			cin >> p[0][i];
			
			ans = max(ans, p[0][i]);
			
			for (int j = 1; j < 4; ++j) { p[j][i] = 0; }
		}
		
		for (int i = 1; i < 4; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				for (int k = 0; k < n; ++k)
				{
					int t = p[i-1][j] + p[0][k];
					p[i][j] = max(p[i][j], ef(t > m, 0, t));
					ans = max(ans, p[i][j]);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}