3900
This question belongs to the 01 knapsack problem, the core is 01 knapsack of the state transition equation for this question to set P[i] for the object of the amount of money, z[i] for the importance of the object, and another array dp[i],n for the total money held, m for the total number of items to buy the problem of the conversion equation is Dp[n]=max (dp[i ],dp[n-p[i]]+z[i]*p[i]) explain: When comparing each set of data, the backpack capacity is changed from N, to the end of the price of this set of data, the first set of data, the price is 800, the importance of 2, then the backpack capacity is reduced, to 800 stops, the array dp[ 800] to dp[1000] has been assigned the initial value, and then compare the second set of data if the result of the second set of data is larger than the first set of data with a large value covering the front of the small value, and so of course, when the amount of money in the back and not more than N, then take and seek to the maximum value
Enclosed AC Code
#include <stdio.h> #include <string.h>int main () {int dp[50000]; Note that this array of prices cannot be opened too small int n,n,m,j,i;int w[30],p[30]; scanf ("%d", &n), while (n--) {memset (dp,0,sizeof (DP)); scanf ("%d%d", &n,&m); for (i=0;i<m;i++) scanf ("%d %d ", &w[i],&p[i]); for (i=0;i<m;i++) {for (j=n;j>=w[i];j--) { if (dp[j]<dp[j-w[i]]+p[i ]*w[i]) //The Core state transition equation of the problem dp[j]=dp[j-w[i]]+p[i]*w[i];} } printf ("%d\n", Dp[n]);} return 0;}