#!/usr/bin/perl
use 5.016;
use warnings;
use List
::Util qw(reduce
);
sub f {
my ($n, $l) = @_;
my $x = reduce{ $a * 2 + $b } @{$n};
return ($_[0]->[0] ? $x - (2 ** @{$n}) : $x); }
my @l = sub {
$_[0] < 1 ? @{$_[1]} : __SUB__
->(int($_[0] / 2), [$_[0] % 2, @{$_[1]}]) }->(($n < 0 ? $n + 2 ** ceil
(lb
(abs $n) + 1) : $n), []);
($_[0]) x
(defined $l ? $l - @l : $_[1]), @l }->($n < 0 ? (1, 0) : (0, 1));
}
say f(0);
say f(1);
say f(-1);
say f(3);
say f(-3);
say f(10);
say f(10, 8);
say f(-8);
say f(-8, 8);
say f([0]);
say f([0, 1]);
say f([1]);
say f([0, 1, 1, 1]);
say f([1, 1, 1, 1]);
say f([1, 0, 0, 0]);
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwpubyB3YXJuaW5ncyBxdyhvbmNlKTsKdXNlIExpc3Q6OlV0aWwgcXcocmVkdWNlKTsKdXNlIFBPU0lYIHF3KGNlaWwpOwoKc3ViIGxiIHsgbG9nKHNoaWZ0KSAvIGxvZygyKSB9CgpzdWIgZiB7CglteSAoJG4sICRsKSA9IEBfOwoKCWlmIChyZWYgJG4pewoJCW15ICR4ID0gcmVkdWNleyAkYSAqIDIgKyAkYiB9IEB7JG59OwoJCXJldHVybiAoJF9bMF0tPlswXSA/ICR4IC0gKDIgKiogQHskbn0pIDogJHgpOwoJfQoKCW15IEBsID0gc3ViIHsKCQkkX1swXSA8IDEgPyBAeyRfWzFdfSA6IF9fU1VCX18tPihpbnQoJF9bMF0gLyAyKSwgWyRfWzBdICUgMiwgQHskX1sxXX1dKQoJfS0+KCgkbiA8IDAgPyAkbiArIDIgKiogY2VpbChsYihhYnMgJG4pICsgMSkgOiAkbiksIFtdKTsKCglyZXR1cm4gc3ViIHsKCQkoJF9bMF0pIHggKGRlZmluZWQgJGwgPyAkbCAtIEBsIDogJF9bMV0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