https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWT-lPB6dHUDFAVT 

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

 

 

 

 

์‹คํŒจ ์ฝ”๋“œ - ๋ชจ๋“  tc ํ†ต๊ณผx

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
	static int N, L;
	static int[] T, K;
	static boolean[] visited;
	static int[] result; //์กฐํ•ฉ ๊ฒฐ๊ณผ(์ธ๋ฑ์Šค ์ €์žฅ)
	static int max = 0;

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int TC = Integer.parseInt(br.readLine());
		
		for(int tc = 1; tc <= TC; tc++) {
			StringTokenizer st = new StringTokenizer(br.readLine(), " ");
			
			N = Integer.parseInt(st.nextToken());
			L = Integer.parseInt(st.nextToken()); //์ œํ•œ ์นผ๋กœ๋ฆฌ
		
			T = new int[N]; //๋ง› ์ ์ˆ˜
			K = new int[N]; //์นผ๋กœ๋ฆฌ
			
			for(int i = 0; i < N; i++) {
				st = new StringTokenizer(br.readLine(), " ");
				
				T[i] = Integer.parseInt(st.nextToken());
				K[i] = Integer.parseInt(st.nextToken());
			}
			
			visited = new boolean[N];
			
			for(int i = 1; i <= N; i++) {
				result = new int[i];
				combination(0, 0, i); //n๊ฐœ ์กฐํ•ฉ
			}
			
			//์ถœ๋ ฅ
			System.out.println("#" +  tc + " " + max);
		}

	}

	public static void combination(int start, int depth, int n) {
		if(depth == n) {
			int scoreSum = 0;
			int calSum = 0;
			
			for(int i = 0; i < result.length; i++) {
				scoreSum += T[result[i]];
				calSum += K[result[i]];
			}
			
			if(calSum <= L) {
				max = Math.max(max, scoreSum);
			}
			
			return;
		}
		
		for(int i = start; i < N; i++) {
			if(visited[i]) continue;
			
			visited[i] = true;
			result[depth] = i; //์ธ๋ฑ์Šค๋ฅผ ์ €์žฅ
			combination(i + 1, depth + 1, n);
			visited[i] = false;
		}
		
	}
}

 

 

 

 

 

์•Œ๊ณ ๋ณด๋‹ˆ ํ…Œ์ผ€ ์•ˆ์—์„œ max = 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ์•ˆํ•ด์คฌ๋‹ค.....

 

์ €๋ ‡๊ฒŒ ์กฐํ•ฉ์œผ๋กœ ํ’€ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋ถ€๋ถ„ ์ง‘ํ•ฉ์œผ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

๋‹ค๋ฅธ ํ’€์ด

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Solution {
    static int N, L;
    static int[] T;
    static int[] K;
    static int max;
 
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
        int TC = Integer.parseInt(br.readLine());
         
        for(int tc = 1; tc <= TC; tc++) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
             
            N = Integer.parseInt(st.nextToken());
            L = Integer.parseInt(st.nextToken()); //์ œํ•œ ์นผ๋กœ๋ฆฌ
         
            T = new int[N]; //๋ง› ์ ์ˆ˜
            K = new int[N]; //์นผ๋กœ๋ฆฌ
             
            for(int i = 0; i < N; i++) {
                st = new StringTokenizer(br.readLine(), " ");
                 
                T[i] = Integer.parseInt(st.nextToken());
                K[i] = Integer.parseInt(st.nextToken());
            }
             
            max = 0;
            dfs(0, 0, 0);
             
            //์ถœ๋ ฅ
            System.out.println("#" + tc + " " + max);
        }
 
    }
 
    public static void dfs(int depth, int score, int cal) {
        if (cal > L) return;
         
        if(depth == N) {
            max = Math.max(max, score);
            return;
        }
         
        dfs(depth + 1, score + T[depth], cal + K[depth]); //์žฌ๋ฃŒ ํฌํ•จ
        dfs(depth + 1, score, cal); //์žฌ๋ฃŒ ํฌํ•จx    
    }
}
giraffe_