https://www.acmicpc.net/problem/2228
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int N, M;
static int[] nums;
static int[][][] dp;
static void input() {
Reader scanner = new Reader();
N = scanner.nextInt();
M = scanner.nextInt();
nums = new int[N + 1];
dp = new int[N + 1][M + 1][2];
for(int num = 0; num <= N; num++) {
for(int partition = 0; partition <= M; partition++)
Arrays.fill(dp[num][partition], Integer.MIN_VALUE);
}
for(int idx = 1; idx <= N; idx++)
nums[idx] = scanner.nextInt();
}
static void solution() {
dp[0][0][1] = dp[0][0][0] = 0;
for(int num = 1; num <= N; num++) {
for(int partition = 0; partition <= Math.min(M, num / 2); partition++)
dp[num][partition][0] = Math.max(dp[num - 1][partition][1], dp[num - 1][partition][0]);
for(int partition = 1; partition <= Math.min(M, (num + 1) / 2); partition++)
dp[num][partition][1] = Math.max(dp[num - 1][partition - 1][0], dp[num - 1][partition][1]) + nums[num];
}
System.out.println(Math.max(dp[N][M][0], dp[N][M][1]));
}
public static void main(String[] args) {
input();
solution();
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
dp[N][partition][0] = max(dp[N - 1][partition][0], dp[N - 1][partition][1])
dp[N][partition][1] = max(dp[N - 1][partition - 1][0], dp[N - 1][partition][1]) + (N번째 수)