#!/usr/bin/env perl use strict; use warnings; my %h; my %memo; sub dfs { my ($one, $five, $count) = @_; if (exists($memo{($one, $five)})) { return; } $memo{($one, $five)} = 1; if ($one > 0) { $h{$count + 1} = 1; dfs($one - 1, $five, $count + 1); } if ($five > 0) { $h{$count + 5} = 1; dfs($one, $five - 1, $count + 5); } } my ($x, $y) = split / /,<>; chomp$y; dfs $x, $y, 0; my @vs; while (my ($k, $v) = each(%h)) { push @vs, $k; } for my $x (sort {$a <=> $b} @vs) { print $x . "\n"; }