-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path자두나무.java
More file actions
59 lines (46 loc) · 1.78 KB
/
자두나무.java
File metadata and controls
59 lines (46 loc) · 1.78 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
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 t = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int[][][] dp = new int[t][w + 1][3];
int[] arr = new int[t];
for (int i = 0; i < t; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
if (arr[0] == 1) {
dp[0][0][1] = 1;
dp[0][1][2] = 0;
} else {
dp[0][0][1] = 0;
dp[0][1][2] = 1;
}
for (int i = 1; i < t; i++) {
if (arr[i] == 1) {
dp[i][0][1] = dp[i - 1][0][1] + 1;
dp[i][0][2] = dp[i - 1][0][2];
for (int j = 1; j <= w; j++) {
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][2]) + 1;
dp[i][j][2] = Math.max(dp[i - 1][j - 1][1], dp[i - 1][j][2]);
}
} else {
dp[i][0][1] = dp[i - 1][0][1];
dp[i][0][2] = dp[i - 1][0][2] + 1;
for (int j = 1; j <= w; j++) {
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][2]);
dp[i][j][2] = Math.max(dp[i - 1][j - 1][1], dp[i - 1][j][2]) + 1;
}
}
}
int res = 0;
for (int i = 0; i <= w; i++) {
res = Math.max(res, Math.max(dp[t - 1][i][1], dp[t - 1][i][2]));
}
System.out.println(res);
}
}