defmodule Main do def main do IO.read(:line) IO.read(:line) |> String.trim() |> String.split() |> Enum.map(&String.to_integer/1) |> solve_rec(0) |> IO.puts() end def solve_rec([], x) do x end def solve_rec(a, x) do l = (a |> hd) - (a |> tl |> hd) r = (a |> Enum.at(-1)) - (a |> Enum.at(-2)) cond do l < r -> solve_rec(a |> Enum.slice(0..-3), x + r) true -> solve_rec(a |> Enum.slice(2..-1), x + l) end end end