File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .Scanner ;
3
+
4
+ public class Main {
5
+
6
+ public static void main (String [] args ) {
7
+ Scanner sc = new Scanner (System .in );
8
+
9
+ int n = sc .nextInt ();
10
+ int s = sc .nextInt ();
11
+ int m = sc .nextInt ();
12
+ ArrayList <Integer > data = new ArrayList <Integer >();
13
+ int [][] dp = new int [101 ][1001 ];
14
+
15
+ for (int i = 0 ; i < n ; i ++) {
16
+ data .add (sc .nextInt ());
17
+ }
18
+
19
+ dp [0 ][s ] = 1 ;
20
+ for (int i = 1 ; i <= n ; i ++) {
21
+ for (int j = 0 ; j <= m ; j ++) {
22
+ if (dp [i - 1 ][j ] == 0 ) continue ;
23
+ if (j - data .get (i - 1 ) >= 0 ) {
24
+ dp [i ][j - data .get (i - 1 )] = 1 ;
25
+ }
26
+ if (j + data .get (i - 1 ) <= m ) {
27
+ dp [i ][j + data .get (i - 1 )] = 1 ;
28
+ }
29
+ }
30
+ }
31
+
32
+ int result = -1 ;
33
+ for (int i = m ; i >= 0 ; i --) {
34
+ if (dp [n ][i ] == 1 ) {
35
+ result = i ;
36
+ break ;
37
+ }
38
+ }
39
+
40
+ System .out .println (result );
41
+ }
42
+
43
+ }
You can’t perform that action at this time.
0 commit comments