#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int INF = INT_MAX / 2; int main() { int n, m; cin >> n >> m; vector w(n); for(int i=0; i> w[i]; vector > dp(m+1, vector(4, -INF)); dp[m][0] = 0; for(int i=0; i > nextDp(m+1, vector(4, -INF)); for(int a=0; a<=m; ++a){ for(int b=0; b<4; ++b){ nextDp[a][b&2] = max(nextDp[a][b&2], dp[a][b]); if(a > 0){ int x = 0; if(b & 1) x += w[i]; if(i == n - 1 && (b & 2)) x += w[0]; if(i == 0) nextDp[a-1][b|3] = max(nextDp[a-1][b|3], dp[a][b] + x); else nextDp[a-1][b|1] = max(nextDp[a-1][b|1], dp[a][b] + x); } } } dp.swap(nextDp); } int ans = *max_element(dp[0].begin(), dp[0].end()); cout << ans << endl; return 0; }