일상

2961 : 도영이가 만든 맛있는 음식

STUDYING,,, 2021. 8. 14. 16:27
import java.util.Scanner;

public class Main {
	static int N, dif = Integer.MAX_VALUE;
	static int[] S;
	static int[] B;
	static boolean[] result;
	
	
	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		
		S = new int[N];
		B = new int[N];
		result = new boolean[N];
		
		for(int i = 0; i < N ; i++) {
			S[i] = sc.nextInt();
			B[i] = sc.nextInt();
		}
		
		subset(0,1,0);
		System.out.println(dif);
		
	}

	private static void subset(int target, int s, int b) {
		
		if(target == N) {
			if(dif > Math.abs(s-b) && b!=0)
				dif = Math.abs(s-b);
			
			return;
		}
		
		
		result[target] =true;
		subset(target +1, s*S[target], b+B[target]);
		result[target] = false;
		subset(target +1, s, b);
		
		
	}

}

'일상' 카테고리의 다른 글

3985 : 롤 케이크  (0) 2021.08.16
16926 : 배열 돌리기 1  (0) 2021.08.14
2798 : 블랙잭  (0) 2021.08.13
15686 : 치킨 배달  (0) 2021.08.13
3040 : 백설 공주와 일곱 난쟁이  (0) 2021.08.12