SWEA 5215ハンバーグダイエット


すべての組合せを求め、条件に合致して更新すればよい.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Solution {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;
	static int N, L, max_point;
	static int[][] igd; //재료의 점수와 칼로리. 
	
	public static void combi(int current_L, int point, int start) {
        if(current_L > L) return;
		if(max_point < point) {
			max_point = point;
		}
		for(int i = start; i < N; i++) {
			combi(current_L + igd[i][1], point + igd[i][0], i + 1);	
		}
	}
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = Integer.parseInt(br.readLine());
		for(int tc = 1; tc <= T; tc++) {
			st = new StringTokenizer(br.readLine(), " ");
			N = Integer.parseInt(st.nextToken());
			L = Integer.parseInt(st.nextToken());
			igd = new int[N][2]; // [0]은 점수, [1]은 칼로리.
			for(int i = 0; i < N; i++) {
				st = new StringTokenizer(br.readLine(), " ");
				igd[i][0] = Integer.parseInt(st.nextToken());
				igd[i][1] = Integer.parseInt(st.nextToken());
			}
			max_point = 0;
			combi(0, 0, 0);
			
			bw.write(String.format("#%d ", tc));
			bw.write(String.format("%d\n", max_point));
			
			
		}
		
		
		
		bw.flush();
		bw.close();
	}
}