#include #include #include #include #include #include #include #include #include #include #include #include #include #define repd(i,a,b) for (int i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) #define var auto #define mod 1000000007 #define inf 2147483647 #define nil -1 #define mp make_pair typedef long long ll; using namespace std; template inline void output(T a, int p) { if(p){ cout << fixed << setprecision(p) << a << "\n"; } else{ cout << a << "\n"; } } // end of template int main() { cin.tie(0); // source code int N, M; cin >> N >> M; vector W(N); rep(i, N){ cin >> W[i]; } vector>>> dp(N, vector>> (N + 1, vector>(2, vector (2, -inf)))); // [いくつまで選んだか][何個選んだか][今いる頂点を選んだか][0番目を選んだか] dp[0][0][0][0] = 0; dp[0][1][1][1] = 0; repd(i, 1, N){ rep(j, i + 2){ rep(z, 2){ dp[i][j][0][z] = max(dp[i - 1][j][0][z], dp[i - 1][j][1][z]); if(j) dp[i][j][1][z] = max(dp[i - 1][j - 1][0][z], dp[i - 1][j - 1][1][z] + W[i - 1]); } } } rep(j, N + 1){ rep(z, 2){ dp[N - 1][j][0][z] = max(dp[N - 2][j][0][z], dp[N - 2][j][1][z]); } if(j){ dp[N - 1][j][1][1] = max(dp[N - 2][j - 1][0][1], dp[N - 2][j - 1][1][1] + W[N - 2]) + W[N - 1]; dp[N - 1][j][1][0] = max(dp[N - 2][j - 1][0][0], dp[N - 2][j - 1][1][0] + W[N - 2]); } } ll ret = -inf; rep(y, 2) rep(z, 2) ret = max(ret, dp[N - 1][M][y][z]); output(ret, 0); return 0; }