This is unbound knapsack problem. When using the greedy method, you will get an approximate answer, which is not always right. For example, due to the length of the wire are given, it is possible you can't use all wire and has some wire left.
Here is a simple example: if the wire length is 50.
p1.length = 10, value =60;
p2.length = 20, value =100;
p3.length = 30, value =120;
Using greedy method, you will get p1+p2, value = 160.
However, the right solution is p2+p3, value = 220
By using the following method, the lengths must be an integer.
L is the total length of this wire.
For Y ≤ L, we will define the highest value of the total price as V(Y) under the premise that the total weight does not exceed Y.
So, V(L) is the answer to the question.
Pj is the price of the jth piece, lj is the length of the jth piece.(n kinds)
Recurrence relation:
V(0) = 0
V(Y) = max { V(Y - 1), { pj + V(Y - lj) | lj ≤ Y } }
There may be two cases where the maximum length of the wire is Y. The first is that the length cannot be completely used, which corresponds to the expression A(Y-1). The second is just used up.
Time complexity is O(nL). Because form length 1 to L, in every step you have to check all kinds of cut-lengths.