#include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) typedef long long ll; using namespace std; typedef vector vll; typedef vector vvll; typedef vector vvvll; typedef vector vvvvll; const ll INF = 1e9; int main() { int n, m; cin >> n >> m; vector w(n); repeat (i,n) cin >> w[i]; vvvvll dp(2, vvvll(n+1, vvll(m+1, vll(2, - INF)))); dp[false][0][0][false] = 0; dp[ true][0][1][ true] = 0; repeat (p,2) repeat (i,n-1) repeat (j,m+1) { ;;;;;;;;;;; dp[p][i+1][j][false] = max(dp[p][i][j ][false], dp[p][i][j ][true]); if (j >= 1) dp[p][i+1][j][ true] = max(dp[p][i][j-1][false], dp[p][i][j-1][true] + w[i]); } repeat (j,m+1) { dp[false][n][j][false] = max(dp[false][n-1][j][false], dp[false][n-1][j][true]); dp[ true][n][j][ true] = max(dp[ true][n-1][j][false], dp[ true][n-1][j][true] + w[n-1]); } ll ans = max(dp[false][n][m][false], dp[true][n][m][true]); cout << ans << endl; return 0; }