gets vs = gets.split.map(&:to_i) S = Struct.new(:val, :idxs) a = [] a[0] = S.new(vs[0], [0]) if vs.size > 0 a[1] = S.new(vs[1], [1]) if vs.size > 1 a[2] = S.new(vs[0] + vs[2], [0, 2]) if vs.size > 2 for i in 3...vs.size do s_prev = a[i-3].val > a[i-2].val ? a[i-3] : a[i-2] a[i] = S.new(s_prev.val + vs[i], s_prev.idxs + [i]) end s_max = a.size >= 2 && a[-2].val > a[-1].val ? a[-2] : a[-1] puts s_max.val puts s_max.idxs.map{|i| i + 1}.join(" ")