def s d
r=2**32
return@m[m]if@m[m=d*""]
return 0if 2>d.size
@m[m]=(d.size-1).times.map{|i|d[i,2].flatten.inject(&:*)+s(d[0,i]+[[d[i][0],d[i+1][1]]]+d[i+2,d.size])}.min
end
@m={}
gets;p s($<.map{|v|v.split.map(&:to_i)})