티스토리 뷰

9229. 한빈이와 Spot Mart

swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AW8Wj7cqbY0DFAXN&categoryId=AW8Wj7cqbY0DFAXN&categoryType=CODE&problemTitle=%ED%95%9C%EB%B9%88&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

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개 뽑기)

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함