Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(solution).
- -export([main/0]).
- main() ->
- {ok,[ArrLth,SCount]}=io:fread("","~d~d"),
- Arr=read_row(),
- List=lists:duplicate(math:pow(2,round(math:log2(ArrLth)))-ArrLth,0),
- io:format("~w,~w~n",[lists:reverse(build_tree(Arr++List,[],[]))))]).
- build_tree([],T,N) ->
- case length(T)>1 of
- true -> build_tree(lists:reverse(T),[],lists:reverse(T)++N);
- false -> lists:reverse(T++N) end;
- build_tree([A],T,N) -> build_tree([],[A|T],N);
- build_tree([A,B|Arr],T,N) ->
- build_tree(Arr,[sum(A,B)|T],N).
- read_row() ->
- Line = io:get_line(""),
- LineWithoutNL = string:strip(string:strip(Line, both, 10),both,13),
- StrNumbers = re:split(LineWithoutNL, "\s+", [notempty]),
- lists:map(fun erlang:binary_to_integer/1,StrNumbers).
- sum(A,B) -> A+B.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement