First, give you a pair of goods, then give you n and N numbers to indicate the price, 10-The price is the profit
Only the first I can be obtained at a time. The maximum profit corresponds to several goods, and no more than 10 solutions can be obtained.
Idea: first, calculate the maximum profit, then store all the solutions, and then find out all the possibilities.
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <queue>#include <vector>#include <set>using namespace std;const int MAXN = 100;int w, val[MAXN][MAXN];set<int > ans;vector<int > p[MAXN];void cal(int n, int sum) {if (n == w) {ans.insert(sum);return;}for (int i = 0; i < p[n].size(); i++)cal(n+1, sum+p[n][i]);}void solve() {ans.clear();int valMax[MAXN], valSum, pro;valSum = 0;memset(valMax, 0, sizeof(valMax));for (int i = 0; i < w; i++) {p[i].clear();pro = 0;for (int j = 1; j <= val[i][0]; j++) {pro += 10 - val[i][j];valMax[i] = max(valMax[i], pro);}valSum += valMax[i];if (valMax[i] == 0)p[i].push_back(0);pro = 0;for (int j = 1; j <= val[i][0]; j++) {pro += 10 - val[i][j];if (valMax[i] == pro)p[i].push_back(j);}}cal(0, 0);printf("Maximum profit is %d.\n", valSum);printf("Number of pruls to buy:");int cnt = 0;for (set<int>::iterator i = ans.begin(); i != ans.end() && cnt != 10; i++, cnt++) printf(" %d", *i);printf("\n");}int main() {int t = 0;while (scanf("%d", &w) != EOF && w) {int b;for (int i = 0; i < w; i++) {scanf("%d", &val[i][0]);for (int j = 1; j <= val[i][0]; j++)scanf("%d", &val[i][j]);}if (t)printf("\n");printf("Workyards %d\n", ++t);solve();}return 0;}