a = [*1..gets.to_i] res = [] res << a.shift loop{ a.empty? ? break : res << a.pop a.empty? ? break : res.unshift(a.pop) a.empty? ? break : res << a.shift a.empty? ? break : res.unshift(a.shift) } res << res[0] p res.each_cons(2).inject(0){|r,(a,b)| r + a * b }