#include #include #include #include #include using namespace std; #define FOR(x,y) for(int x = 0;x < y;x++) #define LLI long long int template class UF { public: vector par,rank,cnt; UF() {par=rank=vector(um,0); cnt=vector(um,1); for(int i=0;irank[y]) return par[x]=y; rank[x]+=rank[x]==rank[y]; return par[y]=x; } }; int dp[2][2][5000][5000]; int w[5000]; int main() { int n, m; cin >> n >> m; FOR(i, n) { cin >> w[i]; } int* pointer = &dp[0][0][0][0]; FOR(i, 2 * 2 * 5000 * 5000) { pointer[i] = INT32_MIN; } dp[0][0][0][0] = 0; dp[0][1][0][0] = 0; dp[1][0][0][1] = 0; dp[1][1][0][1] = 0; int maxValue = INT32_MIN; FOR(u ,2) { FOR(i, n-1) { FOR(j,m + 1) { dp[u][0][i+1][j] = max(dp[u][0][i][j], dp[u][1][i][j]); //cout << "not used" << dp[u][0][i+1][j] << endl; if(j > 0) { dp[u][1][i+1][j] = max(dp[u][0][i][j-1], dp[u][1][i][j-1] + w[i]); //cout << "used" << dp[u][1][i+1][j] << endl; } } } maxValue = max(maxValue, dp[u][0][n-1][m]); if(u == 0) { maxValue = max(maxValue, dp[u][1][n-1][m]); } else if(u == 1) { maxValue = max(maxValue, dp[u][1][n-1][m] + w[n-1]); } //cout << "max " << maxValue << endl; } cout << maxValue << endl; return 0; }