-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWAcademy 5215
50 lines (46 loc) · 1.2 KB
/
SWAcademy 5215
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.*;
import java.util.StringTokenizer;
public class Solution {
static int[] TASTE, CAL;
static boolean[] SELECT;
static int N, L, SUMTASTE, SUMCAL, MAX;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
StringTokenizer st = new StringTokenizer(br.readLine());
MAX = 0;
N = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken());
TASTE = new int[N];
CAL = new int[N];
SELECT = new boolean[N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
TASTE[i] = Integer.parseInt(st.nextToken());;
CAL[i] = Integer.parseInt(st.nextToken());
}
check(0);
System.out.println("#"+t+" "+MAX);
}
}
private static void check(int index) {
if(index == N) {
SUMTASTE = 0; SUMCAL = 0;
for (int i = 0; i < N; i++) {
if(SELECT[i]) {
SUMTASTE += TASTE[i];
SUMCAL += CAL[i];
}
}
if(SUMCAL <= L) {
MAX = Math.max(SUMTASTE, MAX);
}
}else {
SELECT[index] = true;
check(index+1);
SELECT[index] = false;
check(index+1);
}
}
}