//分组背包 for物品 for体积 for 决策 #include <iostream> using namespace std; const int N = 20; int n, m; int w[N][N]; int f[N][N]; int way[N];//方案 int main() { cin >> n >> m; for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) cin >> w[i][j]; //两维,顺序无所谓 for (int i = 1; i <= n; i ++ )//物品 for (int j = 1; j <= m; j ++ )//体积 for (int k = 0; k <= j; k ++ )//决策,枚举每个物品 f[i][j] = max(f[i][j], f[i - 1][j - k] + w[i][k]); cout << f[n][m] << endl; int j = m; for (int i = n; i; i -- )//从后往前推 for (int k = 0; k <= j; k ++ ) if (f[i - 1][j - k] + w[i][k] == f[i][j]) { way[i] = k; j -= k; break; } for (int i = 1; i <= n; i ++ ) cout << i << ' ' << way[i] << endl; return 0; }