-
Notifications
You must be signed in to change notification settings - Fork 46
/
ContestProject.java
56 lines (45 loc) · 982 Bytes
/
ContestProject.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
53
54
55
56
import java.util.List;
import java.util.Scanner;
public class ContestProject {
public static int dem;
public static int sumT;
public static void Dem(int n, int S, List<Integer> totien) {
if(n<0) return;
dem++;
sumT = sumT + totien.get(n);
if(sumT > S) {
dem--;
sumT = sumT - totien.get(n);
Dem(n-1, S, totien);
} else if(sumT == S) {
return;
} else {
Dem(n-1, S, totien);
}
return;
}
public static void main(String[] args) {
int n, S;
List<Integer> totien = null;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
S = scanner.nextInt();
for(int i=0; i<n; i++) {
int temp = 0;
temp = scanner.nextInt();
totien.add(temp);
}
int demMin = 31;
for(int i=n-1; i>=0; i--) {
dem = 0;
sumT = 0;
Dem(i,S,totien);
if((dem<demMin) && (dem>0) && (sumT == S)) {
demMin = dem;
}
}
if(demMin<=30) System.out.println(demMin);
else
System.out.println("-1");
}
}