def main() n = gets.to_i str = gets.to_s.split(" ") sum = 0 v = [] str.each { |num| v << num.to_i } if maxLeft(v[0], v[1]) then index = 0 else index = 1 end sum = v[index] loop { if index+2 >= n then break elsif index+3 >= n then sum += v[index+1] break elsif maxLeft(v[index+2], v[index+3]) then index += 2 else index += 3 end sum += v[index] } p sum end def maxLeft(a, b) return a > b ? true : false end main()