#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair i_i; typedef pair ll_i; typedef pair d_i; typedef pair ll_ll; typedef pair d_d; struct edge { int u, v; ll w; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int INF = INT_MAX / 2; int main() { int n, m; cin >> n >> m; vector a(n); for (int i = 0; i < n; i++) cin >> a[i]; int ma = -INF; { vector dp0(m + 1, -INF), dp1(m + 1, -INF); dp0[0] = 0; for (int i = 1; i < n; i++) { vector _dp0(m + 1, -INF), _dp1(m + 1, -INF); for (int j = 0; j <= m; j++) { _dp0[j] = max(_dp0[j], dp0[j]); _dp0[j] = max(_dp0[j], dp1[j]); if (j + 1 <= m) { _dp1[j + 1] = max(_dp1[j + 1], dp0[j]); _dp1[j + 1] = max(_dp1[j + 1], dp1[j] + a[i - 1]); } } dp0 = _dp0; dp1 = _dp1; } ma = max(ma, dp0[m]); ma = max(ma, dp1[m]); } if (m) { vector dp0(m + 1, -INF), dp1(m + 1, -INF); dp1[1] = 0; for (int i = 1; i < n; i++) { vector _dp0(m + 1, -INF), _dp1(m + 1, -INF); for (int j = 0; j <= m; j++) { _dp0[j] = max(_dp0[j], dp0[j]); _dp0[j] = max(_dp0[j], dp1[j]); if (j + 1 <= m) { _dp1[j + 1] = max(_dp1[j + 1], dp0[j]); _dp1[j + 1] = max(_dp1[j + 1], dp1[j] + a[i - 1]); } } dp0 = _dp0; dp1 = _dp1; } ma = max(ma, dp0[m]); ma = max(ma, dp1[m] + a[n - 1]); } cout << ma << endl; }