import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class baekjoon2798 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N,M,max=0;
static Integer[] arr;
public static void main(String args[])throws IOException{
st = new StringTokenizer(br.readLine()," ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine()," ");
arr = new Integer[N];
for(int i=0;i<N;i++){
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr,Comparator.reverseOrder());
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++){
for(int k=j+1;k<N;k++){
if(arr[i]+arr[j]+arr[k]<=M){
if(max<arr[i]+arr[j]+arr[k]){
max=arr[i]+arr[j]+arr[k];
}
break;
}
}
}
}
System.out.println(max);
}
}
단순반복 브루트포스문제