gets $t=gets.to_i s,*$a=gets.split.map(&:to_i) $memo={} def dfs i,n return if $memo[[i,n]] $memo[[i,n]]=true return n==$t&&[] if !$a[i] return if n>$t if r=dfs(i+1,n+$a[i]) [?+]+r elsif r=dfs(i+1,n*$a[i]) [?*]+r end end puts dfs(0,s)*""