defmodule Main do def main do [n, k] = IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) an = IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) solve(n, k, an) |> IO.puts() end def solve(n, k, an) when 1 <= k and k <= n and n <= 100 do an |> Enum.sort(&(&1 > &2)) |> Enum.reject(&(&1 < 1)) |> (fn [] -> [t | _] = an [t] x -> x end).() |> Stream.take(k) |> Enum.reduce(0, &(&1 + &2)) end end