File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Main {
7+
8+ // IO field
9+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+ static StringTokenizer st;
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static int nextInt() {return Integer . parseInt(st. nextToken());}
15+ static long nextLong() {return Long . parseLong(st. nextToken());}
16+ static void bwEnd() throws Exception {bw. flush();bw. close();}
17+
18+ // Additional field
19+ static int [] dp;
20+ static int N , K ;
21+
22+ public static void main(String [] args) throws Exception {
23+
24+ // ready();
25+ solve();
26+
27+ bwEnd();
28+ }
29+
30+ static void ready() throws Exception {
31+
32+
33+ }
34+
35+ static void solve() throws Exception {
36+
37+ nextLine();
38+ N = nextInt();
39+ K = nextInt();
40+ dp = new int [100001 ];
41+ Arrays . fill(dp, Integer . MAX_VALUE- 1 );
42+ dp[0 ] = 0 ;
43+ nextLine();
44+ while (N -- > 0 ) {
45+ int now = nextInt();
46+ for (int j= 100000 ;j> now;j-- ) if (dp[j- now] != 0 ) dp[j] = Math . min(dp[j- now]+ 1 , dp[j]);
47+ dp[now] = 1 ;
48+ }
49+ bw. write((dp[K ] == Integer . MAX_VALUE- 1 ? - 1 : dp[K ]) + " \n " );
50+
51+
52+ }
53+
54+ }
55+
56+ ```
You can’t perform that action at this time.
0 commit comments