main(_) ->
    {ok, [N, K]} = io:fread("", "~d ~d"),
    Fruits = read_n_data(N),
    Sorted = lists:sort(Fruits),
    {BuyList, _} = lists:split(K, Sorted),
    Sum = lists:sum(BuyList),
    io:format("~p~n", [Sum]),
    halt().


%% N個データ読み込み
read_n_data(N) -> read_n_data(N, []).
read_n_data(0, Acc) -> Acc;
read_n_data(N, Acc) -> 
    {ok, [NewElement]} = io:fread("", "~d"),
    read_n_data(N - 1, [NewElement|Acc]).