BOJ_B2_2798
π [B2_2798] λΈλμ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static StringTokenizer st;
static int N,M;
static int[] arr;
static int[] cal;
static int sum;
static int max = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
st = new StringTokenizer(br.readLine(), " ");
// μΉ΄λμ κ°μ
N = Integer.parseInt(st.nextToken());
// μ«μ M
M = Integer.parseInt(st.nextToken());
// μΉ΄λ μ
λ ₯
arr = new int[N];
// κ³μ° λ°°μ΄
cal = new int[3];
st = new StringTokenizer(br.readLine(), " ");
for(int i=0; i<N; i++){
arr[i] = Integer.parseInt(st.nextToken());
}
combination(0,0);
System.out.println(max);
}
// μ‘°ν© κ΅¬νκΈ°
static void combination(int current, int idx){
if(current == 3){
for(int i=0; i<cal.length;i++){
sum += cal[i];
}
if(sum <=M){
max = Math.max(max, sum);
}
// sum μ΄κΈ°ν !!
sum = 0;
return;
}
for(int i=idx; i<N; i++){
cal[current] = arr[i];
combination(current+1,i+1);
}
}
}
π€ λμ μκ°
μΉ΄λ 3μ₯μ λ½λ κ³Όμ μ΄ μμλ₯Ό μκ΄νμ§ μκΈ° λλ¬Έμ μ‘°ν© κ΅¬νλ μμ ν΅ν΄ ꡬννμλ€.
ν©μ΄ Mμ΄ν μΈ κ²μμ κ°μ₯ ν° κ°μ μ°Ύμμ£Όλ©΄ λ !
μ‘°ν©μ ꡬνλ κ΅¬μ‘°λ§ μλ©΄ κ°λ¨νλ€ !