An algorithm for a minimization problem, How to minimize the wasted length of combination of multiple items with different length and number
Take a look to knapsack problem.

Simple algorithm (not the optimal solution) 


order the item descending 
cut the n5/(x\x5) ("\" is integer division, i.e. 12\9=1) = 15 pipes to satisfy x5 = 9m and partially x1


Repeat step 2 for x3 = 7m and so on.