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<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<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

int main() {
	int N, M; cin >> N >> M;
	static int dp[200000001];
	memset(dp, 5, sizeof(dp));
	dp[0] = 0;

	for (int i = 0; i < N; i++) {
		int P; cin >> P;
		for (int j = 0; j <= M; j++) {
			if (dp[j] >= 0)
				dp[j + P] = min(
					dp[j + P],
					dp[j] + 1
				);
		}
	}
	for (int j = M; j >= 0; j--) {
		if (dp[j] >= 0 && dp[j] <= 4) {
			cout << j<< endl;
			break;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD=1e9+7;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n,m;
  while(true){
      cin>>n>>m;
      if(n==0&&m==0) break;
      std::vector<ll> v(n),kouho;
      for(int i=0;i<n;i++) cin>>v[i];
      v.push_back(0);
      for(int i=0;i<=n;i++)for(int j=0;j<=n;j++){
          kouho.push_back(v[i]+v[j]);
      }
      ll ma=-1;
      sort(kouho.begin(),kouho.end());
      for(int i=0;i<(int)kouho.size();i++){
          if(i>0&&kouho[i]==kouho[i-1]) continue;
          ll res=m-kouho[i];
          if(res<0) break;
          auto itr=upper_bound(kouho.begin(),kouho.end(),res);
          itr--;
          ma=max(kouho[i]+*itr,ma);
          //cout<<ma<<endl;
      }
      cout<<ma<<endl;
  }
  return 0;
}

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

	int tsP[1001 * 1001];
	int main() {
		int N, M;
		int P[1001];
		int ans;
		int cnt;

		P[0] = 0;

		//cin >> N >> M;
		while (cin>>N>>M,N&&M) {
			ans = 0;
			cnt = 0;
			for (int i = 1; i <= N; i++) {
				cin >> P[i];
			}

			for (int i = 0; i <= N; i++) {
				for (int j = i; j <= N; j++) {
					if (P[i] + P[j] <= M) {
						tsP[cnt++] = P[i] + P[j];
					}

				}
			}
			sort(tsP, tsP + cnt);
			for (int i = 0; i < cnt; i++) {
				//for (int j = i; j < cnt; j++) {
					//if (tsP[i] + tsP[j] <= M) {
						ans = max(ans, /*tsP[i] + tsP[j]*/tsP[i] + *(upper_bound(tsP, tsP + cnt, M - tsP[i]) - 1));
					//}
				//}
			}
			cout << ans << endl;
		}
		return 0;
	}

C++: #include <iostream>
#include <algorithm>
#define N1 1001
using namespace std;
int N,M,P[N1],data[N1][N1][3];
int main(){
  int i,j,maxv,k;
  while(1){
    cin >> N >> M;
    if(N==0&&M==0) break;
    for(i=0;i<N;i++) cin >> P[i];
    for(i=0;i<N;i++){
      for(j=i;j<N;j++){
	data[i][j][0]=P[i]+P[j];
	data[i][j][1]=P[i]+P[j]*2;
	data[i][j][2]=P[i]+P[j]*3;
      }
    }
    maxv=0;
    for(i=0;i<N;i++){
      for(j=i;j<N;j++){
	for(k=0;k<3;k++){
	  if(data[i][j][k]<=M) maxv=max(maxv,data[i][j][k]);
	}
      }
    }
    cout << maxv << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#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];
    }

    vector<int> v(1, 0);
    for (int i = 0; i < N; i++) {
      v.push_back(ps[i]);
      for (int j = i; j < N; j++) {
        if (ps[i] + ps[j] <= M) {
          v.push_back(ps[i] + ps[j]);
        }
      }
    }
    sort(v.begin(), v.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<iostream>
using namespace std;

int main() {

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 1005
#define LSIZE 1000005
#define INF 1000000005LL

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int n;
ll M;
ll T[SIZE];
set<ll> S;
ll K;


ll solve()
{
	scanf("%d %lld",&n,&M);
	if(n+M ==0)return -1;
	for(int i=1;i<=n;i++)scanf("%lld",&T[i]);
	T[0]=0;
	S.clear();
	for(int i=0;i<=n;i++){
		for(int j=i;j<=n;j++){
			S.insert(T[i]+T[j]);
		}
	}
	for(ll i=M;i>=0;i--){
		for(set<ll>::iterator it=S.begin();it!=S.end();it++){
			if(i-(*it) < 0)break;
			if(S.find(i-(*it))!=S.end()){
				return i;
			}
		}
	}
	return 0;
}

int main()
{
	while(1){
		ll ans = solve();
		if(ans==-1)break;
		else printf("%lld\n",ans);
	}
	return 0;
}

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

int main(int argc, char *argv[]){
  int n, m;
  while(cin >> n >> m, n){
    int point[n + 1];
    point[0] = 0;
    set<int> a, b;
    for (int i = 1; i <= n; i++) {
      cin >> point[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        a.insert(point[i] + point[j]);
      }
    }
    int ans = 0;
    for (int i: a) {
      if(--a.upper_bound(m - i) != a.end()){
        ans = max(ans, i + *(--a.upper_bound(m - i)));
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}