n = gets.chomp.to_i a = gets.chomp.split(" ").map(&:to_i) a = a.sort now_kai = 0 min_ans = 0 while true max = 2 ** now_kai i = 0 ok = true while ok min_ans += a.pop * now_kai #print "{a}" + " " + "#{min_ans}" + " " +"#{now_kai}" +" "+"#{i}"+ "\n" i += 1 ok = false if a == [] break if max == i #i += 1 end break if ok == false now_kai += 1 #puts "^^^^^^^^^^^^^^" end puts min_ans