($n,$m,$k,$o,@b)=glob<>.<>;--$k;print+(map{$_+=$_&-$_ until$#{$_}++,$_>$k}@b)*(map{$-=v27^$o?$k/$_:$k-$_;0while$a+=@{$-},$--=$-&-$-}<>)-$a