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<iostream>
#include<algorithm>
int main(){
while(1){
int i,n,j,m,s=0,o=0;
static int p[1001]={0},v[1001*1001];
scanf("%d%d",&n,&m);
if(n==0&&m==0)return 0;
for(i=1;i<=n;i++){
scanf("%d",&p[i]);
for(j=i;j>=0;j--){
v[o]=p[i]+p[j];
o++;
}
}
for(i=0;i<o;i++){
for(j=0;j<o;j++){
if(v[i]+v[j]<=m&&v[i]+v[j]>s)s=v[i]+v[j];
}
}
printf("%d\n",s);
}
return 0;
}

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

int main()
{
  int N, M;
  while (cin >> N >> M && N != 0) {
    vector<int> ps(N);
    for (int i = 0; i < N; i++) {
      cin >> ps[i];
    }

    set<int> s;
    s.insert(0);
    for (int i = 0; i < N; i++) {
      s.insert(ps[i]);
      for (int j = i; j < N; j++) {
        if (ps[i] + ps[j] <= M) {
          s.insert(ps[i] + ps[j]);
        }
      }
    }

    const vector<int> v(s.begin(), s.end());
    int ans = 0;
    for (vector<int>::const_iterator it(v.begin()); it != v.end(); ++it) {
      vector<int>::const_iterator jt = lower_bound(v.begin(), v.end(), M - *it);
      if (jt != v.begin()) {
        --jt;
        ans = max(ans, *it + *jt);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
int n,m;
int num[1001];
long long sum[1000005];
int main(){
	while(1){
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
	}
	int ro=0;
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			sum[ro]=num[i]+num[j];
			ro++;
		}
	}
	long long ans=0;
	sort(sum,sum+ro);
	for(int i=0;i<ro;i++){
		if(sum[i]>m){ break;}
		int rp=lower_bound(sum,sum+ro,m-sum[i])-sum;
		if(!rp){
			ans=max(ans,sum[i]);
		}else{
			rp--;
			ans=max(ans,sum[i]+sum[rp]);
		}
	}
	printf("%d\n",sum);
}
	return 0;
}

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

int dp[100000000];

int main(){
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;

int p[1001];
int h[1000*1000+1];

int main(){
	int n,m,ans;
	while(cin>>n>>m){
		if(n==0&&m==0) break;
		p[0]=0;
		ans=0;
		for(int i=1; i<=n; ++i){
			cin >> p[i];
		}
		for(int i=0; i<=n; ++i){	
			for(int j=0; j<=n; ++j){
				h[i*n+j]=p[i]+p[j];
			}
		}
		sort(h,h+n*n+1);
		/*
		for(int i=0; i<=n*n; ++i){
			cout << h[i] << "\n";
		}
		*/
		for(int i=0; i<=n; ++i){	
			for(int j=0; j<=n; ++j){	
				int tmp=p[i]+p[j];
				int l=0,r=n*n;
				int cnt=0;
				while(1){
					int mid=(r+l)/2;
					if(cnt>=30){
						if(h[mid]+tmp<=m) ans=max(ans,h[mid]+tmp);
						break;
					}
					if(h[mid]+tmp>m){
						r=mid;
					} else if(h[mid]+tmp<m){
						l=mid;
					} else if(h[mid]+tmp==m){
						ans=m;
						break;
					}
					++cnt;
				}
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
int n,m;
int a[1000];
int b[1000000];
int i,j;
int ans;

int max(int a,int b){
	if(a>=b)return a;
	else return b;
}

int comp(const void *a,const void *b){
	int p=*(int*)a;
	int q=*(int*)b;
	return p-q;
}

int search(int num){
	int r=0;
	int l=n*n-1;
	int mid;
	for(int k=0;k<25;k++){
		mid=(int)(r+l)/2;
		if(num-b[mid+1]>=0&&num-b[mid]>num-b[mid+1])mid=mid+1;
		if(b[mid]>num)l=mid;
		else r=mid;
	}
	return mid;
}

int main(){
	while(1){
		scanf("%d %d",&n,&m);
		if(n==0&&m==0)return 0;
		ans=0;
		for(i=0;i<n;i++)scanf("%d",&a[i]);
		for(i=0;i<n;i++)for(j=0;j<n;j++)b[i*n+j]=a[i]+a[j];
		qsort(b,n*n,sizeof(int),comp);
		for(i=0;i<n;i++)for(j=0;j<n;j++){
			if(b[i*n+j]<m){
				int p=b[search(m-b[i*n+j])];
				if(p<=m)ans=max(ans,p+b[i*n+j]);
			}
		}
		printf("%d\n",ans);
	}
}

C++: #include<stdio.h>
#include<iostream>
int main(){
	while(1){
		int i,j,n,m,s=0,o;
		static int p[1001],v[1001*1001]={0};
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)return 0;
		o=n+1;
		for(i=1;i<=n;i++){
			scanf("%d",&p[i]);
			v[i]=p[i];
		}
		for(i=0;i<=n;i++){
			for(j=i;j<=n;j++){
				v[o]=p[i]+p[j];
				o++;
			}
		}
		for(i=0;i<o;i++){
			for(j=i;j<o;j++){
				if(v[i]+v[j]>s&&v[i]+v[j]<=m)s=v[i]+v[j];
			}
		}
		printf("%d\n",s);
	}
	return 0;
}

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

using namespace std;

int main(){
    int n,m;
    while(cin >> n >> m){
        if((n|m) == 0) break;

        int p[n+1];
        for(int i=0;i<n;i++){
            cin >> p[i];
        }
        p[n] = 0;
        vector<int> d;
        for(int i=0;i<n+1;i++){
            for(int j=i;j<n+1;j++){
                d.push_back(p[i]+p[j]);
            }
        }
        sort(d.begin(),d.end());

        int ret=0;
        for(int i=0;i<d.size();i++){
            if(d[i] > m) continue;
            ret = max(ret, d[i] + *(upper_bound(d.begin(), d.end(), m-d[i])-1));
        }
        cout << ret << endl;
    }
}

C++: #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
int num[1001];
int sum[1001*501+1];
int main(void){
  scanf("%d%d",&n,&m);
  while(n!=0){
    memset(sum,0,sizeof(sum));
    memset(num,0,sizeof(num));
    int i,j,cnt=0;
    num[0]=0;
    for(i=1;i<=n;i++){
      scanf("%d",&(num[i]));
    }
    for(i=0;i<=n;i++){
      for(j=i;j<=n;j++){
	sum[cnt]=num[i]+num[j];cnt++;
      }
    }
    sort(sum,sum+cnt);
       
    int ans=0;
    for(i=0;i<cnt;i++){
      if(sum[i]<=m){
	int mi,hi,tyu,tai;
	tai=m-sum[i];
	mi=0;hi=cnt-1;
	while(hi-mi>1){
	  tyu=(hi+mi)/2;
	  if(sum[tyu]<=tai){
	    mi=tyu;
	  }
	  else{
	    hi=tyu;
	  }
	}
	//printf("%d %d\n",sum[i],sum[mi]);
	if(tai-sum[mi]<m-ans){
	  ans=sum[mi]+sum[i];
	}
      }
      else break;
    }
    printf("%d\n",ans);
    scanf("%d%d",&n,&m);
  }
  return 0;
}

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

int main(){
  while(true){
    int n,m,p[1001];
    vector<int> a;
    scanf("%d %d",&n,&m);
    if(n==0&&m==0) return 0;
    p[0] = 0;
    for(int i=0;i<n;i++) scanf("%d",p+i+1);
    n++;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        a.push_back(p[i]+p[j]);
      }
    }
    sort(a.begin(),a.end());
    a.erase(unique(a.begin(),a.end()),a.end());
    int ans = 0;
    for(int i=0;i<a.size();i++){
      int x = m - a[i];
      int j = upper_bound(a.begin(),a.end(),x) - a.begin();
      if(j!=0) ans = max(ans,a[i]+a[j-1]);
    }
    printf("%d\n",ans);
  }
}