import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; immutable int INF = 1 << 29; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto W = readln.split.map!(to!int).array; if (M == 0) { writeln(0); return; } // dp[i][j][k][l] 頂点iまででj個の辺をとったときの最大値 // k: 直前の頂点を取ったか? // l: 頂点0を取ったか? auto dp = new int[][][][](N+1, M+1, 2, 2); foreach (i; 0..N+1) foreach (j; 0..M+1) foreach(k; 0..2) fill(dp[i][j][k], -INF); dp[0][0][0][0] = 0; dp[0][1][1][1] = 0; foreach (i; 1..N) { foreach (j; 0..M+1) { foreach (k; 0..2) { foreach (l; 0..2) { if (dp[i-1][j][k][l] == -INF) continue; int take = 0; if (k) take += W[i-1]; if (i == N-1 && l) take += W[N-1]; if (j < M) dp[i][j+1][1][l] = max(dp[i][j+1][1][l], dp[i-1][j][k][l] + take); dp[i][j][0][l] = max(dp[i][j][0][l], dp[i-1][j][k][l]); } } } } int ans = -INF; foreach (k; 0..2) foreach (l; 0..2) ans = max(ans, dp[N-1][M][k][l]); ans.writeln; }