def dfs(deck)
  result = 2 ** 32
  return @memo[deck*""] if @memo[deck*""]
  return 0 if deck.size == 1
  (deck.size - 1).times do |i|
    p cost = deck[i, 2].flatten.inject(&:*)
    result = [result, cost + dfs(deck[0, i] + [[deck[i][0], deck[i+1][1]]] + deck[i + 2, deck.size])].min
  end
  @memo[deck*""] = result
end

@memo = Hash.new
p dfs(gets.to_i.times.map{gets.split.map(&:to_i)})