prices = readlines.map(&:to_i)[1..-1]

profits = prices[0...-1].map.with_index { |price, index|
  profit = prices[index + 1] - price
  [profit, index]
}

positive_profits, negative_profits = profits.partition { |(profit, _)|
  profit >= 0
}

if positive_profits.empty?
  puts negative_profits.max_by { |(profit, _)| profit }.first
  exit
end

previous = nil
profit_sum = nil
max_profit = 0
positive_profits.each do |profit, index|
  previous ||= index
  profit_sum ||= 0

  if (index - previous) > 1
    max_profit = [profit_sum, max_profit].max
    previous, profit_sum = nil, nil
  else
    profit_sum += profit
    previous = index
  end
end

max_profit = [profit_sum, max_profit].max
puts max_profit