티스토리 뷰
9229. 한빈이와 Spot Mart
import java.io.*;
import java.util.*;
// 210208
public class Solution_D3_9229_한빈이와SpotMart {
static int N, M;
static int[] snack;
static int maxWeight;
static void combination(int cnt, int L, int total) {
if (total > M) {
return;
}
if (cnt == 2) {
maxWeight = Math.max(maxWeight, total);
return;
}
if (L == N) {
return;
}
combination(cnt+1, L+1, total + snack[L]);
combination(cnt, L+1, total);
} //
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringTokenizer st;
for (int tc=1; tc<=T; tc++) {
st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken()); // 개수
M = Integer.parseInt(st.nextToken());// 최대 무대
snack = new int[N];
maxWeight = 0;
st = new StringTokenizer(br.readLine(), " ");
for(int i=0; i<N; i++) {
snack[i] = Integer.parseInt(st.nextToken());
}
combination(0, 0, 0);
if (maxWeight == 0) maxWeight = -1;
System.out.println("#" + tc + " " + maxWeight);
} // for
br.close();
} // main
}
cnt 따로 세는 조합 문제 (n개 중 r개 뽑기)
'코딩테스트 > SW Expert' 카테고리의 다른 글
[SWEA] 1228. [S/W 문제해결 기본] 8일차 - 암호문1 (0) | 2021.02.09 |
---|---|
[SWEA] 1494. 사랑의 카운슬러 (0) | 2021.02.09 |
[SWEA] 5215. 햄버거 다이어트 - 부분집합 (0) | 2021.02.09 |
[SWEA] 1940. 가랏! RC카! (0) | 2021.02.08 |
[SWEA] 1234. [S/W 문제해결 기본] 10일차 - 비밀번호 (0) | 2021.02.06 |
댓글