class Yukicoder def initialize n = gets.to_i c = gets.chomp.split nums = c.select{|i| i =~ /\d/}.sort.reverse operation = c.select{|i| i =~ /(\+|\-)/}.sort divide_num = operation.size + 1 list = [nums[0..-divide_num].join, nums[-divide_num+1..-1]].flatten if operation.include?('-') puts [eval(list.zip(operation).join), eval(list.reverse.zip(operation).join)].join(' ') else max_val = eval(list.zip(operation).join) a = Array.new(operation.size+1){""} mod = operation.size + 1 index = 0 while !nums.empty? num = nums.pop a[index%mod] = a[index%mod] + num index += 1 end a = a.map{|i| i.to_i(10)} min_val = eval(a.join('+')) puts [max_val, min_val].join(' ') end end end Yukicoder.new