Your code needs some TLC and your for (0 .. $#list) should probably be for (;;) loops. I had this idea too but felt it wasn't in the spirit of what ikegami
intended (though it meets my criteria just fine).
- Find the lowest sum paying attention to how many times it appears, then print
- Find the lowest sum that is greater than the previous pass paying attention to how many times it appears, then print
- Repeat until there are no new values found
The best solution I have (memory/time) is 4N + M but the most memory efficient while still having a reasonable run time is 2N + M.