sek1pa

rmq

Jan 26th, 2018
614
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -module(solution).
  2. -export([main/0]).
  3.  
  4. main() ->
  5.     {ok,[ArrLth,SCount]}=io:fread("","~d~d"),
  6.     Arr=read_row(),
  7.     List=lists:duplicate(math:pow(2,round(math:log2(ArrLth)))-ArrLth,0),
  8.     io:format("~w,~w~n",[lists:reverse(build_tree(Arr++List,[],[]))))]).
  9.  
  10. build_tree([],T,N) ->
  11.     case length(T)>1 of
  12.     true -> build_tree(lists:reverse(T),[],lists:reverse(T)++N);
  13.     false -> lists:reverse(T++N) end;
  14.  
  15. build_tree([A],T,N) -> build_tree([],[A|T],N);
  16. build_tree([A,B|Arr],T,N) ->
  17.     build_tree(Arr,[sum(A,B)|T],N).
  18.  
  19. read_row() ->
  20.     Line = io:get_line(""),
  21.     LineWithoutNL = string:strip(string:strip(Line, both, 10),both,13),
  22.     StrNumbers = re:split(LineWithoutNL, "\s+", [notempty]),
  23.     lists:map(fun erlang:binary_to_integer/1,StrNumbers).
  24.  
  25. sum(A,B) -> A+B.
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×