If you are looking to minimize space wastage on the dvd, then this is a bin packing problem:
http://en.wikipedia.org/wiki/Bin_packing_problem
If you want something simple perhaps you try something like this:
1. Sort the rpm files in decreasing order by size.
2. Create 18GB/4GB = 5 bins (directories).
3. Pick one item off the top of the sorted list & put it into the first bin where it can fit.
4. Repeat step 3 until the list is empty.
Give it a try & don't forget to show us your script when its done. :-)
Of course, if you have any problems while writing the script you can always ask.