#include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) template bool setmax(T & l, T const & r) { if (not (l < r)) return false; l = r; return true; } using namespace std; const int INF = 1e9; int main() { int n, m; cin >> n >> m; vector w(n); repeat (i,n) cin >> w[i]; int ans = - INF; repeat (p,2) { vector > prv; vector > cur(m+1, vector(2, - INF)); auto fly = [&]() { cur.swap(prv); cur.clear(); cur.resize(m+1, vector(2, - INF)); }; cur[p][p] = 0; repeat (i,n-1) { fly(); repeat (j,m+1) { ;;;;;;;;;;; cur[j][false] = max(prv[j ][false], prv[j ][true]); if (j >= 1) cur[j][ true] = max(prv[j-1][false], prv[j-1][true] + w[i]); } } fly(); repeat (j,m+1) { cur[j][false] = max(prv[j][false], prv[j][true]); cur[j][ true] = max(prv[j][false], prv[j][true] + (p ? w[n-1] : 0)); } setmax(ans, cur[m][p]); } cout << ans << endl; return 0; }