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)
	}
}

Python: # AOJ 0529: Darts
# Python3 2018.7.1 bal4u

import bisect
while True:
	n, m = map(int, input().split())
	if n == 0: break
	p = [int(input()) for i in range(n)]
	p.append(0)
	p.sort()
	s = [i+j for i in p for j in p if i <= j]
	s.sort()
	t = bisect.bisect_left(s, m)
	h = bisect.bisect_left(s, m>>1, 0, t)
	if t < len(s):
		if s[t] == m:
			print(m);
			continue
		s = s[:t]
	ans = 0
	for i in range(h-1, len(s)):
		t = bisect.bisect_left(s, m-s[i])
		if s[i]+s[t-1] > ans: ans = s[i]+s[t-1]
	print(ans)

Python: import bisect
def main():
    while True:
        n, m = map(int, input().split())
        if n == 0:
            break
        p = [0] + list(int(input()) for i in range(n))
        point = []
        for i in range(n):
            for j in p[i:]:
                a = p[i] + j
                if a < m:
                    point.append(a)
        point.sort()

        print(max(i + point[bisect.bisect_left(point, m - i) - 1] for i in point))
            
if __name__=="__main__":
	main()

Python: def solve():
  while True:
    n,m = map(int,input().split())
    if not n and not m: break
    
    lst = [int(input()) for _ in range(n)]
    lst.append(0)
    lst = sorted(lst)
    lst2 = [i + j for i in lst for j in lst if i <= j]
    lst2 = sorted(lst2)
    A = []
    index = len(lst2) - 1
    for i in lst2:
      if i + lst2[-1] <= m:
        A.append(i + lst2[-1])
      elif i <= m:
        while True:
          if i + lst2[index] <= m:
            break
          index -= 1
        A.append(i + lst2[index])
      else:
        break
    print(max(A))
solve()

Python: # -*- coding: utf-8 -*-
"""
Created on Mon May  7 22:55:31 2018

@author: maezawa
"""

def search(p, m, i):
    if i == 1:
        if m<p[0]:
            return -1
        for j, k in enumerate(p):            
            if k>m:
                return p[j-1]
            elif j==n-1:
                return p[n-1]
    else:
        dif = 2*10**8
        for j, k in enumerate(p):
            if m-k<0:
                break
            a = search(p, m-k, i-1)
            #print(m-k, a, i-1)
            if m-k-a < dif:
                dif = m-k-a
        return m-dif

while True:
    p = []
    n, m = list(map(int, input().split()))
    if n==0 and m==0:
        break
    for i in range(n):
        p.append(int(input()))
    p.sort()
    dif = 2*10**8
    for i in range(1,5):
        a = search(p, m, i)
        if a<0:
            continue
        #print("outer",i,a)
        if m-a < dif:
            dif = m-a
    print(m-dif)

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0529
# http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2985387#1

import bisect
while True:
    n, m = map(int, input().split())
    if n == 0: break
    p = [int(input()) for i in range(n)]
    p.append(0)
    p.sort()
    s = [i+j for i in p for j in p if i <= j]
    s.sort()
    t = bisect.bisect_left(s, m)
    h = bisect.bisect_left(s, m>>1, 0, t)
    if t < len(s):
        if s[t] == m:
            print(m);
            continue
        s = s[:t]
    ans = 0
    # for i in range(h-1, len(s)):
    for i in range(h):
        t = bisect.bisect_left(s, m-s[i])
        if s[i]+s[t-1] > ans: ans = s[i]+s[t-1]
    print(ans)

Python: # -*- coding: utf-8 -*-
"""
Created on Mon May  7 22:55:31 2018

@author: maezawa
"""

def search(p, m, i):
    if i == 1:
        if m<p[0]:
            return -1
        for j, k in enumerate(p):            
            if k>m:
                return p[j-1]
            elif j==n-1:
                return p[n-1]
    else:
        dif = 2*10**8
        for j, k in enumerate(p):
            if m-k<0:
                continue
            a = search(p, m-k, i-1)
            #print(m-k, a, i-1)
            if m-k-a < dif:
                dif = m-k-a
        return m-dif

while True:
    p = []
    n, m = list(map(int, input().split()))
    if n==0 and m==0:
        break
    for i in range(n):
        p.append(int(input()))
    p.sort()
    dif = 2*10**8
    for i in range(1,5):
        a = search(p, m, i)
        if a<0:
            continue
        #print("outer",i,a)
        if m-a < dif:
            dif = m-a
    print(m-dif)