//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 100010001; const ll LINF = (ll)INF*INF*10; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} const int N = 3010; //head int n, m; int w[N]; int dpe[N], dpn[N]; int ans; int main() { scanf("%d%d", &n, &m); rep(i, n) scanf("%d", w+i); if(m == 0) { printf("0\n"); return 0; } if(m == n) { printf("%d\n", accumulate(w, w+n, 0)); return 0; } fill(dpe, dpe+m+1, -INF); dpe[1] = 0; fill(dpn, dpn+m+1, -INF); rep(i, n-2) { for(int j = m; j >= 0; j--) { chmax(dpn[j], dpe[j]); if(j) { dpe[j] = max(dpe[j-1]+w[i], dpn[j-1]); } } } chmax(dpn[m], dpe[m]); dpe[m] = max(dpe[m-1]+w[n-2]+w[n-1], dpn[m-1]+w[n-1]); ans = max(dpe[m], dpn[m]); fill(dpe, dpe+m+1, -INF); fill(dpn, dpn+m+1, -INF); dpn[0] = 0; rep(i, n-1) { for(int j = m; j >= 0; j--) { chmax(dpn[j], dpe[j]); if(j) { dpe[j] = max(dpe[j-1]+w[i], dpn[j-1]); } } } chmax(ans, max(dpe[m], dpn[m])); printf("%d\n", ans); }