gets arr = gets.chomp.split.map(&:to_i) dp = {} dp[0] = {} dp[0][0] = arr[0] dp[0][1] = arr[0] def calc_min_and_max(arr, cur) r = [] c = 0 0.upto(1) do |i| r[c] = arr[i] + cur c += 1 r[c] = arr[i] - cur c += 1 r[c] = arr[i] * cur c += 1 r[c] = arr[i] / cur c += 1 end [r.max, r.min] end 1.upto(arr.length - 1) do |i| dp[i] = {} dp[i] = calc_min_and_max(dp[i - 1], arr[i]) end puts dp[arr.size - 1].max