N = gets.to_i V = gets.split.map(&:to_i) $dp = {} def f(v ,i) $dp[i] ||= if i < 1 V[i] elsif i < 2 [V[i], V[i - 1]].max else [V[i] + f(v, i-2), f(v, i - 1)].max end end p f(0, N-1)