Packet type backpack
to sum up:
1. The first circulation volume, each group of items within the recirculation, within each group to ensure that the article is selected only once.
If the exchange position, it is possible within each of the selected multiple items.
2.num array record the number of items in each group;
each array belong recording a sequence number of each article of the article is much
Very clever way
#include<bits/stdc++.h> using namespace std; int dp[1005], val[1005], w[1005], num[1005], belong[101][20]; int maxx, m, n, a, b, c; int main() { cin >> m >> n; for(int i = 1; i <= n; i++) { cin >> a >> b >> c; val[i] = b; w[i] = a; maxx = max(maxx, c); num[c]++; belong[c][num[c]] = i; } for(int i = 1; i <= maxx; i++) for(int j = m; j >= 0; j--) for(int k = 1; k <= num[i]; k++) if(j >= w[belong[i][k]]) dp[j] = max(dp[j], dp[j-w[belong[i][k]]]+val[belong[i][k]]); cout << dp[m]; return 0; }