Question:
There are N items and a backpack with a capacity of V. The consumption of item I is c [I], and the value is w [I]. Solving which items are loaded into a backpack can maximize the total value. (Note: each item has only one item. You can choose to put it in a backpack or not)
Problem solving process:
Let's assume that value [I, v] indicates the maximum value that can be obtained by placing the first I item into a backpack with a capacity of v;
So value [I, v] = max (value [I-1, v], value [I-1, v-c [I] + w [I])
This statement can be interpreted as whether to put the I-th item into the backpack, if you do not put item I into the backpack, then the former I-1 items into the capacity of the v backpack (value [I-1, v]), if the first item is placed in a backpack, the first I-1 item can only be placed in a backpack with a capacity of v-c [I] (value: value [I-1, v-c [I] + w [I], w [I] is the value of the I-th item ).
For (I = 1; I <= N; I ++)
For (v = c [I]; v <= V; v ++)
Value [I, v] = max (value [I-1, v], value [I-1, v-c [I] + w [I]);