/** * @FileName a.cpp * @Author kanpurin * @Created 2020.06.01 02:42:29 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; int main() { int n, m; cin >> n >> m; if (m == 0) { cout << 0 << endl; return 0; } constexpr int INF = 1e9 + 6; vector< vector< vector< vector< int > > > > dp(n + 1, vector< vector< vector< int > > >(m + 1, vector< vector< int > >(2, vector< int >(2, -INF)))); dp[0][0][0][0] = 0; vector w(n); for (int i = 0; i < n; i++) { cin >> w[i]; } for (int i = 0; i < n; i++) { if (i == 0) { for (int j = 0; j <= m; j++) { dp[i + 1][j][0][0] = max(dp[i][j][0][0], dp[i][j][1][0]); if (j - 1 >= 0) dp[i + 1][j][1][1] = max(dp[i][j - 1][0][0], dp[i][j - 1][1][0]); } } else if (i == n - 1) { for (int j = 0; j <= m; j++) { dp[i + 1][j][0][0] = max(dp[i][j][0][0], dp[i][j][1][0]); dp[i + 1][j][0][1] = max(dp[i][j][0][1], dp[i][j][1][1]); if (j - 1 >= 0) dp[i + 1][j][1][0] = max(dp[i][j - 1][0][0], dp[i][j - 1][1][0] + w[i-1]); if (j - 1 >= 0) dp[i + 1][j][1][1] = max(dp[i][j - 1][0][1] + w[i], dp[i][j - 1][1][1] + w[i-1] + w[i]); } } else { for (int j = 0; j <= m; j++) { dp[i + 1][j][0][0] = max(dp[i][j][0][0], dp[i][j][1][0]); dp[i + 1][j][0][1] = max(dp[i][j][0][1], dp[i][j][1][1]); if (j - 1 >= 0) dp[i + 1][j][1][0] = max(dp[i][j - 1][0][0], dp[i][j - 1][1][0] + w[i-1]); if (j - 1 >= 0) dp[i + 1][j][1][1] = max(dp[i][j - 1][0][1], dp[i][j - 1][1][1] + w[i-1]); } } } cout << max({dp[n][m][0][0],dp[n][m][0][1],dp[n][m][1][0],dp[n][m][1][1]}) << endl; return 0; }