#include #include using namespace std; using i64 = long long; class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x w; //vector>>> dp; int dp[2][2][3005][3005]; // i番目まで見た。j個の頂点を取った。before: 前を取った? first: 最初を取った? // 最大の価値を返す。 int rec(int i, int j, bool before, bool first) { // int &res = dp[i][j][before][first]; int &res = dp[before][first][i][j]; if(i==n) { if(j==m) { return res = (before&&first ? w[i-1] : 0); } else { return res = -inf; } } if(res != -1) { return res; } res = -inf; // 前を取ってた if(before) { // i番目を取る if(j < m) { res = max(res, rec(i+1, j+1, true, first) + w[i-1]); } // 取らない res = max(res, rec(i+1, j, false, first)); // 前を取ってない } else { // i番目を取る if(j < m) { res = max(res, rec(i+1, j+1, true, first)); } // 取らない res = max(res, rec(i+1, j, false, first)); } return res; } int main(void) { scanf("%d%d", &n, &m); w.assign(n, 0); for(int i : range(3005)) for(int j : range(3005)) for(int k : range(2)) for(int L : range(2)) dp[k][L][i][j] = -1; // dp.assign(n+10, vector>>(m+10, vector>(2, vector(2, -1)))); for(int i : range(n)) { scanf("%d", &w[i]); } int res = max(rec(1, 1, 1, 1), rec(1, 0, 0, 0)); printf("%d\n", res); return 0; }