Buy-N-Large has to sell quite a lot of small useless goods, and to maximize throughput in their warehouses, they use giant rotating shelves. As item pi arrives, they place it on the top shelf, exactly 1 inch to the right of item pi−1 , or, if the shelf is empty, aligned all the way to left side of the shelf. Each item p(i) is exactly w(i)inches wide and each shelf is W inches wide. When they are done putting items on a shelf (which may happen at any time), they rotate the shelf down and begin stocking the shelf above. No item may hang of the edges or the shelves will jam when they reach the un-stocking robots at the bottom of the shelf device (see diagram). At some point in time, the top shelf is empty. Call the next item to arrive p(1) . In order to minimize the motion by the robots that stock and unstock the shelves, we want to stock items p(1) . . . p(P) in such a way that the sum of squares of the distance from the rightmost element of a shelf to the right edge
of the shelf is minimized.
Give an algorithm that decides which items go on which shelves (which is equivalent to deciding between which items to start stocking the next shelf) and minimizes this value. Demonstrate that it is correct.
[Hint: The solution is either a greedy algorithm or a dynamic programming approach. In both cases, prove that optimal substructure property exists. If you think a greedy approach should be used, prove also that the greedy choice property holds. If you think a dynamic programming approach should be used, explain the order of computing the subproblems. In both cases, explain your algorithm clearly, although you do not need to give an explicit pseudocode. Analyze the runtime of your approach (ideally, it should not be more than O(n3) with preprocessing if necessary).]