n=gets.to_i bs=11 bmask=(1<< bs)-1 pq_base=[nil] gets.split.each.with_index(1){|j,i| a=j.to_i<< bs i/=2 while i>1 && a= max_procs pid = Process.wait counts << pids[pid].gets.to_i pids[pid].close pids.delete(pid) end read, write = IO.pipe pid = fork{ read.close pq=pq_base el.rotate(i).each{|e| a=pq[n] d=pq[i=1]+e while (c=i*2)pq[c+1] break if a<=pq[i]=pq[c] i=c end pq[i]=a i=n i/=2 while i>1 && d