-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ2240.java
More file actions
50 lines (37 loc) · 1.08 KB
/
BOJ2240.java
File metadata and controls
50 lines (37 loc) · 1.08 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
package quki.algorithm.dp;
import java.util.Scanner;
public class BOJ2240 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int a[] = new int[n + 1];
int d[][] = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
}
// ÃʱâÈ ÀÛ¾÷
if (a[1] == 1) {
d[1][0] = 1;
} else {
d[1][1] = 1;
}
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (j == 0) {
d[i][j] = d[i - 1][j];
} else {
d[i][j] = Math.max(d[i - 1][j - 1], d[i - 1][j]);
}
if ((j % 2 == 0 && a[i] == 1) || (j % 2 == 1 && a[i] == 2)) {
d[i][j]++;
}
}
}
int max = 0;
for (int i = 0; i <= m; i++) {
max = Math.max(max, d[n][i]);
}
System.out.println(max);
}
}