2024-09-20 17:45:58 -03:00

48 lines
1.2 KiB
C++

#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int n,m, x;
vector<vector<int>> shelfs, optansw, prefs, tabulardp;
cin >> n >> m;
shelfs.assign(n, vector<int>());
prefs.assign(n, vector<int>());
optansw.assign(n, vector<int>(min(m+1, 101), 0));
for(int i = 0; i < n; i++){
cin >> x;
shelfs[i].resize(x);
prefs[i].resize(x+1); prefs[i][0] = 0;
for(int j = 0; j < x; j++){
cin >> shelfs[i][j];
prefs[i][j+1] = prefs[i][j] + shelfs[i][j];
}
}
for(int i = 0; i < n; i++){
optansw[i][shelfs[i].size()] = prefs[i][shelfs[i].size()];
for(int l = 0; l < shelfs[i].size(); l++){
for(int r = l; r < shelfs[i].size(); r++){
optansw[i][shelfs[i].size() - (r-l+1)] = max(optansw[i][shelfs[i].size() - (r-l+1)], prefs[i][shelfs[i].size()] - prefs[i][r+1] + prefs[i][l]);
}
}
}
tabulardp.assign(n+1, vector<int>(m+1, 0));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){ // Descendo inicialmente j e pegando opt[0]
int stop = min((int)optansw[i-1].size()-1, j);
for(int k = 0; k <= stop; k++){
tabulardp[i][j] = max(tabulardp[i][j], tabulardp[i-1][j-k] + optansw[i-1][k]);
}
}
}
cout << tabulardp[n][m] << '\n';
return 0;
}