Question: give you a given amount of cash goal, and you have n kinds of cash.
The next step is how much cash you own with N denominations. You need to find the maximum value that can be obtained by using cash in your hand for a given goal!
Thought: Multi-backpack problem, state equation dp (I, V) = max (dp (I, j), DP (I-1, V-KCI) + KWI );
After optimization, the AC code is as follows:
# Include <stdio. h> # include <string. h> struct P {int N; int D;} num [11]; int DP [100010]; int main () {int I, j, N, goal, K, cmax; while (scanf ("% d", & goal, & N )! = EOF) {for (I = 0; I <n; I ++) scanf ("% d", & num [I]. n, & num [I]. d); memset (DP, 0, sizeof (DP); DP [0] = 1; // C starts from 0 Cmax = 0; for (I = 0; I <n; I ++) for (j = Cmax; j> = 0; j --) if (DP [J]) // If for (k = 1; k <= num [I]. n; k ++) {int temp = J + K * num [I]. d; If (temp> goal) break; DP [temp] = 1; // mark from the current position and search continuously if (temp> Cmax) Cmax = temp ;} printf ("% d \ n", Cmax);} return 0 ;}