Save money with price rounding In Canada, the penny is no longer circulated.  Cash payments are rounded to the nearest 5 cents.

Money can be saved by splitting purchases.  For example, two $1.02 items cost $2.04 which rounds up to $2.05, but when buying the items in separate purchases, each price rounds to $1.00 for a total of $2.00.  However, when buying two items at $1.03 each, it is better to buy them in a single purchase.

Another way to save money is using a credit card when rounding is unfavourable, because credit payments are not rounded.  If we want two $1.04 items, the total price will round up to $2.10 regardless of how we split the purchases.  Therefore, we should pay for these items with a credit card.

Write a function or program which accepts a list of prices of items as integers in cents and outputs the lowest possible total price (in cents) for those items which can be achieved through a sequence of purchases, each either by cash or by credit.

Shortest code wins.

Test cases

[] : 0
[48] : 48
[92, 20] : 110
[47, 56, 45] : 145
[55, 6, 98, 69] : 225
[6, 39, 85, 84, 7] : 218
[95, 14, 28, 49, 41, 39] : 263
[92, 6, 28, 30, 39, 93, 53] : 335
[83, 33, 62, 12, 34, 29, 18, 12] : 273
[23, 46, 54, 69, 64, 73, 58, 92, 26] : 495
[19, 56, 84, 23, 20, 53, 96, 92, 91, 58] : 583
[3, 3, 19, 56, 3, 84, 3, 23, 20, 53, 96, 92, 91, 58, 3, 3] : 598
[2, 3, 4, 4, 4, 4, 4] : 19
Ruby, 119 105 characters (93 body)

def f s
a,b,c,d=(1..4).map{|i|s.count{|x|x%5==i}}
s.reduce(0,:+)-a-(c-m=c&gt;d ?d:c)/2-2*(b+m+(d-m)/3)
end


Two characters may be saved if the algorithm is allowed to crash when fed an empty shopping list.