import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Iterator; public class Main_yukicoder324_1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Printer pr = new Printer(System.out); final int INF = Integer.MAX_VALUE / 2; int n = sc.nextInt(); int m = sc.nextInt(); int[] w = new int[n]; for (int i = 0; i < n; i++) { w[i] = sc.nextInt(); } int[][][] dp = new int[2][m + 1][2]; int[][][] dp2 = new int[2][m + 1][2]; for (int i = 0; i < 2; i++) { for (int j = 0; j <= m; j++) { Arrays.fill(dp[i][j], -INF); Arrays.fill(dp2[i][j], -INF); } } for (int i = 0; i < 2; i++) { dp[i][0][0] = 0; } dp2[0][1][1] = 0; for (int i = 1; i < n; i++) { int tmpi = i % 2; int tmpim = (i + 1) % 2; for (int j = 1; j <= m; j++) { if (i != n - 1) { dp[tmpi][j][0] = Math.max(dp[tmpi][j][0], Math.max(dp[tmpim][j][0], dp[tmpim][j][1])); dp[tmpi][j][1] = Math.max(dp[tmpi][j][1], Math.max(dp[tmpim][j - 1][0], dp[tmpim][j - 1][1] + w[i - 1])); dp2[tmpi][j][0] = Math.max(dp2[tmpi][j][0], Math.max(dp2[tmpim][j][0], dp2[tmpim][j][1])); dp2[tmpi][j][1] = Math.max(dp2[tmpi][j][1], Math.max(dp2[tmpim][j - 1][0], dp2[tmpim][j - 1][1] + w[i - 1])); } else { dp[tmpi][j][0] = Math.max(dp[tmpi][j][0], Math.max(dp[tmpim][j][0], dp[tmpim][j][1])); dp[tmpi][j][1] = Math.max(dp[tmpi][j][1], Math.max(dp[tmpim][j - 1][0], dp[tmpim][j - 1][1] + w[i - 1])); dp2[tmpi][j][0] = Math.max(dp2[tmpi][j][0], Math.max(dp2[tmpim][j][0], dp2[tmpim][j][1])); dp2[tmpi][j][1] = Math.max(dp2[tmpi][j][1], Math.max(dp2[tmpim][j - 1][0] + w[i], dp2[tmpim][j - 1][1] + w[i - 1] + w[i])); } } } int max = -INF; for (int i = 0; i < 2; i++) { max = Math.max(max, dp[(n - 1) % 2][m][i]); max = Math.max(max, dp2[(n - 1) % 2][m][i]); } pr.println(max); pr.close(); sc.close(); } @SuppressWarnings("unused") private static class Scanner { BufferedReader br; Iterator it; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() throws RuntimeException { try { if (it == null || !it.hasNext()) { it = Arrays.asList(br.readLine().split(" ")).iterator(); } return it.next(); } catch (IOException e) { throw new IllegalStateException(); } } int nextInt() throws RuntimeException { return Integer.parseInt(next()); } long nextLong() throws RuntimeException { return Long.parseLong(next()); } float nextFloat() throws RuntimeException { return Float.parseFloat(next()); } double nextDouble() throws RuntimeException { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { // throw new IllegalStateException(); } } } private static class Printer extends PrintWriter { Printer(PrintStream out) { super(out); } } }