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