N = gets.to_i A = gets.split.map(&:to_i) B = gets.split.map(&:to_i) X = gets.split.map(&:to_i) Y = gets.split.map(&:to_i) sum = 0 N.times do |i| sum += A[2 * i] + A[2 * i + 1] + X[i] end dp = Array.new(N + 1, sum) N.times do |i| # dobule red if dp[i + 1] < dp[i] dp[i + 1] = dp[i] end # one red one blud if dp[i + 1] < dp[i] + (Y[i] - X[i]) + (B[2 * i] - A[2 * i]) dp[i + 1] = dp[i] + (Y[i] - X[i]) + (B[2 * i] - A[2 * i]) end # one blue one red if dp[i + 1] < dp[i] + (Y[i] - X[i]) + (B[2 * i + 1] - A[2 * i + 1]) dp[i + 1] = dp[i] + (Y[i] - X[i]) + (B[2 * i + 1] - A[2 * i + 1]) end # double blue if dp[i + 1] < dp[i] + (B[2 * i] - A[2 * i]) + (B[2 * i + 1] - A[2 * i + 1]) dp[i + 1] = dp[i] + (B[2 * i] - A[2 * i]) + (B[2 * i + 1] - A[2 * i + 1]) end end pp dp.max