gets arr = gets.chomp.split.map(&:to_i) dp = {} dp[0] = {} dp[0][0] = arr[0] dp[1] = {} dp[1][0] = dp[0][0] + arr[1] dp[1][1] = dp[0][0] - arr[1] dp[1][2] = dp[0][0] * arr[1] dp[1][3] = dp[0][0] / arr[1] 2.upto(arr.length - 1) do |i| dp[i] = {} c = 0 0.upto(dp[i - 1].length- 1) do |j| dp[i][c] = dp[i - 1][j] + arr[i] c += 1 dp[i][c] = dp[i - 1][j] - arr[i] c += 1 dp[i][c] = dp[i - 1][j] * arr[i] c += 1 dp[i][c] = dp[i - 1][j] / arr[i] c += 1 end end max = -0xFFFFFFFFF dp[arr.length - 1].map { |k| max = k[1] if max < k[1] } puts max