#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define minimize(a, x) a = std::min(a, x) #define maximize(a, x) a = std::max(a, x) typedef long long ll; int const inf = 1<<29; int main() { int N, M; cin >> N >> M; vector>>> dp(N+1, vector>>(M+1, vector>(2, vector(2, -inf)))); int w[N]; rep(i, N) { cin >> w[i]; } dp[0][M][0][0] = 0; rep(i, N) rep(used, M+1) rep(prevused, 2) rep(firstused, 2) { auto && curr = dp[i][used][prevused][firstused]; if(curr == inf) { continue; } if(used) { int ncost = curr + (i > 0 && prevused ? w[i-1] : 0); if(i == N-1 && firstused) ncost += w[N-1]; maximize(dp[i+1][used-1][1][firstused || i == 0], ncost); } maximize(dp[i+1][used][0][firstused], curr); } int max = -inf; rep(i, 2) rep(j, 2) { maximize(max, dp[N][0][i][j]); } cout << max << endl; return 0; }