-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
62 lines (53 loc) · 1.67 KB
/
Main.java
File metadata and controls
62 lines (53 loc) · 1.67 KB
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
57
58
59
60
61
62
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int S = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[] volumns = new int[N+1];
volumns[0] = S;
st = new StringTokenizer(br.readLine());
for(int i=1; i<volumns.length; i++){
volumns[i] = Integer.parseInt(st.nextToken());
}
int[][] dp = new int[N+1][M+1];
for(int i=0; i<dp.length; i++){
Arrays.fill(dp[i], 0);
}
dp[0][S] = 1;
for(int i=1; i<N+1; i++){
for(int j=0; j<M+1; j++){
if(dp[i-1][j] == 0) continue;
if(j - volumns[i] >= 0){
dp[i][j-volumns[i]] = 1;
}
if(j + volumns[i] <= M){
dp[i][j+volumns[i]] = 1;
}
}
}
/*
for(int i=0; i<dp.length; i++){
for(int j=0; j<dp[i].length; j++){
System.out.print(dp[i][j] + " ");
}
System.out.println();
}
*/
int answer = -1;
for(int i=M; i >= 0; i--){
if(dp[N][i] == 1){
answer = i;
break;
}
}
System.out.println(answer);
br.close();
}
}