-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlackJack.java
52 lines (37 loc) · 991 Bytes
/
BlackJack.java
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
51
52
package algo.Algorithms;
import java.util.Scanner;
/**
* https://www.acmicpc.net/problem/2798
* 백준 2798번 문제 블랙잭
*/
public class BlackJack {
private static int n;
private static int m;
private static int answer;
private static int cards[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
cards = new int[n];
for (int i = 0; i < n; i++) {
cards[i] = sc.nextInt();
}
findMaxSum(0, 0, 0);
System.out.println(answer);
}
private static void findMaxSum(int index, int sum, int count) {
if (sum > m) {
return;
}
if(index>=n){
return;
}
if(count == 3){
answer = Math.max(answer, sum);
return;
}
findMaxSum(index+1, sum+cards[index], count+1);
findMaxSum(index+1, sum, count);
}
}