N = gets.to_i Q = N.times.map { gets.split.map(&:to_i) } dp = Array.new(N + 1) { Array.new(2, 0) } dp[0][0] = Q[0][0] dp[0][1] = Q[0][1] (1...N).each do |i| q, a = Q[i] _, ba = Q[i - 1] dp[i][0] = [dp[i - 1][0] + q, dp[i - 1][1] + 2 * q].max dp[i][1] = [dp[i - 1][0] + a, dp[i - 1][1] + 2 * a].max end puts dp[N - 1].max